Repository navigation
Keep escaped mustaches and segment literals in print() - #21794
NullVoxPopuli-ai-agent wants to merge 5 commits into
Conversation
|
i realize now that the |
print() now gives output that parses back to the same AST:
- `\{{` stays escaped in text and in attribute values, and `\\{{` keeps
its live mustache.
- A path segment that is not a plain ID keeps its brackets, for example
`{{foo.[bar baz]}}` and `{{array.[0]}}`.
- Text that Handlebars cannot express next to a mustache (a `\` before a
literal `{{`, a `{` before a mustache, a `}` after one) uses an HTML
entity.
Fixes emberjs#21791
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
37d6e02 to
8b166c2
Compare
|
|
b.path('this.list.[1]') kept the brackets in the tail segment. The
printer now brackets segments itself, so it printed [[1\]]. Split the
string the way the Handlebars lexer reads it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| // Empty string literal: arguments use ="" while attributes are valueless | ||
| `<Hello @world="" data-foo />`, | ||
|
|
||
| // escaped mustaches |
There was a problem hiding this comment.
this file is what we need to review carefully
the nested levels of escaping is a bit dizzying
There was a problem hiding this comment.
importantly though, existing tests are unchanged -- so all this coverage is at least additive from today's behavior
There was a problem hiding this comment.
I/claude checked every case this PR adds to print-test.ts at 837b774. All of the expectations seem correct.
How checked each case was checked:
- Is it stable in default mode and in codemod/`raw` mode? - Does `parse(print(ast))` give the same AST (ignoring loc)? - Does `precompile(src)` give the same wire format as `precompile(print(...))`, with adjacent text ops merged? This is what decides what renders.The added templates cases (L103-L123) and the builder cases (L205-L218) pass all three checks. On main the same inputs compile to a different wire format, except {{!-- c --}}} and {{this.[foo]}}. I also tried about 50 more inputs and all of them round-trip:
- runs of 1 to 5 backslashes before
{{ \{{{x}}}\{{x}}inside<textarea>,<pre>, single-quoted attributes and@argattributes- an escaped mustache directly before or after a real one
-1,1.5,null,..,@bar,a"b,[]andthis.[0]as segments
Three gaps remain. main prints the same output for each of them, so none is a regression. Take them or leave them:
| input | printed | result |
|---|---|---|
b.text('a{'), b.text('{b}}') |
a{{b}} |
parses as a mustache b. textNeighbor only looks for a neighbor that starts with {{, so a { split across two text nodes is not caught. The same thing happens with [mustache, b.text(''), b.text('}')], which prints {{x}}}. Escaping the joined text of adjacent text nodes would fix both. |
{{foo.[this]}} |
{{foo.this}} |
Invalid path: foo.this. Also {{[else]}} prints as {{else}}, which is a parse error. printPathSegment could put this and else in brackets too. |
a segment that ends in \, followed later by ] |
{{foo.[a\\]}}{{bar.[0]}} |
reparses as a single tail a\]}}{{bar.[0. The cause is in the lexer, not the printer: '['('\\]'|[^\]])*']' reads \\] as \ + \]. The same source written by hand gives the same result. |
Fixes #21791.
print()from@glimmer/syntaxnow gives templates that parse back to the same AST.\{{escaped}}{{escaped}}(live mustache)\{{escaped}}\\{{live}}\{{live}}(text)\\{{live}}<p title="\{{x}}"><p title="{{x}}"><p title="\{{x}}">{{foo.[bar baz]}}{{foo.bar baz}}(one argument){{foo.[bar baz]}}{{foo.[bar.baz]}}{{foo.bar.baz}}(three segments){{foo.[bar.baz]}}{{array.[0]}}{{array.0}}(parse error){{array.[0]}}This matters outside of codemods.
babel-plugin-ember-template-compilationsends every template throughprint()whentargetFormatis'hbs', as in v2 addon builds. So an addon with\{{orarray.[0]in a template published a changed or broken template.The same printer bug also hit text next to a mustache. A text that ends in
{before a mustache printed{{{, and a text that starts with}after one printed}}}. A\right before a literal{{also had no valid output. Handlebars cannot express these three cases, so the printer now uses an HTML entity ({,},\). The sourcea\\{{b}}produces that last case.Changes to output:
{{this.[foo]}}still prints as{{this.foo}}, and{{namespace/foo}}does not change.{{x}}inTextNode.charsto make a live mustache now gets the text\{{x}}. Use aMustacheStatementfor that.entityEncoding: 'raw'mode, the three entity cases come back as entity text when parsed again in codemod mode. This is the same as the existing quote fallback in attribute values. A normal compile decodes the entity.Tests: round-trip cases in both print modes in
packages/@glimmer/syntax/test/generation/print-test.ts, plus builder-made nodes for the entity cases.🤖 Generated with Claude Code