Skip to content

Template literals with substitutions process their escape sequences - #539

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-template-literal-escapes
Oct 11, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-template-literal-escapes

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

Escape sequences in the text around a substitution were kept as written:

const n = 1;
const s = `${n}\n${n}`;
print(s.length);   // 4: "1", "\", "n", "1" (expected 3)

A template without substitutions and a string literal were fine. Reported in docs/bugs/native-ts-graphics-findings.md, issue 4 (docs/bugs/ntg_template_literal_escape.ts): ImGui labels showed a literal \n.

The same mistake was in two more places:

  • Tagged templates: the tag received the unprocessed strings (tag\x\ty${n}``).
  • Template literal types: type Greeting = `hi\t${Name}` kept the backslash, so const g: Greeting = "hi\tyou" failed to compile with can't cast from literal type: '"hi\09you"' to '"hi\tyou"'.

Fix

Use the template node's text (escapes processed by the scanner) instead of rawText for the head and every span, in mlirGen(TemplateLiteralLikeNode), mlirGen(TaggedTemplateExpression) and getTemplateLiteralType/getTemplateLiteralSpan. In JavaScript a tag's strings are the processed ones (the raw ones are strings.raw, which tslang doesn't provide), so the tag now gets what JavaScript would give it.

Testing

  • New 00template_literal_escapes.ts (compile + JIT + rc/none corpus) covers escapes in the head, middle and tail, \x41/B, escaped backticks, a tagged template and a template literal type. The current release fails with assertion failed: middle escape (and the type case doesn't compile).
  • docs/bugs/ntg_template_literal_escape.ts now prints 3, 3, 3.
  • Full ctest: 3919/3919 pass.

🤖 Generated with Claude Code

The text around a substitution was taken as written (`rawText`) instead
of with its escapes processed (`text`): `${n}\n${n}` held the four
characters 1, \, n, 1, so two-line labels showed a literal "\n". Tagged
templates passed the tag the same unprocessed strings, and a template
literal type kept them too, so "hi\tyou" did not match `hi\t${Name}`.

Reported in docs/bugs/native-ts-graphics-findings.md (issue 4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 5391b05 into main Oct 11, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-template-literal-escapes branch October 11, 2026 12:32
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.

1 participant