Repository navigation
fix: Fix MemberList panic on merged enum element access #64746
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
Andre Fajar N (andre-fajar-n)
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
andre-fajar-n:fix/memberlist-moduleblock-panic
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.
+211
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
146a7bc
fix: Fix MemberList panic on enum element access with merged namespac…
andre-fajar-n c227631
fix: Resolve aliases before EnumMember check in enum element access
andre-fajar-n 7d147ec
Merge branch 'main' into fix/memberlist-moduleblock-panic
andre-fajar-n 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24634,10 +24634,18 @@ func (c *Checker) evaluateEntity(expr *ast.Node, location *ast.Node) evaluator.R | |
| name := expr.AsElementAccessExpression().ArgumentExpression.Text() | ||
| member := rootSymbol.Exports()[name] | ||
| if member != nil { | ||
| if location != nil { | ||
| return c.evaluateEnumMember(expr, member, location) | ||
| // Follow aliases like resolveEntityName (property-access path), e.g. export import C = F.A. | ||
| for member.Flags()&ast.SymbolFlagsValue == 0 && member.Flags()&ast.SymbolFlagsAlias != 0 { | ||
| member = c.resolveAlias(member) | ||
| } | ||
| // Merged namespace+enum symbols can export non-enum members (e.g. a class). | ||
| // Only treat EnumMember exports as constant enum values — mirror the identifier path. | ||
| if member.Flags()&ast.SymbolFlagsEnumMember != 0 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And extract this to a helper we can share with the |
||
| if location != nil { | ||
| return c.evaluateEnumMember(expr, member, location) | ||
| } | ||
| return c.getEnumMemberValue(member.ValueDeclaration()) | ||
| } | ||
| return c.getEnumMemberValue(member.ValueDeclaration()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
28 changes: 28 additions & 0 deletions
28
...tdata/baselines/reference/compiler/enumInitializerNamespaceExportElementAccess.errors.txt
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,28 @@ | ||
| enumInitializerNamespaceExportElementAccess.ts(8,9): error TS18033: Type 'typeof C' is not assignable to type 'number' as required for computed enum member values. | ||
|
|
||
|
|
||
| ==== enumInitializerNamespaceExportElementAccess.ts (1 errors) ==== | ||
| // Namespace export merged with enum must not be treated as an enum member via E["C"]. | ||
| // Previously panicked: Unhandled case in Node.MemberList: KindModuleBlock | ||
|
|
||
| namespace E { | ||
| export class C {} | ||
| } | ||
| enum E { | ||
| D = E["C"], | ||
| ~~~~~~ | ||
| !!! error TS18033: Type 'typeof C' is not assignable to type 'number' as required for computed enum member values. | ||
| } | ||
|
|
||
| // Alias exports that resolve to enum members must still constant-evaluate (like E.C). | ||
| enum F { | ||
| A = 1, | ||
| } | ||
| namespace E2 { | ||
| export import C = F.A; | ||
| } | ||
| enum E2 { | ||
| D = E2["C"], | ||
| Next, | ||
| } | ||
|
|
52 changes: 52 additions & 0 deletions
52
tsc/testdata/baselines/reference/compiler/enumInitializerNamespaceExportElementAccess.js
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,52 @@ | ||
| //// [tests/cases/compiler/enumInitializerNamespaceExportElementAccess.ts] //// | ||
|
|
||
| //// [enumInitializerNamespaceExportElementAccess.ts] | ||
| // Namespace export merged with enum must not be treated as an enum member via E["C"]. | ||
| // Previously panicked: Unhandled case in Node.MemberList: KindModuleBlock | ||
|
|
||
| namespace E { | ||
| export class C {} | ||
| } | ||
| enum E { | ||
| D = E["C"], | ||
| } | ||
|
|
||
| // Alias exports that resolve to enum members must still constant-evaluate (like E.C). | ||
| enum F { | ||
| A = 1, | ||
| } | ||
| namespace E2 { | ||
| export import C = F.A; | ||
| } | ||
| enum E2 { | ||
| D = E2["C"], | ||
| Next, | ||
| } | ||
|
|
||
|
|
||
| //// [enumInitializerNamespaceExportElementAccess.js] | ||
| "use strict"; | ||
| // Namespace export merged with enum must not be treated as an enum member via E["C"]. | ||
| // Previously panicked: Unhandled case in Node.MemberList: KindModuleBlock | ||
| var E; | ||
| (function (E) { | ||
| class C { | ||
| } | ||
| E.C = C; | ||
| })(E || (E = {})); | ||
| (function (E) { | ||
| E[E["D"] = E["C"]] = "D"; | ||
| })(E || (E = {})); | ||
| // Alias exports that resolve to enum members must still constant-evaluate (like E.C). | ||
| var F; | ||
| (function (F) { | ||
| F[F["A"] = 1] = "A"; | ||
| })(F || (F = {})); | ||
| var E2; | ||
| (function (E2) { | ||
| E2.C = F.A; | ||
| })(E2 || (E2 = {})); | ||
| (function (E2) { | ||
| E2[E2["D"] = 1] = "D"; | ||
| E2[E2["Next"] = 2] = "Next"; | ||
| })(E2 || (E2 = {})); |
48 changes: 48 additions & 0 deletions
48
...testdata/baselines/reference/compiler/enumInitializerNamespaceExportElementAccess.symbols
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/enumInitializerNamespaceExportElementAccess.ts] //// | ||
|
|
||
| === enumInitializerNamespaceExportElementAccess.ts === | ||
| // Namespace export merged with enum must not be treated as an enum member via E["C"]. | ||
| // Previously panicked: Unhandled case in Node.MemberList: KindModuleBlock | ||
|
|
||
| namespace E { | ||
| >E : Symbol(E, Decl(enumInitializerNamespaceExportElementAccess.ts, 0, 0), Decl(enumInitializerNamespaceExportElementAccess.ts, 5, 1)) | ||
|
|
||
| export class C {} | ||
| >C : Symbol(C, Decl(enumInitializerNamespaceExportElementAccess.ts, 3, 13)) | ||
| } | ||
| enum E { | ||
| >E : Symbol(E, Decl(enumInitializerNamespaceExportElementAccess.ts, 0, 0), Decl(enumInitializerNamespaceExportElementAccess.ts, 5, 1)) | ||
|
|
||
| D = E["C"], | ||
| >D : Symbol(E.D, Decl(enumInitializerNamespaceExportElementAccess.ts, 6, 8)) | ||
| >E : Symbol(E, Decl(enumInitializerNamespaceExportElementAccess.ts, 0, 0), Decl(enumInitializerNamespaceExportElementAccess.ts, 5, 1)) | ||
| >"C" : Symbol(E.C, Decl(enumInitializerNamespaceExportElementAccess.ts, 3, 13)) | ||
| } | ||
|
|
||
| // Alias exports that resolve to enum members must still constant-evaluate (like E.C). | ||
| enum F { | ||
| >F : Symbol(F, Decl(enumInitializerNamespaceExportElementAccess.ts, 8, 1)) | ||
|
|
||
| A = 1, | ||
| >A : Symbol(F.A, Decl(enumInitializerNamespaceExportElementAccess.ts, 11, 8)) | ||
| } | ||
| namespace E2 { | ||
| >E2 : Symbol(E2, Decl(enumInitializerNamespaceExportElementAccess.ts, 13, 1), Decl(enumInitializerNamespaceExportElementAccess.ts, 16, 1)) | ||
|
|
||
| export import C = F.A; | ||
| >C : Symbol(C, Decl(enumInitializerNamespaceExportElementAccess.ts, 14, 14)) | ||
| >F : Symbol(F, Decl(enumInitializerNamespaceExportElementAccess.ts, 8, 1)) | ||
| >A : Symbol(C, Decl(enumInitializerNamespaceExportElementAccess.ts, 11, 8)) | ||
| } | ||
| enum E2 { | ||
| >E2 : Symbol(E2, Decl(enumInitializerNamespaceExportElementAccess.ts, 13, 1), Decl(enumInitializerNamespaceExportElementAccess.ts, 16, 1)) | ||
|
|
||
| D = E2["C"], | ||
| >D : Symbol(E2.D, Decl(enumInitializerNamespaceExportElementAccess.ts, 17, 9)) | ||
| >E2 : Symbol(E2, Decl(enumInitializerNamespaceExportElementAccess.ts, 13, 1), Decl(enumInitializerNamespaceExportElementAccess.ts, 16, 1)) | ||
| >"C" : Symbol(E2.C, Decl(enumInitializerNamespaceExportElementAccess.ts, 14, 14)) | ||
|
|
||
| Next, | ||
| >Next : Symbol(E2.Next, Decl(enumInitializerNamespaceExportElementAccess.ts, 18, 16)) | ||
| } | ||
|
|
51 changes: 51 additions & 0 deletions
51
tsc/testdata/baselines/reference/compiler/enumInitializerNamespaceExportElementAccess.types
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,51 @@ | ||
| //// [tests/cases/compiler/enumInitializerNamespaceExportElementAccess.ts] //// | ||
|
|
||
| === enumInitializerNamespaceExportElementAccess.ts === | ||
| // Namespace export merged with enum must not be treated as an enum member via E["C"]. | ||
| // Previously panicked: Unhandled case in Node.MemberList: KindModuleBlock | ||
|
|
||
| namespace E { | ||
| >E : typeof E | ||
|
|
||
| export class C {} | ||
| >C : C | ||
| } | ||
| enum E { | ||
| >E : E | ||
|
|
||
| D = E["C"], | ||
| >D : E.D | ||
| >E["C"] : typeof E.C | ||
| >E : typeof E | ||
| >"C" : "C" | ||
| } | ||
|
|
||
| // Alias exports that resolve to enum members must still constant-evaluate (like E.C). | ||
| enum F { | ||
| >F : F | ||
|
|
||
| A = 1, | ||
| >A : F.A | ||
| >1 : 1 | ||
| } | ||
| namespace E2 { | ||
| >E2 : typeof E2 | ||
|
|
||
| export import C = F.A; | ||
| >C : F.A | ||
| >F : F | ||
| >A : F.A | ||
| } | ||
| enum E2 { | ||
| >E2 : E2 | ||
|
|
||
| D = E2["C"], | ||
| >D : E2.D | ||
| >E2["C"] : F | ||
| >E2 : typeof E2 | ||
| >"C" : "C" | ||
|
|
||
| Next, | ||
| >Next : E2.Next | ||
| } | ||
|
|
21 changes: 21 additions & 0 deletions
21
tsc/testdata/tests/cases/compiler/enumInitializerNamespaceExportElementAccess.ts
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,21 @@ | ||
| // Namespace export merged with enum must not be treated as an enum member via E["C"]. | ||
| // Previously panicked: Unhandled case in Node.MemberList: KindModuleBlock | ||
|
|
||
| namespace E { | ||
| export class C {} | ||
| } | ||
| enum E { | ||
| D = E["C"], | ||
| } | ||
|
|
||
| // Alias exports that resolve to enum members must still constant-evaluate (like E.C). | ||
| enum F { | ||
| A = 1, | ||
| } | ||
| namespace E2 { | ||
| export import C = F.A; | ||
| } | ||
| enum E2 { | ||
| D = E2["C"], | ||
| Next, | ||
| } |
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.
Extract this to a helper we can share with
resolveEntityName, so the comment is less needed. (resolveAliasRecursively?) It's interesting that these two places are the only ones we actually do a fully recursive alias resolve.