-
Notifications
You must be signed in to change notification settings - Fork 13.2k
fix(checker): Allow element access expressions in computed property names if argument is literal #62968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kairosci
wants to merge
7
commits into
microsoft:main
Choose a base branch
from
kairosci:fix/enum-computed-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(checker): Allow element access expressions in computed property names if argument is literal #62968
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e813bb0
fix(checker): Allow element access expressions in computed property n…
kairosci 74055e7
Merge branch 'main' into fix/enum-computed-keys
kairosci 69bcbc4
Merge branch 'main' into fix/enum-computed-keys
kairosci 91424d8
fix(checker): Allow element access expressions in computed property n…
kairosci 5e9b706
Merge branch 'main' into fix/enum-computed-keys
kairosci d1fff11
Merge branch 'main' into fix/enum-computed-keys
kairosci 598f80f
Merge branch 'main' into fix/enum-computed-keys
kairosci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //// [tests/cases/compiler/enumKeysInTypeLiteral.ts] //// | ||
|
|
||
| //// [enumKeysInTypeLiteral.ts] | ||
| enum Type { | ||
| Foo = 'foo', | ||
| '3x14' = '3x14' | ||
| } | ||
|
|
||
| type TypeMap = { | ||
| [Type.Foo]: 1; | ||
| [Type['3x14']]: 2; | ||
| } | ||
|
|
||
| const t: TypeMap = { | ||
| 'foo': 1, | ||
| '3x14': 2 | ||
| }; | ||
|
|
||
| enum Numeric { | ||
| Negative = -1, | ||
| Zero = 0 | ||
| } | ||
|
|
||
| type NumericMap = { | ||
| // Valid: Accessing enum member via string literal for the name | ||
| [Numeric['Negative']]: number; | ||
| [Numeric['Zero']]: number; | ||
| // Valid: Parenthesized access | ||
| [Numeric[('Negative')]]: number; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| //// [enumKeysInTypeLiteral.js] | ||
| var Type; | ||
| (function (Type) { | ||
| Type["Foo"] = "foo"; | ||
| Type["3x14"] = "3x14"; | ||
| })(Type || (Type = {})); | ||
| var t = { | ||
| 'foo': 1, | ||
| '3x14': 2 | ||
| }; | ||
| var Numeric; | ||
| (function (Numeric) { | ||
| Numeric[Numeric["Negative"] = -1] = "Negative"; | ||
| Numeric[Numeric["Zero"] = 0] = "Zero"; | ||
| })(Numeric || (Numeric = {})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| //// [tests/cases/compiler/enumKeysInTypeLiteral.ts] //// | ||
|
|
||
| === enumKeysInTypeLiteral.ts === | ||
| enum Type { | ||
| >Type : Symbol(Type, Decl(enumKeysInTypeLiteral.ts, 0, 0)) | ||
|
|
||
| Foo = 'foo', | ||
| >Foo : Symbol(Type.Foo, Decl(enumKeysInTypeLiteral.ts, 0, 11)) | ||
|
|
||
| '3x14' = '3x14' | ||
| >'3x14' : Symbol(Type['3x14'], Decl(enumKeysInTypeLiteral.ts, 1, 14)) | ||
| } | ||
|
|
||
| type TypeMap = { | ||
| >TypeMap : Symbol(TypeMap, Decl(enumKeysInTypeLiteral.ts, 3, 1)) | ||
|
|
||
| [Type.Foo]: 1; | ||
| >[Type.Foo] : Symbol([Type.Foo], Decl(enumKeysInTypeLiteral.ts, 5, 16)) | ||
| >Type.Foo : Symbol(Type.Foo, Decl(enumKeysInTypeLiteral.ts, 0, 11)) | ||
| >Type : Symbol(Type, Decl(enumKeysInTypeLiteral.ts, 0, 0)) | ||
| >Foo : Symbol(Type.Foo, Decl(enumKeysInTypeLiteral.ts, 0, 11)) | ||
|
|
||
| [Type['3x14']]: 2; | ||
| >[Type['3x14']] : Symbol([Type['3x14']], Decl(enumKeysInTypeLiteral.ts, 6, 16)) | ||
| >Type : Symbol(Type, Decl(enumKeysInTypeLiteral.ts, 0, 0)) | ||
| >'3x14' : Symbol(Type['3x14'], Decl(enumKeysInTypeLiteral.ts, 1, 14)) | ||
| } | ||
|
|
||
| const t: TypeMap = { | ||
| >t : Symbol(t, Decl(enumKeysInTypeLiteral.ts, 10, 5)) | ||
| >TypeMap : Symbol(TypeMap, Decl(enumKeysInTypeLiteral.ts, 3, 1)) | ||
|
|
||
| 'foo': 1, | ||
| >'foo' : Symbol('foo', Decl(enumKeysInTypeLiteral.ts, 10, 20)) | ||
|
|
||
| '3x14': 2 | ||
| >'3x14' : Symbol('3x14', Decl(enumKeysInTypeLiteral.ts, 11, 13)) | ||
|
|
||
| }; | ||
|
|
||
| enum Numeric { | ||
| >Numeric : Symbol(Numeric, Decl(enumKeysInTypeLiteral.ts, 13, 2)) | ||
|
|
||
| Negative = -1, | ||
| >Negative : Symbol(Numeric.Negative, Decl(enumKeysInTypeLiteral.ts, 15, 14)) | ||
|
|
||
| Zero = 0 | ||
| >Zero : Symbol(Numeric.Zero, Decl(enumKeysInTypeLiteral.ts, 16, 18)) | ||
| } | ||
|
|
||
| type NumericMap = { | ||
| >NumericMap : Symbol(NumericMap, Decl(enumKeysInTypeLiteral.ts, 18, 1)) | ||
|
|
||
| // Valid: Accessing enum member via string literal for the name | ||
| [Numeric['Negative']]: number; | ||
| >[Numeric['Negative']] : Symbol([Numeric['Negative']], Decl(enumKeysInTypeLiteral.ts, 20, 19), Decl(enumKeysInTypeLiteral.ts, 23, 30)) | ||
| >Numeric : Symbol(Numeric, Decl(enumKeysInTypeLiteral.ts, 13, 2)) | ||
| >'Negative' : Symbol(Numeric.Negative, Decl(enumKeysInTypeLiteral.ts, 15, 14)) | ||
|
|
||
| [Numeric['Zero']]: number; | ||
| >[Numeric['Zero']] : Symbol([Numeric['Zero']], Decl(enumKeysInTypeLiteral.ts, 22, 34)) | ||
| >Numeric : Symbol(Numeric, Decl(enumKeysInTypeLiteral.ts, 13, 2)) | ||
| >'Zero' : Symbol(Numeric.Zero, Decl(enumKeysInTypeLiteral.ts, 16, 18)) | ||
|
|
||
| // Valid: Parenthesized access | ||
| [Numeric[('Negative')]]: number; | ||
| >[Numeric[('Negative')]] : Symbol([Numeric['Negative']], Decl(enumKeysInTypeLiteral.ts, 20, 19), Decl(enumKeysInTypeLiteral.ts, 23, 30)) | ||
| >Numeric : Symbol(Numeric, Decl(enumKeysInTypeLiteral.ts, 13, 2)) | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| //// [tests/cases/compiler/enumKeysInTypeLiteral.ts] //// | ||
|
|
||
| === enumKeysInTypeLiteral.ts === | ||
| enum Type { | ||
| >Type : Type | ||
| > : ^^^^ | ||
|
|
||
| Foo = 'foo', | ||
| >Foo : Type.Foo | ||
| > : ^^^^^^^^ | ||
| >'foo' : "foo" | ||
| > : ^^^^^ | ||
|
|
||
| '3x14' = '3x14' | ||
| >'3x14' : (typeof Type)["3x14"] | ||
| > : ^^^^^^^^^^^^^^^^^^^^^ | ||
| >'3x14' : "3x14" | ||
| > : ^^^^^^ | ||
| } | ||
|
|
||
| type TypeMap = { | ||
| >TypeMap : TypeMap | ||
| > : ^^^^^^^ | ||
|
|
||
| [Type.Foo]: 1; | ||
| >[Type.Foo] : 1 | ||
| > : ^ | ||
| >Type.Foo : Type.Foo | ||
| > : ^^^^^^^^ | ||
| >Type : typeof Type | ||
| > : ^^^^^^^^^^^ | ||
| >Foo : Type.Foo | ||
| > : ^^^^^^^^ | ||
|
|
||
| [Type['3x14']]: 2; | ||
| >[Type['3x14']] : 2 | ||
| > : ^ | ||
| >Type['3x14'] : (typeof Type)["3x14"] | ||
| > : ^^^^^^^^^^^^^^^^^^^^^ | ||
| >Type : typeof Type | ||
| > : ^^^^^^^^^^^ | ||
| >'3x14' : "3x14" | ||
| > : ^^^^^^ | ||
| } | ||
|
|
||
| const t: TypeMap = { | ||
| >t : TypeMap | ||
| > : ^^^^^^^ | ||
| >{ 'foo': 1, '3x14': 2} : { foo: 1; '3x14': 2; } | ||
| > : ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| 'foo': 1, | ||
| >'foo' : 1 | ||
| > : ^ | ||
| >1 : 1 | ||
| > : ^ | ||
|
|
||
| '3x14': 2 | ||
| >'3x14' : 2 | ||
| > : ^ | ||
| >2 : 2 | ||
| > : ^ | ||
|
|
||
| }; | ||
|
|
||
| enum Numeric { | ||
| >Numeric : Numeric | ||
| > : ^^^^^^^ | ||
|
|
||
| Negative = -1, | ||
| >Negative : Numeric.Negative | ||
| > : ^^^^^^^^^^^^^^^^ | ||
| >-1 : -1 | ||
| > : ^^ | ||
| >1 : 1 | ||
| > : ^ | ||
|
|
||
| Zero = 0 | ||
| >Zero : Numeric.Zero | ||
| > : ^^^^^^^^^^^^ | ||
| >0 : 0 | ||
| > : ^ | ||
| } | ||
|
|
||
| type NumericMap = { | ||
| >NumericMap : NumericMap | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| // Valid: Accessing enum member via string literal for the name | ||
| [Numeric['Negative']]: number; | ||
| >[Numeric['Negative']] : number | ||
| > : ^^^^^^ | ||
| >Numeric['Negative'] : Numeric.Negative | ||
| > : ^^^^^^^^^^^^^^^^ | ||
| >Numeric : typeof Numeric | ||
| > : ^^^^^^^^^^^^^^ | ||
| >'Negative' : "Negative" | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| [Numeric['Zero']]: number; | ||
| >[Numeric['Zero']] : number | ||
| > : ^^^^^^ | ||
| >Numeric['Zero'] : Numeric.Zero | ||
| > : ^^^^^^^^^^^^ | ||
| >Numeric : typeof Numeric | ||
| > : ^^^^^^^^^^^^^^ | ||
| >'Zero' : "Zero" | ||
| > : ^^^^^^ | ||
|
|
||
| // Valid: Parenthesized access | ||
| [Numeric[('Negative')]]: number; | ||
| >[Numeric[('Negative')]] : number | ||
| > : ^^^^^^ | ||
| >Numeric[('Negative')] : Numeric.Negative | ||
| > : ^^^^^^^^^^^^^^^^ | ||
| >Numeric : typeof Numeric | ||
| > : ^^^^^^^^^^^^^^ | ||
| >('Negative') : "Negative" | ||
| > : ^^^^^^^^^^ | ||
| >'Negative' : "Negative" | ||
| > : ^^^^^^^^^^ | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
|
|
||
| enum Type { | ||
| Foo = 'foo', | ||
| '3x14' = '3x14' | ||
| } | ||
|
|
||
| type TypeMap = { | ||
| [Type.Foo]: 1; | ||
| [Type['3x14']]: 2; | ||
| } | ||
|
|
||
| const t: TypeMap = { | ||
| 'foo': 1, | ||
| '3x14': 2 | ||
| }; | ||
|
|
||
| enum Numeric { | ||
| Negative = -1, | ||
| Zero = 0 | ||
| } | ||
|
|
||
| type NumericMap = { | ||
| // Valid: Accessing enum member via string literal for the name | ||
| [Numeric['Negative']]: number; | ||
| [Numeric['Zero']]: number; | ||
| // Valid: Parenthesized access | ||
| [Numeric[('Negative')]]: number; | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for handling
ElementAccessExpressionis incorrect. Whennodeis anElementAccessExpression(e.g.,Type['key']), this code passes onlynode.argumentExpression(which would be the literal'key') toisLateBindableExpression.However,
isLateBindableExpressionexpects to validate the full expression chain. When passed a literal like'key', it skips the while loop (since a literal is not anElementAccessExpression) and callsisEntityNameExpression('key'), which returnsfalsebecause a string literal is not an entity name expression.The correct approach is to pass the entire
nodetoisLateBindableExpression, not justnode.argumentExpression. This would allow the function to properly validate the element access chain and then check that the base is an entity name expression.