Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec compatibility for template literals. #5791

Merged
merged 16 commits into from Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -75,7 +75,7 @@ In loose mode, tagged template literal objects aren't frozen.

`boolean`, defaults to `false`.

This option wraps all template literal expressions with `String`. See [babel/babel#1065](https://github.com/babel/babel/issues/1065) for more info.
Copy link
Member

@hzoo hzoo Jun 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove the link? We should add a reason for why this is necessary or a good idea so people understand why they would use this option (if it's just old then we can add a new sentence here)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hzoo I think we can add a link to this PR in README. Also going to add a few sentences about spec option and why we are using String.prototype.concat.

This option combines all template literal expressions and quasis with `String.prototype.concat`.

**In**

Expand All @@ -86,5 +86,5 @@ This option wraps all template literal expressions with `String`. See [babel/bab
**Out**

```javascript
"foo" + String(bar);
"foo".concat(bar);
```
@@ -1,7 +1,28 @@
export default function ({ types: t }) {

function buildConcatCallExressions(items) {
return items.reduce(function(left, right) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the _withIdentifier, can't we rewrite to:

let first = true;
return items.reduce((left, right) => {
  // Use isLiteral instead, 3 and null, and etc.
  let canInsert = t.isLiteral(right);
  if (!canInsert && first) {
    canInsert = true;
    first = false;
  }
  if (canInsert) {
    left.arguments.push(right);
    return left;
  }
  return t.callExpression(
    t.memberExpression(left, t.identifier('concat')),
    [right]
  );
});

const isString = t.isStringLiteral(right);
const canBeInserted = !left._withIdentifier || isString;

if (t.isCallExpression(left) && canBeInserted) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can flip the LogicalExpression to save a function call in some cases.

left._withIdentifier = left._withIdentifier || !isString;
left.arguments.push(right);
return left;
}
const nextCall = t.callExpression(
t.memberExpression(left, t.identifier("concat")),
[right]
);
nextCall._withIdentifier = !isString;
return nextCall;
});
}

return {
visitor: {
TaggedTemplateExpression(path, state) {

const { node } = path;
const { quasi } = node;

Expand Down Expand Up @@ -45,23 +66,28 @@ export default function ({ types: t }) {
if (index < expressions.length) {
const expr = expressions[index++];
const node = expr.node;
if (state.opts.spec && !expr.isBaseType("string") && !expr.isBaseType("number")) {
nodes.push(t.callExpression(t.identifier("String"), [node]));
} else if (!t.isStringLiteral(node, { value: "" })) {
if (!t.isStringLiteral(node, { value: "" })) {
nodes.push(node);
}
}
}

// since `+` is left-to-right associative
// ensure the first node is a string if first/second isn't
if (!t.isStringLiteral(nodes[0]) && !t.isStringLiteral(nodes[1])) {
const considerSecondNode = state.opts.spec || !t.isStringLiteral(nodes[1]);
if (!t.isStringLiteral(nodes[0]) && considerSecondNode) {
nodes.unshift(t.stringLiteral(""));
}

let root = nodes[0];
for (let i = 1; i < nodes.length; i++) {
root = t.binaryExpression("+", root, nodes[i]);

if (state.opts.spec) {
if (nodes.length > 1) {
root = buildConcatCallExressions(nodes);
}
} else {
for (let i = 1; i < nodes.length; i++) {
root = t.binaryExpression("+", root, nodes[i]);
}
}

path.replaceWith(root);
Expand Down
@@ -1 +1 @@
var t = "'" + String(foo) + "' \"" + String(bar) + "\"";
var t = "'".concat(foo, "' \"").concat(bar, "\"");
@@ -1 +1 @@
var foo = "test " + String(_.test(foo)) + " " + String(bar);
var foo = "test ".concat(_.test(foo), " ").concat(bar);
@@ -1 +1 @@
var foo = "test " + String(foo) + " " + String(bar);
var foo = "test ".concat(foo, " ").concat(bar);
@@ -1 +1 @@
var foo = "" + String(test);
var foo = "".concat(test);
@@ -1 +1 @@
var foo = "test " + String(foo);
var foo = "test ".concat(foo);
@@ -1 +1 @@
var foo = "test " + String(foo + bar);
var foo = "test ".concat(foo + bar);
@@ -1,7 +1,7 @@
"use strict";

var _this = undefined;
"1" + String(a);
"1".concat(a);

(function () {
babelHelpers.newArrowCheck(this, _this);
Expand Down