Template Literals

&

EscapeSequences

Template Literals

  • should enable embedding languages
  • but escape sequences are a problem
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 
`;

Remove EscapeSequence restriction

for tagged template literals

  • Illegal EscapeSequences no longer a syntax errorĀ 
  • Set cooked values with illegal escape sequences to undefined
  • Programmers still have access to raw
  • Untagged illegal escapes are still a syntax error
function tag(strs) {
  strs[0] === undefined
  strs.raw[0] === "\\unicode ";

  strs[1] === " U";
  strs.raw[1] === " \\u{55}";
}
tag`\unicode ${"and"} \u{55}`


// syntax error
let untagged = `\unicode and \u{55}` 

Grammar Details

One more thing...

<!doctype html>
<html>
  <head>
    <script>
        "use </script"
       let s = html`</script>`
    </script>
  </head>
</html>

Template Literal Proposal May

By disnet

Template Literal Proposal May

  • 435
Loading comments...

More from disnet