Repository navigation
Description
🐞 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:
- Backslash-escaped mustaches:
\{{escaped}}parses to aTextNodewithchars: "{{escaped}}"(in element content and quoted attribute values), while\\{{live}}parses toTextNode("\\")followed byMustacheStatement("live").Printer.TextNode/escapeText/escapeAttrValueinpackages/@glimmer/syntax/lib/generation/never re-escape{{(or\immediately preceding a mustache), so\{{escaped}}prints as{{escaped}}(now aMustacheStatement) and\\{{live}}prints as\{{live}}(now literal text). - Segment-literal paths (Handlebars literal segments):
Printer.PathExpressionwritespath.original, which joinsheadandtailwith.without restoring[...].{{foo.[bar baz]}}prints as{{foo.bar baz}}(a call tofoo.barwith positional argumentbaz),{{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-source7.4.0 /@glimmer/syntax0.97.0 (also reproduced onmainat9ed2915) - 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
Keep escaped mustaches and segment literals in print()