Skip to content

fix(compiler): detect component context in compound track expressions#172

Open
ashley-hunter wants to merge 1 commit intovoidzero-dev:mainfrom
ashley-hunter:fix/for-tracking
Open

fix(compiler): detect component context in compound track expressions#172
ashley-hunter wants to merge 1 commit intovoidzero-dev:mainfrom
ashley-hunter:fix/for-tracking

Conversation

@ashley-hunter
Copy link
Contributor

Track expressions using binary operators (??. +), ternary, or unary operators that referenced component members were incorrectly compiled as arrow functions, causing this to be undefined at runtime.

The root cause was that expression_contains_context — a function not present in Angular's implementation — failed to traverse compound IR expression types like Binary and Ternary. Instead of patching every missing variant, this commit aligns with Angular's approach: detect ContextExpr during the transformExpressionsInExpression callback that already visits all expression types, setting usesComponentInstance in the same pass that replaces Context with TrackContext.

This removes ~180 lines of redundant detection code (expression_contains_context, ast_contains_implicit_receiver) that duplicated work already done by resolve_names and the transform visitor.

Output verified against the official Angular compiler for all test cases.

Track expressions using binary operators (??. +), ternary, or unary
operators that referenced component members were incorrectly compiled
as arrow functions, causing `this` to be undefined at runtime.

The root cause was that `expression_contains_context` — a function not
present in Angular's implementation — failed to traverse compound IR
expression types like Binary and Ternary. Instead of patching every
missing variant, this commit aligns with Angular's approach: detect
ContextExpr during the transformExpressionsInExpression callback that
already visits all expression types, setting usesComponentInstance in
the same pass that replaces Context with TrackContext.

This removes ~180 lines of redundant detection code
(expression_contains_context, ast_contains_implicit_receiver) that
duplicated work already done by resolve_names (phase 31) and the
transform visitor.

Output verified against the official Angular compiler (NgtscProgram)
for all test cases.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant