Repository navigation
fix(compiler): do not leak nested arrow function parameters into the parent scope - #71188
Open
GabeSilvaDev wants to merge 1 commit into
Open
GabeSilvaDev wants to merge 1 commit into
GabeSilvaDev wants to merge 1 commit into
Conversation
…parent scope When a template arrow function contained another arrow function, the parameters of the inner one were treated as parameters of the outer one too. A later read with the same name outside the inner function was emitted as a local variable instead of a component property, which threw a ReferenceError at runtime.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
With a component that has a
yfield, this template throwsReferenceError: y is not defined:{{ ((x) => twice((y) => y * 2) + y)(1) }}The last
yis outside the inner arrow, so it should read the component'sy, and the type checker treats it that way. The generated code isx => ctx.twice(y => y * 2) + yinstead. A sibling arrow is affected the same way: intwice((y) => y) + twice((z) => y)the second callback becomesz => y.updateParameterReferencesadds the parameters of every nested arrow function to the set it uses for the root, so any read of those names anywhere in the root becomes a local variable. This came in with 7b5625e, which replaced the per-scopelocalSymbolswith that single pass.What is the new behavior?
updateParameterReferencesnow only uses the root's own parameters. Nested arrow functions go throughconvertAst(and this function) before their parent, so their own parameter reads are already resolved by the time the parent is processed. The example now renders 104 and the sibling case readsctx.y.I added a compliance case next to the other arrow function ones and an acceptance test under "arrow functions" in
integration_spec.ts. Both fail without the change.Does this PR introduce a breaking change?
Other information
A parameter named
ctxis still shadowed by the generated context parameter (run((ctx) => ctx + y)). That needs a different fix (unique generated names), so I left it out of this PR.