Template Literals

Are Broken

Template Literals

  • should enable embedding languages
  • but escape sequences introduce a problem
  • \u00FF is a legal escape sequence but \unicode is not
  • can't embed languages if syntax is restricted to legal ES escape sequences
function latex(strs) {
  // ...
}

let doc = latex`
\newcommand{\fun}{\textbf{Fun!}}
\newcommand{\unicode}{\textbf{Unicode!}} 
\newcommand{\xerxes}{\textbf{King!}} 

Breve over the h goes \u{h}ere 
`;
function latex(strs) {
  // ...
}

let doc = latex`
\newcommand{\fun}{\textbf{Fun!}}




`;

Proposal:

Remove escape sequence restriction

Cooked Values Option 1

  • How should we handle cooked values with illegal escape sequences?
  • Cook valid escape sequences and leave invalid escape sequences raw
  • Seems like a footgun
  • Could be surprising that \unicode is literal but \uface is cooked to 龜
function tag(strs) {
  strs[0] === "\\unicode and U"
  strs.raw[0] === "\\unicode and \\u{55}";
}
tag`\unicode and \u{55}`

Cooked Values Option 2

  • How should we handle cooked values with illegal escape sequences?
  • Set cooked values with illegal escape sequences to undefined
  • Programmers still have access to raw
function tag(strs) {
  strs[0] === undefined
  strs.raw[0] === "\\unicode and \\u{55}";
}
tag`\unicode and \u{55}`
Made with Slides.com