Fix crash in isolatedDeclarations on this.x = … assignments - #64471
Mateusz Burzyński (Andarist) wants to merge 2 commits into
Conversation
…perty assignments
…assignments isBoundExpando resolved the root of any `a.b = ...` assignment as though it were an identifier, so `this.x = foo()` (and `super.x = ...`, `f().x = ...`) reached referenceResolver with a non-Identifier and panicked in Node.Text. Use the same expando-host definition as transformExpandoAssignment: the leftmost access expression must be an identifier. Fixes microsoft#64458
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
| if !(ast.IsExpandoPropertyDeclaration(node) && ast.IsPropertyAccessExpression(node.AsBinaryExpression().Left)) { | ||
| return false | ||
| } | ||
| ref := s.resolver.GetReferencedValueDeclarationUnsafe(ast.GetLeftmostExpression(node.AsBinaryExpression().Left, true)) |
There was a problem hiding this comment.
With isolatedDeclarations on, if a type can't be inferred, the Go declaration emitter reports an error. Before reporting, it checks if the node is inside an expando assignment like f.x = …, so it doesn't report the same problem twice. This check (isBoundExpando) treated every a.b = … assignment as rooted at a name and passed that root to name lookup.
But with this.x = foo() the root is the this keyword, not a name, so the lookup's Node.Text() would just panic.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused guard matches existing transformation behavior and includes regression coverage for the reported crash and related assignment roots.
Review effort: Balanced
Findings: None
What changed in this PR
Prevents declaration emit crashes by rejecting non-identifier-rooted assignments as expando bindings.
Changes:
- Aligns expando tracking with declaration transformation logic.
- Adds TypeScript and JavaScript regression coverage.
- Records expected diagnostics, symbols, types, and declaration output.
| File | Description |
|---|---|
tsc/internal/transformers/declarations/tracker.go |
Guards expando resolution by requiring an identifier root. |
tsc/testdata/tests/cases/compiler/isolatedDeclarationsNonIdentifierAssignmentInference.ts |
Tests this, super, and call-expression assignment roots. |
tsc/testdata/tests/cases/compiler/isolatedDeclarationsJsThisPropertyAssignmentInference.ts |
Reproduces the JavaScript this.x crash. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsNonIdentifierAssignmentInference(isolateddeclarations=true).types |
Captures isolated-declaration types. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsNonIdentifierAssignmentInference(isolateddeclarations=true).symbols |
Captures isolated-declaration symbols. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsNonIdentifierAssignmentInference(isolateddeclarations=true).errors.txt |
Captures expected inference diagnostics. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsNonIdentifierAssignmentInference(isolateddeclarations=false).types |
Captures standard-mode types. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsNonIdentifierAssignmentInference(isolateddeclarations=false).symbols |
Captures standard-mode symbols. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsNonIdentifierAssignmentInference(isolateddeclarations=false).js |
Captures declaration output without isolation. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsJsThisPropertyAssignmentInference.types |
Captures JavaScript inference types. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsJsThisPropertyAssignmentInference.symbols |
Captures JavaScript symbols. |
tsc/testdata/baselines/reference/compiler/isolatedDeclarationsJsThisPropertyAssignmentInference.errors.txt |
Captures JavaScript diagnostics. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
fixes a crash reported here: #64458 (comment) (fallout from microsoft/typescript-go#4513)