Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve lexical scope #1421

Merged
merged 2 commits into from
May 4, 2023
Merged

Improve lexical scope #1421

merged 2 commits into from
May 4, 2023

Commits on May 4, 2023

  1. Improved lexical scope

    This is an initial implementation of an improved API for compiling a
    template with lexical scope.
    
    Instead of passing a list of locals to the precompileTemplate function,
    you can pass a function that takes a variable name and returns a boolean
    to indicate whether the variable is in scope.
    
    Importantly, this fixes an inconsistency with how HTML tags and keywords
    behave when they refer to a Handlebars local variable vs. when they
    refer to a JavaScript lexical variable.
    
    ```jsx
    const component = <template><div>{{yield}}</div></template>;
    
    <template>
      <component />
    </template>
    ```
    
    After this change, the `<component>` invocation correctly refers to the
    surrounding scope variable, even though `component` is a keyword.
    
    This makes it consistent with:
    
    ```jsx
    const variable = <template><div>{{yield}}</div></template>;
    
    <template>
    {{#let variable as |component|}}
      <component />
    {{/let}}
    </template>
    ```
    
    These semantics are an important future-proofing measure: they allow us
    to add additional keywords in the future, since local variables (either
    lexical JavaScript variables of Handlebars variables) will win over the
    newly defined keywords.
    
    If we allowed keywords to win over lexical JavaScript variables, every
    new keyword would be a breaking change. The semantics we used for
    `as |div|` and `as |component|` were carefully designed to avoid this
    problem, and this change helps us implement the same behavior for
    lexical JavaScript variables.
    wycats committed May 4, 2023
    Configuration menu
    Copy the full SHA
    1d7de6b View commit details
    Browse the repository at this point in the history
  2. Support upvar paths

    wycats committed May 4, 2023
    Configuration menu
    Copy the full SHA
    3d4f102 View commit details
    Browse the repository at this point in the history