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 2 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,4 +1,10 @@
export default function ({ types: t }) {
function buildConcatCallExression(target, items) {
return t.callExpression(
t.memberExpression(target, t.identifier("concat")), items
);
}

return {
visitor: {
TaggedTemplateExpression(path, state) {
Expand Down Expand Up @@ -45,23 +51,27 @@ 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])) {
if (!t.isStringLiteral(nodes[0]) && (state.opts.spec || !t.isStringLiteral(nodes[1]))) {
nodes.unshift(t.stringLiteral(""));
}
let root = nodes.shift();
Copy link
Member

Choose a reason for hiding this comment

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

I just got rid of this #shift 😛. Let's do:

let root = nodes[0];
if (spec && nodes.length > 1) {
  root = t.callExpression(t.memberExpression(target, t.identifier("concat")), nodes.slice(1));
} else {
  for (;;) {
    // ...
  }

Whether you use the helper function is up to you.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I just saw you had refactored this previously.
I'm ok with a bit of immutability 👍


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) {
root = buildConcatCallExression(root, nodes);
}
} else {
for (let i = 0; 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, "' \"", bar, "\"");
@@ -1 +1 @@
var foo = "test " + String(_.test(foo)) + " " + String(bar);
var foo = "test ".concat(_.test(foo), " ", bar);
@@ -1 +1 @@
var foo = "test " + String(foo) + " " + String(bar);
var foo = "test ".concat(foo, " ", 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