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
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7504d02
Spec compatibility for template literals.
yavorsky 0b83531
Update preset-es2015 `spec` expected case.
yavorsky 13e5834
Prevent array mutability by replacing `shift`.
yavorsky 68df216
Fix condition for single item.
yavorsky 76a6bc3
Group concats to ensure toPrimitive sequence.
yavorsky b83917f
Update function test case.
yavorsky b81996d
Add semi for function test case.
yavorsky 30fd06e
Simplify concat call expressions creating.
yavorsky 0932114
Fix some cases with multiple idengifiers.
yavorsky ae5e9d0
Add test case with different literals.
yavorsky 6dd693a
Add test case for `Symbol()` and toPrimitive order
yavorsky 291a308
Add actual literal case.
yavorsky dca1864
Add minNodeVersion to template literals order.
yavorsky 8e1ca42
Flip the logical expression.
yavorsky c360dc3
Update README for template literals spec option.
yavorsky 27942ab
docs [skip ci]
hzoo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
export default function ({ types: t }) { | ||
|
||
function buildConcatCallExressions(items) { | ||
return items.reduce(function(left, right) { | ||
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. To avoid the 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) { | ||
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. 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; | ||
|
||
|
@@ -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); | ||
|
2 changes: 1 addition & 1 deletion
2
...el-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var t = "'" + String(foo) + "' \"" + String(bar) + "\""; | ||
var t = "'".concat(foo, "' \"").concat(bar, "\""); |
2 changes: 1 addition & 1 deletion
2
.../babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), " ").concat(bar); |
2 changes: 1 addition & 1 deletion
2
...s/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "test " + String(foo) + " " + String(bar); | ||
var foo = "test ".concat(foo, " ").concat(bar); |
2 changes: 1 addition & 1 deletion
2
packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/only/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "" + String(test); | ||
var foo = "".concat(test); |
2 changes: 1 addition & 1 deletion
2
...ges/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/single/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "test " + String(foo); | ||
var foo = "test ".concat(foo); |
2 changes: 1 addition & 1 deletion
2
.../babel-plugin-transform-es2015-template-literals/test/fixtures/spec/statement/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
var foo = "test " + String(foo + bar); | ||
var foo = "test ".concat(foo + bar); |
2 changes: 1 addition & 1 deletion
2
packages/babel-preset-es2015/test/fixtures/preset-options/spec/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
.