Skip to content

Lift Template Literal Restriction on Backslashes #12700

@DanielRosenwasser

Description

@DanielRosenwasser

Overview

Everyone loves LATEX so ESNext will likely allow tagged template strings to contain backslashes on any sequence - even ones that look like they might contain an invalid Unicode escapes.

In other words, something like

let x = tag `\u{wat}`;

is now valid, whereas previously it would be considered an error.

Reference: https://tc39.github.io/proposal-template-literal-revision/

As of this writing, the linked proposal is at Stage 3.

Notes

  1. This restriction lift doesn't apply to untagged template strings. For example:

    // All of these are still invalid.
    let bad = `bad escape sequence: \unicode`;
    let bracingOurselves = `\u{shouldntDoThis}`;
    let theWorst = `\xtremely bad`;
  2. Because there is no appropriate representation for a "cooked" string that contains one of these escapes, the returned value at a position on the top-level array will be undefined, but the raw representation will be available. For example:

    function tag(strs) {
      console.log(strs[0] === undefined);
      console.log(strs.raw[0] === "\\unicode and \\u{55}");
    }

// Prints 'true' twice.
tag\unicode and \u{55}

Proposed Emit

Input:

let x = tag `\u{hello} ${ 100 } \xtraordinary ${ 200 } wonderful ${ 300 } \uworld`;

Output:

var x = (
    _a = [undefined, undefined, " wonderful ", undefined],
    _a.raw = ["\\u{hello} ", " \\xtraordinary ", " wonderful ", " \\uworld"],
    tag(_a, 100, 200, 300)
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptES NextNew featurers for ECMAScript (a.k.a. ESNext)Help WantedYou can do this

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions