Skip to content

Keep escaped mustaches and segment literals in print() - #21794

Open
NullVoxPopuli-ai-agent wants to merge 5 commits into
emberjs:mainfrom
NullVoxPopuli-ai-agent:nvp/print-escapes-and-segment-literals
Open

NullVoxPopuli-ai-agent wants to merge 5 commits into
emberjs:mainfrom
NullVoxPopuli-ai-agent:nvp/print-escapes-and-segment-literals

Conversation

@NullVoxPopuli-ai-agent

Copy link
Copy Markdown
Contributor

Fixes #21791. print() from @glimmer/syntax now gives templates that parse back to the same AST.

Input Before After
\{{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-compilation sends every template through print() when targetFormat is 'hbs', as in v2 addon builds. So an addon with \{{ or array.[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 (&#123;, &#125;, &#92;). The source a&#92;\{{b}} produces that last case.

Changes to output:

  • A path segment gets brackets only if it is not a plain Handlebars ID, a number, or a literal keyword. {{this.[foo]}} still prints as {{this.foo}}, and {{namespace/foo}} does not change.
  • A codemod that puts {{x}} in TextNode.chars to make a live mustache now gets the text \{{x}}. Use a MustacheStatement for that.
  • In 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

@NullVoxPopuli
NullVoxPopuli requested a review from ef4 November 18, 2026 23:17
@NullVoxPopuli

Copy link
Copy Markdown
Contributor

i realize now that the [] stuff may have been explicitly removed earlier 🤔

@NullVoxPopuli
NullVoxPopuli marked this pull request as draft November 24, 2026 16:29
NullVoxPopuli and others added 4 commits November 24, 2026 12:39
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]>
@NullVoxPopuli-ai-agent
NullVoxPopuli-ai-agent force-pushed the nvp/print-escapes-and-segment-literals branch from 37d6e02 to 8b166c2 Compare November 24, 2026 17:39
@NullVoxPopuli
NullVoxPopuli marked this pull request as ready for review November 24, 2026 17:53
@johanrd

johanrd commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

b.path('this.list.[1]') now prints {{this.list.[[1\]]}} (was {{this.list.[1]}}), because buildHead keeps the brackets in the segment. I found no public callers that do this, so maybe just a line in "Changes to output"?

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is what we need to review carefully

the nested levels of escaping is a bit dizzying

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

importantly though, existing tests are unchanged -- so all this coverage is at least additive from today's behavior

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 @arg attributes
  • an escaped mustache directly before or after a real one
  • -1, 1.5, null, .., @bar, a"b, [] and this.[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.

This branch has not been deployed

No deployments
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.

@glimmer/syntax print() drops \{{ escapes and [...] segment-literal brackets

3 participants