Repository navigation
Conversation
…ction - A this in an arrow function escapes from the first enclosing function that isn't an arrow function, not from the enclosing arrow function
abdulhamid999
left a comment
There was a problem hiding this comment.
Reviewed against core/ast/src/scope.rs, core/ast/src/scope_analyzer.rs, and core/engine/src/bytecompiler/env.rs (static inspection; not compiled locally).
Findings
- Arrow boundary vs.
functionflag: Keepingfunction: trueonScope::new_arrow(arrow: true) preserves closure-capture tracking inScope::access_binding(core/ast/src/scope.rs),ByteCompiler::push_scope(core/engine/src/bytecompiler/env.rs), and theassert!(scopes.function_scope().is_function())invariant inScopeIndexVisitor::visit_function_like(core/ast/src/scope_analyzer.rs), while allowingescape_this_in_enclosing_function_scopeto walk past all enclosing arrow scopes via!current.inner.arrow. - Sub-scopes and enclosing
thishosts: Non-arrowthishosts (Scope::new_global, functions/methods viaScope::new(..., true), class field scopes, andClassElement::StaticBlock) all setarrow: false, and intermediate sub-scopes (parameters_eval_scope,parameters_scope,lexical_scope, blocks) usefunction: false, arrow: false. That means 3+ nested arrows as well as nested arrows inside block scopes or default parameter initializers correctly propagatethis_escapedto the real enclosingthisbinding.superandnew.targetin nested arrows are already covered bycontains(..., ContainsSymbol::Super | ContainsSymbol::NewTarget)inScopeIndexVisitor. visit_function_likecleanup: Simplifyingfunction_scopeconstruction beforeif arrow { Scope::new_arrow(function_scope) } else { Scope::new(function_scope, true) }cleanly avoids duplicating the named-function-expression branch.
Minor Nits / Suggestions
- Doc comment phrasing (
core/ast/src/scope.rsaboveescape_this_in_enclosing_function_scope):"as arrow functions have the \this` of theirs"reads slightly awkwardly;"as arrow functions inherit `this` from their enclosing non-arrow function"` might read more clearly. - Extra test coverage (optional): Consider adding an assertion for a deeper chain or intermediate block/default-parameter arrow (e.g.,
() => { return () => this.value; }) alongside the existingobject.method()case.
Looks solid overall!
This branch has not been deployed
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.
This Pull Request fixes
thisin an arrow function nested in another arrow function, when only the inner one usesthis:escape_this_in_enclosing_function_scopemarkedthisas escaping in the first enclosing function scope, which was the outer arrow function.requires_function_scopeignores an arrow's escapedthis, so the method didn't keep itsthisfor the inner arrow.It changes the following:
Scope::new_arrowand know they are arrowsthisescapes from the first enclosing function scope that isn't an arrow function'snested_arrow_functions_have_the_this_of_their_method