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
default to spec mode for template literal transform #6098
Changes from 3 commits
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 |
---|---|---|
|
@@ -42,8 +42,7 @@ With options: | |
{ | ||
"plugins": [ | ||
["transform-es2015-template-literals", { | ||
"loose": true, | ||
"spec": true | ||
"loose": true | ||
}] | ||
] | ||
} | ||
|
@@ -69,22 +68,24 @@ require("babel-core").transform("code", { | |
|
||
`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` | ||
|
||
`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). | ||
When `false` or not set, 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** | ||
|
||
```javascript | ||
`foo${bar}baz${quux}${1}`; | ||
``` | ||
|
||
**Out** | ||
**Out (without `{"loose": true}`)** | ||
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 think it might be better if we update the example above with the default behavior, and keep this In/Out for |
||
|
||
```javascript | ||
"foo".concat(bar, "baz").concat(quux, 1); | ||
``` | ||
|
||
**Out (with `{"loose": true}`)** | ||
|
||
```javascript | ||
"foo" + bar + "baz" + quux + 1; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,20 +75,20 @@ export default function({ types: t }) { | |
// since `+` is left-to-right associative | ||
// ensure the first node is a string if first/second isn't | ||
const considerSecondNode = | ||
state.opts.spec || !t.isStringLiteral(nodes[1]); | ||
!state.opts.loose || !t.isStringLiteral(nodes[1]); | ||
if (!t.isStringLiteral(nodes[0]) && considerSecondNode) { | ||
nodes.unshift(t.stringLiteral("")); | ||
} | ||
let root = nodes[0]; | ||
|
||
if (state.opts.spec) { | ||
if (nodes.length > 1) { | ||
root = buildConcatCallExressions(nodes); | ||
} | ||
} else { | ||
if (state.opts.loose) { | ||
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. @jridgewell let me know if i misunderstood what you meant by "flip" here! 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. That was it. |
||
for (let i = 1; i < nodes.length; i++) { | ||
root = t.binaryExpression("+", root, nodes[i]); | ||
} | ||
} else { | ||
if (nodes.length > 1) { | ||
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. Can you also merge 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. 👍 great idea! |
||
root = buildConcatCallExressions(nodes); | ||
} | ||
} | ||
|
||
path.replaceWith(root); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": [["transform-es2015-template-literals", {"loose": true}]] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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.
Maybe: