Skip to content

Fix crash on parsing webpack dependency (neo-async) by adding nil check #1340

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ func GetAssignmentDeclarationKind(bin *BinaryExpression) JSDeclarationKind {
if IsInJSFile(bin.Left) && bin.Left.Expression().Kind == KindThisKeyword {
return JSDeclarationKindThisProperty
}
if bin.Left.Kind == KindPropertyAccessExpression && IsEntityNameExpressionEx(bin.Left.Expression(), IsInJSFile(bin.Left)) && IsIdentifier(bin.Left.Name()) ||
if bin.Left.Kind == KindPropertyAccessExpression && IsEntityNameExpressionEx(bin.Left.Expression(), IsInJSFile(bin.Left)) && bin.Left.Name() != nil && IsIdentifier(bin.Left.Name()) ||
bin.Left.Kind == KindElementAccessExpression && IsEntityNameExpressionEx(bin.Left.Expression(), IsInJSFile(bin.Left)) {
return JSDeclarationKindProperty
}
Expand Down
46 changes: 46 additions & 0 deletions testdata/baselines/reference/compiler/crash-on-neo-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//// [tests/cases/compiler/crash-on-neo-async.ts] ////

//// [crash-on-neo-async.js]
// This test reproduces a crash that occurs when parsing files like webpack/node_modules/neo-async/async.js
// The crash happens in GetAssignmentDeclarationKind when checking IsIdentifier(bin.Left.Name())
// where bin.Left.Name() returns nil for ElementAccessExpression
// Pattern that causes the crash - element access assignment
var obj = {};
var prop = 'test';
// This assignment with element access should not crash
// It previously crashed because ElementAccessExpression.Name() returns nil
// and IsIdentifier was called on that nil value
obj[prop] = function () {
return 42;
};
// Property access assignment should work fine (has a valid Name())
obj.prop2 = function () {
return 43;
};
// Nested element access assignment
obj['nested'][prop] = function () {
return 44;
};


//// [crash-on-neo-async.js]
// This test reproduces a crash that occurs when parsing files like webpack/node_modules/neo-async/async.js
// The crash happens in GetAssignmentDeclarationKind when checking IsIdentifier(bin.Left.Name())
// where bin.Left.Name() returns nil for ElementAccessExpression
// Pattern that causes the crash - element access assignment
var obj = {};
var prop = 'test';
// This assignment with element access should not crash
// It previously crashed because ElementAccessExpression.Name() returns nil
// and IsIdentifier was called on that nil value
obj[prop] = function () {
return 42;
};
// Property access assignment should work fine (has a valid Name())
obj.prop2 = function () {
return 43;
};
// Nested element access assignment
obj['nested'][prop] = function () {
return 44;
};
38 changes: 38 additions & 0 deletions testdata/baselines/reference/compiler/crash-on-neo-async.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/compiler/crash-on-neo-async.ts] ////

=== crash-on-neo-async.js ===
// This test reproduces a crash that occurs when parsing files like webpack/node_modules/neo-async/async.js
// The crash happens in GetAssignmentDeclarationKind when checking IsIdentifier(bin.Left.Name())
// where bin.Left.Name() returns nil for ElementAccessExpression
// Pattern that causes the crash - element access assignment
var obj = {};
>obj : Symbol(obj, Decl(crash-on-neo-async.js, 4, 3))

var prop = 'test';
>prop : Symbol(prop, Decl(crash-on-neo-async.js, 5, 3))

// This assignment with element access should not crash
// It previously crashed because ElementAccessExpression.Name() returns nil
// and IsIdentifier was called on that nil value
obj[prop] = function () {
>obj : Symbol(obj, Decl(crash-on-neo-async.js, 4, 3))
>prop : Symbol(prop, Decl(crash-on-neo-async.js, 5, 3))

return 42;
};
// Property access assignment should work fine (has a valid Name())
obj.prop2 = function () {
>obj.prop2 : Symbol(prop2, Decl(crash-on-neo-async.js, 11, 2))
>obj : Symbol(obj, Decl(crash-on-neo-async.js, 4, 3))
>prop2 : Symbol(prop2, Decl(crash-on-neo-async.js, 11, 2))

return 43;
};
// Nested element access assignment
obj['nested'][prop] = function () {
>obj : Symbol(obj, Decl(crash-on-neo-async.js, 4, 3))
>prop : Symbol(prop, Decl(crash-on-neo-async.js, 5, 3))

return 44;
};

56 changes: 56 additions & 0 deletions testdata/baselines/reference/compiler/crash-on-neo-async.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//// [tests/cases/compiler/crash-on-neo-async.ts] ////

=== crash-on-neo-async.js ===
// This test reproduces a crash that occurs when parsing files like webpack/node_modules/neo-async/async.js
// The crash happens in GetAssignmentDeclarationKind when checking IsIdentifier(bin.Left.Name())
// where bin.Left.Name() returns nil for ElementAccessExpression
// Pattern that causes the crash - element access assignment
var obj = {};
>obj : { prop2: () => number; }
>{} : { prop2: () => number; }

var prop = 'test';
>prop : string
>'test' : "test"

// This assignment with element access should not crash
// It previously crashed because ElementAccessExpression.Name() returns nil
// and IsIdentifier was called on that nil value
obj[prop] = function () {
>obj[prop] = function () { return 42;} : () => number
>obj[prop] : any
>obj : { prop2: () => number; }
>prop : string
>function () { return 42;} : () => number

return 42;
>42 : 42

};
// Property access assignment should work fine (has a valid Name())
obj.prop2 = function () {
>obj.prop2 = function () { return 43;} : () => number
>obj.prop2 : () => number
>obj : { prop2: () => number; }
>prop2 : () => number
>function () { return 43;} : () => number

return 43;
>43 : 43

};
// Nested element access assignment
obj['nested'][prop] = function () {
>obj['nested'][prop] = function () { return 44;} : () => number
>obj['nested'][prop] : any
>obj['nested'] : any
>obj : { prop2: () => number; }
>'nested' : "nested"
>prop : string
>function () { return 44;} : () => number

return 44;
>44 : 44

};

22 changes: 22 additions & 0 deletions testdata/tests/cases/compiler/crash-on-neo-async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @checkJs: true
// @filename: crash-on-neo-async.js
// This test reproduces a crash that occurs when parsing files like webpack/node_modules/neo-async/async.js
// The crash happens in GetAssignmentDeclarationKind when checking IsIdentifier(bin.Left.Name())
// where bin.Left.Name() returns nil for ElementAccessExpression
// Pattern that causes the crash - element access assignment
var obj = {};
var prop = 'test';
// This assignment with element access should not crash
// It previously crashed because ElementAccessExpression.Name() returns nil
// and IsIdentifier was called on that nil value
obj[prop] = function () {
return 42;
};
// Property access assignment should work fine (has a valid Name())
obj.prop2 = function () {
return 43;
};
// Nested element access assignment
obj['nested'][prop] = function () {
return 44;
};