Fix Cannot read property 'text' of undefined crash (#32734)

* Fix Cannot read property 'text' of undefined crash

* fix condition for tsx

* Rename and improve the method
This commit is contained in:
rChaser53 2019-08-20 04:08:34 +09:00 committed by Ryan Cavanaugh
parent d9f0212324
commit d75de60548
6 changed files with 71 additions and 5 deletions

View File

@ -3525,8 +3525,8 @@ namespace ts {
}
function getTypeNamesForErrorDisplay(left: Type, right: Type): [string, string] {
let leftStr = typeToString(left);
let rightStr = typeToString(right);
let leftStr = symbolValueDeclarationIsContextSensitive(left.symbol) ? typeToString(left, left.symbol.valueDeclaration) : typeToString(left);
let rightStr = symbolValueDeclarationIsContextSensitive(right.symbol) ? typeToString(right, right.symbol.valueDeclaration) : typeToString(right);
if (leftStr === rightStr) {
leftStr = typeToString(left, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
rightStr = typeToString(right, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
@ -3534,6 +3534,10 @@ namespace ts {
return [leftStr, rightStr];
}
function symbolValueDeclarationIsContextSensitive(symbol: Symbol): boolean {
return symbol && symbol.valueDeclaration && isExpression(symbol.valueDeclaration) && !isContextSensitive(symbol.valueDeclaration);
}
function toNodeBuilderFlags(flags = TypeFormatFlags.None): NodeBuilderFlags {
return flags & TypeFormatFlags.NodeBuilderFlagsMask;
}
@ -12718,8 +12722,8 @@ namespace ts {
}
function tryElaborateErrorsForPrimitivesAndObjects(source: Type, target: Type) {
const sourceType = typeToString(source);
const targetType = typeToString(target);
const sourceType = symbolValueDeclarationIsContextSensitive(source.symbol) ? typeToString(source, source.symbol.valueDeclaration) : typeToString(source);
const targetType = symbolValueDeclarationIsContextSensitive(target.symbol) ? typeToString(target, target.symbol.valueDeclaration) : typeToString(target);
if ((globalStringType === source && stringType === target) ||
(globalNumberType === source && numberType === target) ||

View File

@ -1,11 +1,15 @@
tests/cases/compiler/errorElaboration.ts(10,18): error TS2300: Duplicate identifier 'foo'.
tests/cases/compiler/errorElaboration.ts(12,5): error TS2345: Argument of type '() => Container<Ref<string>>' is not assignable to parameter of type '() => Container<Ref<number>>'.
Type 'Container<Ref<string>>' is not assignable to type 'Container<Ref<number>>'.
Type 'Ref<string>' is not assignable to type 'Ref<number>'.
Type 'string' is not assignable to type 'number'.
tests/cases/compiler/errorElaboration.ts(17,11): error TS2322: Type '"bar"' is not assignable to type '"foo"'.
tests/cases/compiler/errorElaboration.ts(22,7): error TS2300: Duplicate identifier 'foo'.
tests/cases/compiler/errorElaboration.ts(23,15): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/errorElaboration.ts(23,19): error TS2339: Property 'bar' does not exist on type '(x: () => Container<Ref<number>>) => void'.
==== tests/cases/compiler/errorElaboration.ts (2 errors) ====
==== tests/cases/compiler/errorElaboration.ts (6 errors) ====
// Repro for #5712
interface Ref<T> {
@ -16,6 +20,8 @@ tests/cases/compiler/errorElaboration.ts(17,11): error TS2322: Type '"bar"' is n
m2: T;
}
declare function foo(x: () => Container<Ref<number>>): void;
~~~
!!! error TS2300: Duplicate identifier 'foo'.
let a: () => Container<Ref<string>>;
foo(a);
~
@ -32,4 +38,15 @@ tests/cases/compiler/errorElaboration.ts(17,11): error TS2322: Type '"bar"' is n
!!! error TS2322: Type '"bar"' is not assignable to type '"foo"'.
!!! related TS6500 tests/cases/compiler/errorElaboration.ts:16:18: The expected type comes from property 'foo' which is declared here on type '{ foo: "foo"; }'
}
// Repro for #32358
const foo = { bar: 'a' };
~~~
!!! error TS2300: Duplicate identifier 'foo'.
const x = ({ [foo.bar]: c }) => undefined;
~~~~~~~
!!! error TS2538: Type 'any' cannot be used as an index type.
~~~
!!! error TS2339: Property 'bar' does not exist on type '(x: () => Container<Ref<number>>) => void'.

View File

@ -17,6 +17,11 @@ foo(a);
function test(): {[A in "foo"]: A} {
return {foo: "bar"};
}
// Repro for #32358
const foo = { bar: 'a' };
const x = ({ [foo.bar]: c }) => undefined;
//// [errorElaboration.js]
@ -27,3 +32,9 @@ foo(a);
function test() {
return { foo: "bar" };
}
// Repro for #32358
var foo = { bar: 'a' };
var x = function (_a) {
var _b = foo.bar, c = _a[_b];
return undefined;
};

View File

@ -49,3 +49,15 @@ function test(): {[A in "foo"]: A} {
>foo : Symbol(foo, Decl(errorElaboration.ts, 16, 10))
}
// Repro for #32358
const foo = { bar: 'a' };
>foo : Symbol(foo, Decl(errorElaboration.ts, 21, 5))
>bar : Symbol(bar, Decl(errorElaboration.ts, 21, 13))
const x = ({ [foo.bar]: c }) => undefined;
>x : Symbol(x, Decl(errorElaboration.ts, 22, 5))
>foo : Symbol(foo, Decl(errorElaboration.ts, 8, 1))
>c : Symbol(c, Decl(errorElaboration.ts, 22, 12))
>undefined : Symbol(undefined)

View File

@ -35,3 +35,20 @@ function test(): {[A in "foo"]: A} {
>"bar" : "bar"
}
// Repro for #32358
const foo = { bar: 'a' };
>foo : { bar: string; }
>{ bar: 'a' } : { bar: string; }
>bar : string
>'a' : "a"
const x = ({ [foo.bar]: c }) => undefined;
>x : ({ [foo.bar]: c }: {}) => any
>({ [foo.bar]: c }) => undefined : ({ [foo.bar]: c }: {}) => any
>foo.bar : any
>foo : (x: () => Container<Ref<number>>) => void
>bar : any
>c : any
>undefined : undefined

View File

@ -16,3 +16,8 @@ foo(a);
function test(): {[A in "foo"]: A} {
return {foo: "bar"};
}
// Repro for #32358
const foo = { bar: 'a' };
const x = ({ [foo.bar]: c }) => undefined;