Add support for a #
prefix in path segments
#14
+598
−320
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.
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 needingthis.[#foo]
. It might seem surprising that#foo
isn't equivalent tothis.#foo
, but it's not a breaking change and the reason that{{#foo}}
can't parse as a path is fairly obvious.