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
Changes from 2 commits
7504d02
0b83531
13e5834
68df216
76a6bc3
b83917f
b81996d
30fd06e
0932114
ae5e9d0
6dd693a
291a308
dca1864
8e1ca42
c360dc3
27942ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just got rid of this 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I just saw you had refactored this previously. |
||
|
||
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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var t = "'" + String(foo) + "' \"" + String(bar) + "\""; | ||
var t = "'".concat(foo, "' \"", bar, "\""); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "test " + String(_.test(foo)) + " " + String(bar); | ||
var foo = "test ".concat(_.test(foo), " ", bar); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "test " + String(foo) + " " + String(bar); | ||
var foo = "test ".concat(foo, " ", bar); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "" + String(test); | ||
var foo = "".concat(test); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "test " + String(foo); | ||
var foo = "test ".concat(foo); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "test " + String(foo + bar); | ||
var foo = "test ".concat(foo + bar); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
.