Skip to content

Add support for a # prefix in path segments#14

Merged
wycats merged 9 commits intomasterfrom
feature/private-fields
Oct 23, 2024
Merged

Add support for a # prefix in path segments#14
wycats merged 9 commits intomasterfrom
feature/private-fields

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.

@wycats
Copy link
Contributor Author

wycats commented Oct 23, 2024

@jaylinski Thanks!

@wycats wycats merged commit 723e8b7 into master Oct 23, 2024
@wycats wycats deleted the feature/private-fields branch October 23, 2024 04:19
@wycats
Copy link
Contributor Author

wycats commented Oct 23, 2024

@jaylinski I updated the infrastructure to use pnpm and the GitHub action setup I use elsewhere. I don't think this should affect anything for consumers of the parser, but please let me know if you notice any issues downstream 😄

@jaylinski
Copy link
Member

Looks good! There is no stable release of handlebars yet that uses this package (handlebars 4.x still uses its own parser). But I'll test it with the handlebars master-branch as soon as you publish a new release.

@github-actions github-actions bot mentioned this pull request Mar 19, 2025
@NullVoxPopuli NullVoxPopuli added the enhancement New feature or request label Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants