default to spec mode for template literal transform (#6098)

* spec/loose/default switch for template literal transform, update/re-org tests

* update readme

* flip if statements

* consolidate else/if

* readme wording modification, updates to examples
This commit is contained in:
Noah Lemen 2017-08-28 14:57:09 -04:00 committed by Henry Zhu
parent 95dd16aeeb
commit 40805894c5
59 changed files with 18 additions and 78 deletions

View File

@ -13,7 +13,7 @@
**Out** **Out**
```javascript ```javascript
"foo" + bar; "foo".concat(bar);
``` ```
## Installation ## Installation
@ -42,8 +42,7 @@ With options:
{ {
"plugins": [ "plugins": [
["transform-es2015-template-literals", { ["transform-es2015-template-literals", {
"loose": true, "loose": true
"spec": true
}] }]
] ]
} }
@ -69,22 +68,18 @@ require("babel-core").transform("code", {
`boolean`, defaults to `false`. `boolean`, defaults to `false`.
In loose mode, tagged template literal objects aren't frozen. When `true`, tagged template literal objects aren't frozen. All template literal expressions and quasis are combined with the `+` operator instead of with `String.prototype.concat`.
### `spec` When `false` or not set, all template literal expressions and quasis are combined with `String.prototype.concat`. It will handle cases with `Symbol.toPrimitive` correctly and throw correctly if template literal expression is a `Symbol()`. See [babel/babel#5791](https://github.com/babel/babel/pull/5791).
`boolean`, defaults to `false`.
This option combines all template literal expressions and quasis with `String.prototype.concat`. It will handle cases with `Symbol.toPrimitive` correctly and throw correctly if template literal expression is a `Symbol()`. See [babel/babel#5791](https://github.com/babel/babel/pull/5791).
**In** **In**
```javascript ```javascript
`foo${bar}baz${quux}${1}`; `foo${bar}`;
``` ```
**Out** **Out**
```javascript ```javascript
"foo".concat(bar, "baz").concat(quux, 1); "foo" + bar;
``` ```

View File

@ -75,20 +75,18 @@ export default function({ types: t }) {
// since `+` is left-to-right associative // since `+` is left-to-right associative
// ensure the first node is a string if first/second isn't // ensure the first node is a string if first/second isn't
const considerSecondNode = const considerSecondNode =
state.opts.spec || !t.isStringLiteral(nodes[1]); !state.opts.loose || !t.isStringLiteral(nodes[1]);
if (!t.isStringLiteral(nodes[0]) && considerSecondNode) { if (!t.isStringLiteral(nodes[0]) && considerSecondNode) {
nodes.unshift(t.stringLiteral("")); nodes.unshift(t.stringLiteral(""));
} }
let root = nodes[0]; let root = nodes[0];
if (state.opts.spec) { if (state.opts.loose) {
if (nodes.length > 1) {
root = buildConcatCallExressions(nodes);
}
} else {
for (let i = 1; i < nodes.length; i++) { for (let i = 1; i < nodes.length; i++) {
root = t.binaryExpression("+", root, nodes[i]); root = t.binaryExpression("+", root, nodes[i]);
} }
} else if (nodes.length > 1) {
root = buildConcatCallExressions(nodes);
} }
path.replaceWith(root); path.replaceWith(root);

View File

@ -0,0 +1,3 @@
{
"plugins": [["transform-es2015-template-literals", {"loose": true}]]
}

View File

@ -0,0 +1,5 @@
var _templateObject = _taggedTemplateLiteralLoose(["wow\na", "b ", ""], ["wow\\na", "b ", ""]);
function _taggedTemplateLiteralLoose(strings, raw) { strings.raw = raw; return strings; }
var foo = bar(_templateObject, 42, _.foobar());

View File

@ -1,3 +0,0 @@
var foo = bar`wow\na${ 42 }b ${_.foobar()}`;
var bar = bar`wow\nab${ 42 } ${_.foobar()}`;
var bar = bar`wow\naB${ 42 } ${_.baz()}`;

View File

@ -1,9 +0,0 @@
var _templateObject = _taggedTemplateLiteralLoose(["wow\na", "b ", ""], ["wow\\na", "b ", ""]),
_templateObject2 = _taggedTemplateLiteralLoose(["wow\nab", " ", ""], ["wow\\nab", " ", ""]),
_templateObject3 = _taggedTemplateLiteralLoose(["wow\naB", " ", ""], ["wow\\naB", " ", ""]);
function _taggedTemplateLiteralLoose(strings, raw) { strings.raw = raw; return strings; }
var foo = bar(_templateObject, 42, _.foobar());
var bar = bar(_templateObject2, 42, _.foobar());
var bar = bar(_templateObject3, 42, _.baz());

View File

@ -1,3 +0,0 @@
{
"plugins": [["transform-es2015-template-literals", { "loose": true }]]
}

View File

@ -1,5 +0,0 @@
var _templateObject = _taggedTemplateLiteral(["wow\na", "b ", ""], ["wow\\na", "b ", ""]);
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
var foo = bar(_templateObject, 42, _.foobar());

View File

@ -1,3 +0,0 @@
{
"plugins": [["transform-es2015-template-literals", { "loose": true }]]
}

View File

@ -1,3 +0,0 @@
{
"plugins": [["transform-es2015-template-literals", { "spec": true }]]
}

View File

@ -1,13 +0,0 @@
tag`\unicode and \u{55}`;
tag`\01`;
tag`\xg${0}right`;
tag`left${0}\xg`;
tag`left${0}\xg${1}right`;
tag`left${0}\u000g${1}right`;
tag`left${0}\u{-0}${1}right`;
function a() {
var undefined = 4;
tag`\01`;
}

View File

@ -1,22 +0,0 @@
var _templateObject = _taggedTemplateLiteral([void 0], ["\\unicode and \\u{55}"]),
_templateObject2 = _taggedTemplateLiteral([void 0], ["\\01"]),
_templateObject3 = _taggedTemplateLiteral([void 0, "right"], ["\\xg", "right"]),
_templateObject4 = _taggedTemplateLiteral(["left", void 0], ["left", "\\xg"]),
_templateObject5 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\xg", "right"]),
_templateObject6 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"]),
_templateObject7 = _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "right"]);
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
tag(_templateObject);
tag(_templateObject2);
tag(_templateObject3, 0);
tag(_templateObject4, 0);
tag(_templateObject5, 0, 1);
tag(_templateObject6, 0, 1);
tag(_templateObject7, 0, 1);
function a() {
var undefined = 4;
tag(_templateObject2);
}