Skip to content

Commit d75de60

Browse files
rchaser53RyanCavanaugh
authored andcommitted
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
1 parent d9f0212 commit d75de60

File tree

6 files changed

+71
-5
lines changed

6 files changed

+71
-5
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,15 +3525,19 @@ namespace ts {
35253525
}
35263526

35273527
function getTypeNamesForErrorDisplay(left: Type, right: Type): [string, string] {
3528-
let leftStr = typeToString(left);
3529-
let rightStr = typeToString(right);
3528+
let leftStr = symbolValueDeclarationIsContextSensitive(left.symbol) ? typeToString(left, left.symbol.valueDeclaration) : typeToString(left);
3529+
let rightStr = symbolValueDeclarationIsContextSensitive(right.symbol) ? typeToString(right, right.symbol.valueDeclaration) : typeToString(right);
35303530
if (leftStr === rightStr) {
35313531
leftStr = typeToString(left, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
35323532
rightStr = typeToString(right, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
35333533
}
35343534
return [leftStr, rightStr];
35353535
}
35363536

3537+
function symbolValueDeclarationIsContextSensitive(symbol: Symbol): boolean {
3538+
return symbol && symbol.valueDeclaration && isExpression(symbol.valueDeclaration) && !isContextSensitive(symbol.valueDeclaration);
3539+
}
3540+
35373541
function toNodeBuilderFlags(flags = TypeFormatFlags.None): NodeBuilderFlags {
35383542
return flags & TypeFormatFlags.NodeBuilderFlagsMask;
35393543
}
@@ -12718,8 +12722,8 @@ namespace ts {
1271812722
}
1271912723

1272012724
function tryElaborateErrorsForPrimitivesAndObjects(source: Type, target: Type) {
12721-
const sourceType = typeToString(source);
12722-
const targetType = typeToString(target);
12725+
const sourceType = symbolValueDeclarationIsContextSensitive(source.symbol) ? typeToString(source, source.symbol.valueDeclaration) : typeToString(source);
12726+
const targetType = symbolValueDeclarationIsContextSensitive(target.symbol) ? typeToString(target, target.symbol.valueDeclaration) : typeToString(target);
1272312727

1272412728
if ((globalStringType === source && stringType === target) ||
1272512729
(globalNumberType === source && numberType === target) ||

tests/baselines/reference/errorElaboration.errors.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
tests/cases/compiler/errorElaboration.ts(10,18): error TS2300: Duplicate identifier 'foo'.
12
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>>'.
23
Type 'Container<Ref<string>>' is not assignable to type 'Container<Ref<number>>'.
34
Type 'Ref<string>' is not assignable to type 'Ref<number>'.
45
Type 'string' is not assignable to type 'number'.
56
tests/cases/compiler/errorElaboration.ts(17,11): error TS2322: Type '"bar"' is not assignable to type '"foo"'.
7+
tests/cases/compiler/errorElaboration.ts(22,7): error TS2300: Duplicate identifier 'foo'.
8+
tests/cases/compiler/errorElaboration.ts(23,15): error TS2538: Type 'any' cannot be used as an index type.
9+
tests/cases/compiler/errorElaboration.ts(23,19): error TS2339: Property 'bar' does not exist on type '(x: () => Container<Ref<number>>) => void'.
610

711

8-
==== tests/cases/compiler/errorElaboration.ts (2 errors) ====
12+
==== tests/cases/compiler/errorElaboration.ts (6 errors) ====
913
// Repro for #5712
1014

1115
interface Ref<T> {
@@ -16,6 +20,8 @@ tests/cases/compiler/errorElaboration.ts(17,11): error TS2322: Type '"bar"' is n
1620
m2: T;
1721
}
1822
declare function foo(x: () => Container<Ref<number>>): void;
23+
~~~
24+
!!! error TS2300: Duplicate identifier 'foo'.
1925
let a: () => Container<Ref<string>>;
2026
foo(a);
2127
~
@@ -32,4 +38,15 @@ tests/cases/compiler/errorElaboration.ts(17,11): error TS2322: Type '"bar"' is n
3238
!!! error TS2322: Type '"bar"' is not assignable to type '"foo"'.
3339
!!! related TS6500 tests/cases/compiler/errorElaboration.ts:16:18: The expected type comes from property 'foo' which is declared here on type '{ foo: "foo"; }'
3440
}
41+
42+
// Repro for #32358
43+
44+
const foo = { bar: 'a' };
45+
~~~
46+
!!! error TS2300: Duplicate identifier 'foo'.
47+
const x = ({ [foo.bar]: c }) => undefined;
48+
~~~~~~~
49+
!!! error TS2538: Type 'any' cannot be used as an index type.
50+
~~~
51+
!!! error TS2339: Property 'bar' does not exist on type '(x: () => Container<Ref<number>>) => void'.
3552

tests/baselines/reference/errorElaboration.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ foo(a);
1717
function test(): {[A in "foo"]: A} {
1818
return {foo: "bar"};
1919
}
20+
21+
// Repro for #32358
22+
23+
const foo = { bar: 'a' };
24+
const x = ({ [foo.bar]: c }) => undefined;
2025

2126

2227
//// [errorElaboration.js]
@@ -27,3 +32,9 @@ foo(a);
2732
function test() {
2833
return { foo: "bar" };
2934
}
35+
// Repro for #32358
36+
var foo = { bar: 'a' };
37+
var x = function (_a) {
38+
var _b = foo.bar, c = _a[_b];
39+
return undefined;
40+
};

tests/baselines/reference/errorElaboration.symbols

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ function test(): {[A in "foo"]: A} {
4949
>foo : Symbol(foo, Decl(errorElaboration.ts, 16, 10))
5050
}
5151

52+
// Repro for #32358
53+
54+
const foo = { bar: 'a' };
55+
>foo : Symbol(foo, Decl(errorElaboration.ts, 21, 5))
56+
>bar : Symbol(bar, Decl(errorElaboration.ts, 21, 13))
57+
58+
const x = ({ [foo.bar]: c }) => undefined;
59+
>x : Symbol(x, Decl(errorElaboration.ts, 22, 5))
60+
>foo : Symbol(foo, Decl(errorElaboration.ts, 8, 1))
61+
>c : Symbol(c, Decl(errorElaboration.ts, 22, 12))
62+
>undefined : Symbol(undefined)
63+

tests/baselines/reference/errorElaboration.types

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,20 @@ function test(): {[A in "foo"]: A} {
3535
>"bar" : "bar"
3636
}
3737

38+
// Repro for #32358
39+
40+
const foo = { bar: 'a' };
41+
>foo : { bar: string; }
42+
>{ bar: 'a' } : { bar: string; }
43+
>bar : string
44+
>'a' : "a"
45+
46+
const x = ({ [foo.bar]: c }) => undefined;
47+
>x : ({ [foo.bar]: c }: {}) => any
48+
>({ [foo.bar]: c }) => undefined : ({ [foo.bar]: c }: {}) => any
49+
>foo.bar : any
50+
>foo : (x: () => Container<Ref<number>>) => void
51+
>bar : any
52+
>c : any
53+
>undefined : undefined
54+

tests/cases/compiler/errorElaboration.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ foo(a);
1616
function test(): {[A in "foo"]: A} {
1717
return {foo: "bar"};
1818
}
19+
20+
// Repro for #32358
21+
22+
const foo = { bar: 'a' };
23+
const x = ({ [foo.bar]: c }) => undefined;

0 commit comments

Comments
 (0)