Skip to content

fix(ast): give nested arrow functions the this of their enclosing function - #5691

Open
lehni wants to merge 1 commit into
boa-dev:mainfrom
lehni:fix/nested-arrow-this
Open

lehni wants to merge 1 commit into
boa-dev:mainfrom
lehni:fix/nested-arrow-this

Conversation

@lehni

@lehni lehni commented Nov 24, 2026

Copy link
Copy Markdown

This Pull Request fixes this in an arrow function nested in another arrow function, when only the inner one uses this:

const object = {
    method() {
        return [1].map(() => [2].map(() => this.value));
    },
    value: 3,
};
object.method(); // [[undefined]] instead of [[3]]; `this.f()` throws "not a callable function"

escape_this_in_enclosing_function_scope marked this as escaping in the first enclosing function scope, which was the outer arrow function. requires_function_scope ignores an arrow's escaped this, so the method didn't keep its this for the inner arrow.

It changes the following:

  • Scopes of arrow functions are created with Scope::new_arrow and know they are arrows
  • this escapes from the first enclosing function scope that isn't an arrow function's
  • Adds a test, nested_arrow_functions_have_the_this_of_their_method

…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
@lehni
lehni requested a review from a team as a code owner November 24, 2026 21:43
@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Nov 24, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 24, 2026
@github-actions github-actions Bot added C-Tests Issues and PRs related to the tests. C-AST Issue surrounding the abstract syntax tree labels Nov 24, 2026

@abdulhamid999 abdulhamid999 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. function flag: Keeping function: true on Scope::new_arrow (arrow: true) preserves closure-capture tracking in Scope::access_binding (core/ast/src/scope.rs), ByteCompiler::push_scope (core/engine/src/bytecompiler/env.rs), and the assert!(scopes.function_scope().is_function()) invariant in ScopeIndexVisitor::visit_function_like (core/ast/src/scope_analyzer.rs), while allowing escape_this_in_enclosing_function_scope to walk past all enclosing arrow scopes via !current.inner.arrow.
  • Sub-scopes and enclosing this hosts: Non-arrow this hosts (Scope::new_global, functions/methods via Scope::new(..., true), class field scopes, and ClassElement::StaticBlock) all set arrow: false, and intermediate sub-scopes (parameters_eval_scope, parameters_scope, lexical_scope, blocks) use function: false, arrow: false. That means 3+ nested arrows as well as nested arrows inside block scopes or default parameter initializers correctly propagate this_escaped to the real enclosing this binding. super and new.target in nested arrows are already covered by contains(..., ContainsSymbol::Super | ContainsSymbol::NewTarget) in ScopeIndexVisitor.
  • visit_function_like cleanup: Simplifying function_scope construction before if 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.rs above escape_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 existing object.method() case.

Looks solid overall!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-AST Issue surrounding the abstract syntax tree C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants