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

Add support for a # prefix in path segments #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

wycats
Copy link
Contributor

@wycats wycats commented Oct 11, 2024

To support embedding Handlebars templates in JavaScript classes and allow them to access private fields, we need property paths segments to be able to begin with a #.

Since # is already a control character (i.e. {{#block}}), this parser change implements a new path separator (.#), which effectively creates a new kind of identifier (a path segment identifier) that has the same lexical structure as any other identifier, but can only appear at the beginning of a path segment. The . and # characters count as a single token, and cannot be separated by whitespace.

This syntax is intended to be used in embedding environments that are careful about lexical scope, and require an explicit this. prefix to access the current instance.

In classic Handlebars environments, you would expect {{this.#foo}} to be equivalent to {{#foo}}, but that would parse as a block start, which would be ambiguous.

In the future, it would be helpful to explicitly document the strict lexical subset of Handlebars, but this change doesn't break classic Handlebars. In classic Handlebars contexts, this change would make this.#foo work without needing this.[#foo]. It might seem surprising that #foo isn't equivalent to this.#foo, but it's not a breaking change and the reason that {{#foo}} can't parse as a path is fairly obvious.

To support embedding Handlebars templates in JavaScript classes and
allow them to access private fields, we need property paths segments
to be able to begin with a `#`.

Since `#` is already a control character (i.e. `{{#block}}`), this
parser change implements a new path separator (`.#`), which effectively
creates a new kind of identifier (a path segment identifier) that has
the same lexical structure as any other identifier, but can only appear
at the beginning of a path segment. The `.` and `#` characters count as
a single token, and cannot be separated by whitespace.

This syntax is intended to be used in embedding environments that are
careful about lexical scope, and require an explicit `this.` prefix to
access the current instance.

In classic Handlebars environments, you would expect `{{this.#foo}}` to
be equivalent to `{{#foo}}`, but that would parse as a block start,
which would be ambiguous.

In the future, it would be helpful to explicitly document the strict
lexical subset of Handlebars, but this change doesn't *break* classic
Handlebars. In classic Handlebars contexts, this change would make
`this.#foo` work without needing `this[#foo]`. It might seem surprising
that `#foo` isn't equivalent to `this.#foo`, but it's not a breaking
change and the reason that `{{#foo}}` can't parse as a path is fairly
obvious.
@jaylinski
Copy link
Member

jaylinski commented Oct 12, 2024

I think you have to either add the changes in package-lock.json or delete it, otherwise the pipeline (npm) complains.

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.

2 participants