Skip to content

Commit 1cbbe28

Browse files
authored
Treat any mix of element/prop access as declaration in JS (microsoft#34649)
Fixes microsoft#34642 but, notably, doesn't actually make the assignment into a working declaration. It just fixes the crash.
1 parent 159b626 commit 1cbbe28

5 files changed

+50
-8
lines changed

src/compiler/utilities.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,26 +2061,30 @@ namespace ts {
20612061
isBindableStaticNameExpression(expr.arguments[0], /*excludeThisKeyword*/ true);
20622062
}
20632063

2064-
export function isBindableStaticElementAccessExpression(node: Node, excludeThisKeyword?: boolean): node is BindableStaticElementAccessExpression {
2065-
return isLiteralLikeElementAccess(node)
2066-
&& ((!excludeThisKeyword && node.expression.kind === SyntaxKind.ThisKeyword) ||
2067-
isEntityNameExpression(node.expression) ||
2068-
isBindableStaticElementAccessExpression(node.expression, /*excludeThisKeyword*/ true));
2069-
}
2070-
2064+
/** x.y OR x[0] */
20712065
export function isLiteralLikeAccess(node: Node): node is LiteralLikeElementAccessExpression | PropertyAccessExpression {
20722066
return isPropertyAccessExpression(node) || isLiteralLikeElementAccess(node);
20732067
}
20742068

2069+
/** x[0] OR x['a'] OR x[Symbol.y] */
20752070
export function isLiteralLikeElementAccess(node: Node): node is LiteralLikeElementAccessExpression {
20762071
return isElementAccessExpression(node) && (
20772072
isStringOrNumericLiteralLike(node.argumentExpression) ||
20782073
isWellKnownSymbolSyntactically(node.argumentExpression));
20792074
}
20802075

2076+
/** Any series of property and element accesses. */
20812077
export function isBindableStaticAccessExpression(node: Node, excludeThisKeyword?: boolean): node is BindableStaticAccessExpression {
20822078
return isPropertyAccessExpression(node) && (!excludeThisKeyword && node.expression.kind === SyntaxKind.ThisKeyword || isBindableStaticNameExpression(node.expression, /*excludeThisKeyword*/ true))
2083-
|| isBindableStaticElementAccessExpression(node, excludeThisKeyword);
2079+
|| isBindableStaticElementAccessExpression(node, excludeThisKeyword);
2080+
}
2081+
2082+
/** Any series of property and element accesses, ending in a literal element access */
2083+
export function isBindableStaticElementAccessExpression(node: Node, excludeThisKeyword?: boolean): node is BindableStaticElementAccessExpression {
2084+
return isLiteralLikeElementAccess(node)
2085+
&& ((!excludeThisKeyword && node.expression.kind === SyntaxKind.ThisKeyword) ||
2086+
isEntityNameExpression(node.expression) ||
2087+
isBindableStaticAccessExpression(node.expression, /*excludeThisKeyword*/ true));
20842088
}
20852089

20862090
export function isBindableStaticNameExpression(node: Node, excludeThisKeyword?: boolean): node is BindableStaticNameExpression {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [mixedPropertyElementAccessAssignmentDeclaration.ts]
2+
// Should not crash: #34642
3+
var arr = [];
4+
arr[0].prop[2] = {};
5+
6+
7+
//// [mixedPropertyElementAccessAssignmentDeclaration.js]
8+
// Should not crash: #34642
9+
var arr = [];
10+
arr[0].prop[2] = {};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/salsa/mixedPropertyElementAccessAssignmentDeclaration.ts ===
2+
// Should not crash: #34642
3+
var arr = [];
4+
>arr : Symbol(arr, Decl(mixedPropertyElementAccessAssignmentDeclaration.ts, 1, 3))
5+
6+
arr[0].prop[2] = {};
7+
>arr : Symbol(arr, Decl(mixedPropertyElementAccessAssignmentDeclaration.ts, 1, 3))
8+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/salsa/mixedPropertyElementAccessAssignmentDeclaration.ts ===
2+
// Should not crash: #34642
3+
var arr = [];
4+
>arr : any[]
5+
>[] : undefined[]
6+
7+
arr[0].prop[2] = {};
8+
>arr[0].prop[2] = {} : {}
9+
>arr[0].prop[2] : any
10+
>arr[0].prop : any
11+
>arr[0] : any
12+
>arr : any[]
13+
>0 : 0
14+
>prop : any
15+
>2 : 2
16+
>{} : {}
17+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Should not crash: #34642
2+
var arr = [];
3+
arr[0].prop[2] = {};

0 commit comments

Comments
 (0)