Skip to content

Commit be051c4

Browse files
committed
fix(checker): also skip parentheses on element access keys
Support obj[('a')] by applying skipParentheses to argumentExpression before checking isStringOrNumericLiteralLike. Added test cases for parenthesized keys in element access.
1 parent 7f835de commit be051c4

File tree

5 files changed

+131
-1
lines changed

5 files changed

+131
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13744,7 +13744,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1374413744
if (isPropertyAccessExpression(node) && isIdentifier(node.name)) {
1374513745
return isLateBindableAccessExpression(node.expression);
1374613746
}
13747-
if (isElementAccessExpression(node) && isStringOrNumericLiteralLike(node.argumentExpression)) {
13747+
if (isElementAccessExpression(node) && isStringOrNumericLiteralLike(skipParentheses(node.argumentExpression))) {
1374813748
return isLateBindableAccessExpression(node.expression);
1374913749
}
1375013750
return false;

tests/baselines/reference/enumKeysAsComputedPropertiesWithBracketNotation.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ type TypeMap10 = {
7777
type TypeMap11 = {
7878
[(nested).inner.key]: string;
7979
}
80+
81+
// Parenthesized keys in element access
82+
type TypeMap12 = {
83+
[nested[('inner')]['key']]: string;
84+
}
85+
86+
type TypeMap13 = {
87+
[nested['inner'][('key')]]: string;
88+
}
89+
90+
type TypeMap14 = {
91+
[deep[('a')][('b')].c['d']]: string;
92+
}
8093

8194

8295
//// [enumKeysAsComputedPropertiesWithBracketNotation.js]

tests/baselines/reference/enumKeysAsComputedPropertiesWithBracketNotation.symbols

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,33 @@ type TypeMap11 = {
185185
>key : Symbol(key, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 21, 12))
186186
}
187187

188+
// Parenthesized keys in element access
189+
type TypeMap12 = {
190+
>TypeMap12 : Symbol(TypeMap12, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 75, 1))
191+
192+
[nested[('inner')]['key']]: string;
193+
>[nested[('inner')]['key']] : Symbol([nested[('inner')]['key']], Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 78, 18))
194+
>nested : Symbol(nested, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 20, 5))
195+
>'key' : Symbol(key, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 21, 12))
196+
}
197+
198+
type TypeMap13 = {
199+
>TypeMap13 : Symbol(TypeMap13, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 80, 1))
200+
201+
[nested['inner'][('key')]]: string;
202+
>[nested['inner'][('key')]] : Symbol([nested['inner'][('key')]], Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 82, 18))
203+
>nested : Symbol(nested, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 20, 5))
204+
>'inner' : Symbol(inner, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 20, 16))
205+
}
206+
207+
type TypeMap14 = {
208+
>TypeMap14 : Symbol(TypeMap14, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 84, 1))
209+
210+
[deep[('a')][('b')].c['d']]: string;
211+
>[deep[('a')][('b')].c['d']] : Symbol([deep[('a')][('b')].c['d']], Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 86, 18))
212+
>deep[('a')][('b')].c : Symbol(c, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 48, 12))
213+
>deep : Symbol(deep, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 46, 5))
214+
>c : Symbol(c, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 48, 12))
215+
>'d' : Symbol(d, Decl(enumKeysAsComputedPropertiesWithBracketNotation.ts, 49, 16))
216+
}
217+

tests/baselines/reference/enumKeysAsComputedPropertiesWithBracketNotation.types

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,77 @@ type TypeMap11 = {
320320
> : ^^^^^^^
321321
}
322322

323+
// Parenthesized keys in element access
324+
type TypeMap12 = {
325+
>TypeMap12 : TypeMap12
326+
> : ^^^^^^^^^
327+
328+
[nested[('inner')]['key']]: string;
329+
>[nested[('inner')]['key']] : string
330+
> : ^^^^^^
331+
>nested[('inner')]['key'] : "hello"
332+
> : ^^^^^^^
333+
>nested[('inner')] : { key: "hello"; }
334+
> : ^^^^^^^^^^^^^^^^^
335+
>nested : { inner: { key: "hello"; }; }
336+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
337+
>('inner') : "inner"
338+
> : ^^^^^^^
339+
>'inner' : "inner"
340+
> : ^^^^^^^
341+
>'key' : "key"
342+
> : ^^^^^
343+
}
344+
345+
type TypeMap13 = {
346+
>TypeMap13 : TypeMap13
347+
> : ^^^^^^^^^
348+
349+
[nested['inner'][('key')]]: string;
350+
>[nested['inner'][('key')]] : string
351+
> : ^^^^^^
352+
>nested['inner'][('key')] : "hello"
353+
> : ^^^^^^^
354+
>nested['inner'] : { key: "hello"; }
355+
> : ^^^^^^^^^^^^^^^^^
356+
>nested : { inner: { key: "hello"; }; }
357+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
358+
>'inner' : "inner"
359+
> : ^^^^^^^
360+
>('key') : "key"
361+
> : ^^^^^
362+
>'key' : "key"
363+
> : ^^^^^
364+
}
365+
366+
type TypeMap14 = {
367+
>TypeMap14 : TypeMap14
368+
> : ^^^^^^^^^
369+
370+
[deep[('a')][('b')].c['d']]: string;
371+
>[deep[('a')][('b')].c['d']] : string
372+
> : ^^^^^^
373+
>deep[('a')][('b')].c['d'] : "value"
374+
> : ^^^^^^^
375+
>deep[('a')][('b')].c : { d: "value"; }
376+
> : ^^^^^^^^^^^^^^^
377+
>deep[('a')][('b')] : { c: { d: "value"; }; }
378+
> : ^^^^^^^^^^^^^^^^^^^^^^^
379+
>deep[('a')] : { b: { c: { d: "value"; }; }; }
380+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
381+
>deep : { a: { b: { c: { d: "value"; }; }; }; }
382+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
383+
>('a') : "a"
384+
> : ^^^
385+
>'a' : "a"
386+
> : ^^^
387+
>('b') : "b"
388+
> : ^^^
389+
>'b' : "b"
390+
> : ^^^
391+
>c : { d: "value"; }
392+
> : ^^^^^^^^^^^^^^^
393+
>'d' : "d"
394+
> : ^^^
395+
}
396+

tests/cases/compiler/enumKeysAsComputedPropertiesWithBracketNotation.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,16 @@ type TypeMap10 = {
7676
type TypeMap11 = {
7777
[(nested).inner.key]: string;
7878
}
79+
80+
// Parenthesized keys in element access
81+
type TypeMap12 = {
82+
[nested[('inner')]['key']]: string;
83+
}
84+
85+
type TypeMap13 = {
86+
[nested['inner'][('key')]]: string;
87+
}
88+
89+
type TypeMap14 = {
90+
[deep[('a')][('b')].c['d']]: string;
91+
}

0 commit comments

Comments
 (0)