Skip to content

Commit a64166d

Browse files
committed
isDynamicName skips parentheses for element access
Neither `x[0]` nor `x[(0)]` should be dynamic names. Previously, the latter was because `isDynamicName` didn't skip parentheses. Since the binder treats dynamic names in property assignments as assignment declarations, this incorrectly tried to create a binding for expressions like `x[(0)] = 1`. This caused an assert because `x[(0)]` would not take the dynamic name code path during binding (`hasDynamicName` returned false), but the normal code path for static names.
1 parent ffa35d3 commit a64166d

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3061,7 +3061,7 @@ namespace ts {
30613061
if (!(name.kind === SyntaxKind.ComputedPropertyName || name.kind === SyntaxKind.ElementAccessExpression)) {
30623062
return false;
30633063
}
3064-
const expr = isElementAccessExpression(name) ? name.argumentExpression : name.expression;
3064+
const expr = isElementAccessExpression(name) ? skipParentheses(name.argumentExpression) : name.expression;
30653065
return !isStringOrNumericLiteralLike(expr) &&
30663066
!isSignedNumericLiteral(expr) &&
30673067
!isWellKnownSymbolSyntactically(expr);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/salsa/bug38934.js ===
2+
var x = {};
3+
>x : Symbol(x, Decl(bug38934.js, 0, 3))
4+
5+
// should not crash and also should not result in a property '0' on x.
6+
x[(0)] = 1;
7+
>x : Symbol(x, Decl(bug38934.js, 0, 3))
8+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/conformance/salsa/bug38934.js ===
2+
var x = {};
3+
>x : {}
4+
>{} : {}
5+
6+
// should not crash and also should not result in a property '0' on x.
7+
x[(0)] = 1;
8+
>x[(0)] = 1 : 1
9+
>x[(0)] : any
10+
>x : {}
11+
>(0) : 0
12+
>0 : 0
13+
>1 : 1
14+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
// @Filename: bug38934.js
5+
6+
var x = {};
7+
// should not crash and also should not result in a property '0' on x.
8+
x[(0)] = 1;

0 commit comments

Comments
 (0)