Skip to content

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

In review

Description

@trackoor

🐞 Describe the Bug

print(preprocess(src)) (and print(preprocess(src, { mode: 'codemod' }), { entityEncoding: 'raw' })) produces templates that parse to a different AST or fail to parse in two cases:

  1. Backslash-escaped mustaches: \{{escaped}} parses to a TextNode with chars: "{{escaped}}" (in element content and quoted attribute values), while \\{{live}} parses to TextNode("\\") followed by MustacheStatement("live"). Printer.TextNode / escapeText / escapeAttrValue in packages/@glimmer/syntax/lib/generation/ never re-escape {{ (or \ immediately preceding a mustache), so \{{escaped}} prints as {{escaped}} (now a MustacheStatement) and \\{{live}} prints as \{{live}} (now literal text).
  2. Segment-literal paths (Handlebars literal segments): Printer.PathExpression writes path.original, which joins head and tail with . without restoring [...]. {{foo.[bar baz]}} prints as {{foo.bar baz}} (a call to foo.bar with positional argument baz), {{foo.[bar.baz]}} prints as {{foo.bar.baz}} (three segments instead of two), and {{array.[0]}} prints as {{array.0}} (a parse error).

🔬 Minimal Reproduction

// npm i @glimmer/[email protected] && node repro.mjs
import { preprocess, print } from '@glimmer/syntax';

const inputs = [
  '\\{{escaped}}',
  '\\\\{{live}}',
  '<p title="\\{{x}}">a \\{{y}} b</p>',
  '{{foo.[bar baz]}}',
  '{{foo.[bar.baz]}}',
  '{{array.[0]}}',
];

for (const src of inputs) {
  console.log(JSON.stringify(src), '->', JSON.stringify(print(preprocess(src))));
}

😕 Actual Behavior

"\\{{escaped}}" -> "{{escaped}}"
"\\\\{{live}}" -> "\\{{live}}"
"<p title=\"\\{{x}}\">a \\{{y}} b</p>" -> "<p title=\"{{x}}\">a {{y}} b</p>"
"{{foo.[bar baz]}}" -> "{{foo.bar baz}}"
"{{foo.[bar.baz]}}" -> "{{foo.bar.baz}}"
"{{array.[0]}}" -> "{{array.0}}"

Re-parsing "{{escaped}}" produces a MustacheStatement instead of a TextNode, re-parsing "\\{{live}}" produces a TextNode instead of a backslash plus MustacheStatement, re-parsing "{{foo.bar baz}}" passes baz as a positional parameter, re-parsing "{{foo.bar.baz}}" splits "bar.baz" into two segments, and re-parsing "{{array.0}}" throws Parse error on line 1: Expecting 'ID', got 'NUMBER'.

🤔 Expected Behavior

The printed template parses back to an equivalent AST, preserving \{{ / \\{{ escapes in text and attributes and [...] brackets around path segments that are not plain Handlebars identifiers (\{{escaped}}, \\{{live}}, <p title="\{{x}}">a \{{y}} b</p>, {{foo.[bar baz]}}, {{foo.[bar.baz]}}, {{array.[0]}}).

🌍 Environment

  • Ember: ember-source 7.4.0 / @glimmer/syntax 0.97.0 (also reproduced on main at 9ed2915)
  • Ember-CLI: N/A (standalone @glimmer/syntax)
  • Node.js/npm: Node.js v26.13.0 / npm 11.20.0
  • OS: Linux x86_64
  • Browser: N/A

➕ Additional Context

The same output occurs in codemod mode (print(preprocess(src, { mode: 'codemod' }), { entityEncoding: 'raw' })), which is used by babel-plugin-ember-template-compilation when targetFormat: 'hbs' (such as v2 addon builds) as well as codemods built on @glimmer/syntax. For {{array.[0]}} specifically, see also the unmerged test PR glimmerjs/glimmer-vm#1264 on the archived glimmer-vm repo.

Credit: https://ucsbplse.github.io/

Activity

  1. added a commit that references this issue on Nov 24, 2026
    261717d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions