Skip to content

Implement Template Literals Revision proposal #4798

@TimothyGu

Description

@TimothyGu

The Template Literals Revision proposal reached Stage 3 in July, but is not yet implemented.

Input Code

Example derived from the proposal linked above.

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

Expected Behavior

The above code should execute without throwing any error.

Current Behavior

An error is currently thrown by Babylon:

repl: Bad character escape sequence (5:6)
  3 |   assert(strs.raw[0] === "\\unicode and \\u{55}");
  4 | }
> 5 | tag`\unicode and \u{55}`;
    |  

Possible Solution

First step would of course be adding support for it in Babylon.

Currently, the babel-plugin-transform-es2015-template-literals plugin transforms

tag`ab\\c`;

to

var _templateObject = _taggedTemplateLiteral(["ab\\c"], ["ab\\\\c"]);

function _taggedTemplateLiteral(strings, raw) {
  return Object.freeze(Object.defineProperties(strings, {
    raw: {
      value: Object.freeze(raw)
    }
  }));
}

tag(_templateObject);

To support this new syntax it could just convert

tag`\unicode and \u{55}`;

to

var _templateObject = _taggedTemplateLiteral([], ["\\unicode and \\u{55}"]);

function _taggedTemplateLiteral(strings, raw) {
  return Object.freeze(Object.defineProperties(strings, {
    raw: {
      value: Object.freeze(raw)
    }
  }));
}

tag(_templateObject);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions