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

default to spec mode for template literal transform #6098

Merged
merged 5 commits into from Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 10 additions & 9 deletions packages/babel-plugin-transform-es2015-template-literals/README.md
Expand Up @@ -42,8 +42,7 @@ With options:
{
"plugins": [
["transform-es2015-template-literals", {
"loose": true,
"spec": true
"loose": true
}]
]
}
Expand All @@ -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).
Copy link
Member

Choose a reason for hiding this comment

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

When false or not set, combines all template...

Maybe:

When false or not set, all template literal expressions and quasis are combined with String.prototype.concat.


**In**

```javascript
`foo${bar}baz${quux}${1}`;
```

**Out**
**Out (without `{"loose": true}`)**
Copy link
Member

Choose a reason for hiding this comment

The 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 loose: true


```javascript
"foo".concat(bar, "baz").concat(quux, 1);
```

**Out (with `{"loose": true}`)**

```javascript
"foo" + bar + "baz" + quux + 1;
```
Expand Up @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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!

Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you also merge else { if ( -> else if (?

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 great idea!

root = buildConcatCallExressions(nodes);
}
}

path.replaceWith(root);
Expand Down
@@ -0,0 +1,3 @@
{
"plugins": [["transform-es2015-template-literals", {"loose": true}]]
}
@@ -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.