Skip to content

Fix panic when compiling webpack by handling ElementAccessExpression in Node.Text() #892

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,11 @@ func GetElementOrPropertyAccessName(node *Node) string {
if name == nil {
return ""
}
// If we get back the original node and it's an ElementAccessExpression,
// it means it had a non-literal argument, so return empty string
if name == node && node.Kind == KindElementAccessExpression {
return ""
}
return name.Text()
}

Expand Down
26 changes: 26 additions & 0 deletions testdata/tests/cases/compiler/elementAccessExpressionInJS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-check
// @checkJs: true

// Create an object with properties
const obj = { staticKey: "value", 0: "numeric" };

// Use ElementAccessExpression with string literal (should work)
const value1 = obj["staticKey"];

// Use ElementAccessExpression with numeric literal (should work)
const value2 = obj[0];

// Use ElementAccessExpression with variable (would trigger the panic before fix)
const dynamicKey = "dynamicKey";
const value3 = obj[dynamicKey];

// Nested ElementAccessExpression (common in webpack code)
const nestedObj = { inner: { deep: "value" } };
const innerKey = "inner";
const deepKey = "deep";
const nestedValue = nestedObj[innerKey][deepKey];

// Create an array and access with ElementAccessExpression
const arr = ["first", "second", "third"];
const idx = 1;
const arrValue = arr[idx];