From 7504d02dedadbef21268dd9dfc95a3fdfd1563da Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Tue, 30 May 2017 14:17:15 +0300 Subject: [PATCH 01/16] Spec compatibility for template literals. --- .../README.md | 4 ++-- .../src/index.js | 24 +++++++++++++------ .../fixtures/spec/escape-quotes/expected.js | 2 +- .../test/fixtures/spec/functions/expected.js | 2 +- .../test/fixtures/spec/multiple/expected.js | 2 +- .../test/fixtures/spec/only/expected.js | 2 +- .../test/fixtures/spec/single/expected.js | 2 +- .../test/fixtures/spec/statement/expected.js | 2 +- 8 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/README.md b/packages/babel-plugin-transform-es2015-template-literals/README.md index 477b11cce60b..be3ec6531912 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/README.md +++ b/packages/babel-plugin-transform-es2015-template-literals/README.md @@ -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. +This option combines all template literal expressions and quasis with `String.prototype.concat`. **In** @@ -86,5 +86,5 @@ This option wraps all template literal expressions with `String`. See [babel/bab **Out** ```javascript -"foo" + String(bar); +"foo".concat(bar); ``` diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index 12554e9027aa..eaaa3033771d 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -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,9 +51,7 @@ 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); } } @@ -55,13 +59,19 @@ export default function ({ types: t }) { // 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(); - 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); diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js index 287788efd92c..6ea0de512e9f 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js @@ -1 +1 @@ -var t = "'" + String(foo) + "' \"" + String(bar) + "\""; +var t = "'".concat(foo, "' \"", bar, "\""); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js index 9268305f36ff..976c78de00ff 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js @@ -1 +1 @@ -var foo = "test " + String(_.test(foo)) + " " + String(bar); +var foo = "test ".concat(_.test(foo), " ", bar); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js index 29a6093e5ccd..64d22b2e1d49 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js @@ -1 +1 @@ -var foo = "test " + String(foo) + " " + String(bar); +var foo = "test ".concat(foo, " ", bar); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/only/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/only/expected.js index eadb924d1dc4..84a771cd4de0 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/only/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/only/expected.js @@ -1 +1 @@ -var foo = "" + String(test); +var foo = "".concat(test); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/single/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/single/expected.js index 265332f7f2d2..f32ccd500f3c 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/single/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/single/expected.js @@ -1 +1 @@ -var foo = "test " + String(foo); +var foo = "test ".concat(foo); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/statement/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/statement/expected.js index 7625842cc80b..d0367942ae13 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/statement/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/statement/expected.js @@ -1 +1 @@ -var foo = "test " + String(foo + bar); +var foo = "test ".concat(foo + bar); \ No newline at end of file From 0b835314363749b1f466f7fb66f3ee58ed8fc2fc Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Tue, 30 May 2017 18:02:04 +0300 Subject: [PATCH 02/16] Update preset-es2015 `spec` expected case. --- .../test/fixtures/preset-options/spec/expected.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-preset-es2015/test/fixtures/preset-options/spec/expected.js b/packages/babel-preset-es2015/test/fixtures/preset-options/spec/expected.js index 1ff90ab1ed8c..6295df920a0f 100644 --- a/packages/babel-preset-es2015/test/fixtures/preset-options/spec/expected.js +++ b/packages/babel-preset-es2015/test/fixtures/preset-options/spec/expected.js @@ -1,7 +1,7 @@ "use strict"; var _this = undefined; -"1" + String(a); +"1".concat(a); (function () { babelHelpers.newArrowCheck(this, _this); From 13e5834871bb64aa03d016c38457ea40732114a6 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Tue, 30 May 2017 22:53:50 +0300 Subject: [PATCH 03/16] Prevent array mutability by replacing `shift`. --- .../src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index eaaa3033771d..7f732b2dc0e3 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -62,14 +62,14 @@ export default function ({ types: t }) { if (!t.isStringLiteral(nodes[0]) && (state.opts.spec || !t.isStringLiteral(nodes[1]))) { nodes.unshift(t.stringLiteral("")); } - let root = nodes.shift(); + let root = nodes[0]; if (state.opts.spec) { if (nodes.length) { - root = buildConcatCallExression(root, nodes); + root = buildConcatCallExression(root, nodes.slice(1)); } } else { - for (let i = 0; i < nodes.length; i++) { + for (let i = 1; i < nodes.length; i++) { root = t.binaryExpression("+", root, nodes[i]); } } From 68df216cab19dd26eadce5e911a32ddd50067f17 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Tue, 30 May 2017 23:29:41 +0300 Subject: [PATCH 04/16] Fix condition for single item. --- .../src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index 7f732b2dc0e3..02bbb1c9ec64 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -65,7 +65,7 @@ export default function ({ types: t }) { let root = nodes[0]; if (state.opts.spec) { - if (nodes.length) { + if (nodes.length > 1) { root = buildConcatCallExression(root, nodes.slice(1)); } } else { From 76a6bc34e3ea3b788af1b6a6b03db14fe53f7d47 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 2 Jun 2017 01:34:50 +0300 Subject: [PATCH 05/16] Group concats to ensure toPrimitive sequence. --- .../src/index.js | 28 +++++++++++++++---- .../fixtures/spec/escape-quotes/expected.js | 2 +- .../test/fixtures/spec/multiple/expected.js | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index 02bbb1c9ec64..15e6463e7c7e 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -1,13 +1,28 @@ export default function ({ types: t }) { - function buildConcatCallExression(target, items) { - return t.callExpression( - t.memberExpression(target, t.identifier("concat")), items - ); + + function buildConcatCallExressions(items) { + return items.reduce(function(left, right) { + const isString = t.isStringLiteral(right); + const canBeInserted = !left._withIdentifier || isString; + + if (t.isCallExpression(left) && canBeInserted) { + 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; @@ -59,14 +74,15 @@ export default function ({ types: t }) { // since `+` is left-to-right associative // ensure the first node is a string if first/second isn't - if (!t.isStringLiteral(nodes[0]) && (state.opts.spec || !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]; if (state.opts.spec) { if (nodes.length > 1) { - root = buildConcatCallExression(root, nodes.slice(1)); + root = buildConcatCallExressions(nodes); } } else { for (let i = 1; i < nodes.length; i++) { diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js index 6ea0de512e9f..bf559e274bc6 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/escape-quotes/expected.js @@ -1 +1 @@ -var t = "'".concat(foo, "' \"", bar, "\""); \ No newline at end of file +var t = "'".concat(foo, "' \"").concat(bar, "\""); \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js index 64d22b2e1d49..a3acee1c67bf 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/multiple/expected.js @@ -1 +1 @@ -var foo = "test ".concat(foo, " ", bar); \ No newline at end of file +var foo = "test ".concat(foo, " ").concat(bar); \ No newline at end of file From b83917fe836dc4e393d8e1e880dd061d4e8207b0 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 2 Jun 2017 01:40:08 +0300 Subject: [PATCH 06/16] Update function test case. --- .../test/fixtures/spec/functions/expected.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js index 976c78de00ff..07f272b45437 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js @@ -1 +1 @@ -var foo = "test ".concat(_.test(foo), " ", bar); \ No newline at end of file +var foo = "test ".concat(_.test(foo), " ").concat(bar) \ No newline at end of file From b81996d4d3665b062d7b32ab9da0a379491c6282 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 2 Jun 2017 01:49:12 +0300 Subject: [PATCH 07/16] Add semi for function test case. --- .../test/fixtures/spec/functions/expected.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js index 07f272b45437..4f675ef40e73 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/functions/expected.js @@ -1 +1 @@ -var foo = "test ".concat(_.test(foo), " ").concat(bar) \ No newline at end of file +var foo = "test ".concat(_.test(foo), " ").concat(bar); \ No newline at end of file From 30fd06ece3b29dd238bf4515052331f4d5b98b20 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 2 Jun 2017 11:43:02 +0300 Subject: [PATCH 08/16] Simplify concat call expressions creating. --- .../src/index.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index 15e6463e7c7e..f78213b0a7e3 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -2,20 +2,14 @@ export default function ({ types: t }) { function buildConcatCallExressions(items) { return items.reduce(function(left, right) { - const isString = t.isStringLiteral(right); - const canBeInserted = !left._withIdentifier || isString; - - if (t.isCallExpression(left) && canBeInserted) { - left._withIdentifier = left._withIdentifier || !isString; + if (t.isCallExpression(left) && t.isLiteral(right)) { left.arguments.push(right); return left; } - const nextCall = t.callExpression( + return t.callExpression( t.memberExpression(left, t.identifier("concat")), [right] ); - nextCall._withIdentifier = !isString; - return nextCall; }); } From 09321149b04f4aecc17c1dc844e7443984f0d0a9 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 2 Jun 2017 12:27:50 +0300 Subject: [PATCH 09/16] Fix some cases with multiple idengifiers. --- .../src/index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index f78213b0a7e3..ac6f398585d6 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -1,8 +1,15 @@ export default function ({ types: t }) { function buildConcatCallExressions(items) { + let avail = true; return items.reduce(function(left, right) { - if (t.isCallExpression(left) && t.isLiteral(right)) { + let canBeInserted = t.isLiteral(right); + + if (!canBeInserted && avail) { + canBeInserted = true; + avail = false; + } + if (t.isCallExpression(left) && canBeInserted) { left.arguments.push(right); return left; } From ae5e9d0b47757c8c2c52bc3bcc8a7050958e40da Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 2 Jun 2017 12:28:05 +0300 Subject: [PATCH 10/16] Add test case with different literals. --- .../test/fixtures/spec/literals/actual.js | 0 .../test/fixtures/spec/literals/expected.js | 1 + 2 files changed, 1 insertion(+) create mode 100644 packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/actual.js create mode 100644 packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/expected.js diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/actual.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/actual.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/expected.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/expected.js new file mode 100644 index 000000000000..e2fb8e51c675 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/expected.js @@ -0,0 +1 @@ +var foo = "".concat(1, f, "oo", true).concat(b, "ar", 0).concat(baz); \ No newline at end of file From 6dd693a627e35bd36f1bcd9d8433eb8fe0da60b3 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 2 Jun 2017 13:13:04 +0300 Subject: [PATCH 11/16] Add test case for `Symbol()` and toPrimitive order --- .../test/fixtures/spec/order/exec.js | 24 +++++++++++++++++++ .../test/fixtures/spec/symbol/exec.js | 3 +++ 2 files changed, 27 insertions(+) create mode 100644 packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/order/exec.js create mode 100644 packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/symbol/exec.js diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/order/exec.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/order/exec.js new file mode 100644 index 000000000000..895677e3a27a --- /dev/null +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/order/exec.js @@ -0,0 +1,24 @@ +const calls = []; + +` + ${ + calls.push(1), + { + [Symbol.toPrimitive](){ + calls.push(2); + return 'foo'; + } + } + } + ${ + calls.push(3), + { + [Symbol.toPrimitive](){ + calls.push(4); + return 'bar'; + } + } + } +`; + +assert.deepEqual(calls, [1, 2, 3, 4]); diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/symbol/exec.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/symbol/exec.js new file mode 100644 index 000000000000..39316c78efa6 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/symbol/exec.js @@ -0,0 +1,3 @@ +const fn = () => `${Symbol()}`; + +assert.throws(fn, TypeError); From 291a308a1c44e7b20388aec2b91ab31e197440d4 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 2 Jun 2017 13:14:30 +0300 Subject: [PATCH 12/16] Add actual literal case. --- .../test/fixtures/spec/literals/actual.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/actual.js b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/actual.js index e69de29bb2d1..a6e9c4a8362b 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/actual.js +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/literals/actual.js @@ -0,0 +1 @@ +var foo = `${1}${f}oo${true}${b}ar${0}${baz}`; \ No newline at end of file From dca18649ae03fba113fbb634d81579c00bfe4678 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Sat, 3 Jun 2017 01:57:13 +0300 Subject: [PATCH 13/16] Add minNodeVersion to template literals order. --- .../test/fixtures/spec/order/options.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/order/options.json diff --git a/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/order/options.json b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/order/options.json new file mode 100644 index 000000000000..7d8c3c204cc8 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-template-literals/test/fixtures/spec/order/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "6.0.0" +} From 8e1ca42a256fbc7681272655a311c67301c331ba Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Sat, 3 Jun 2017 14:44:32 +0300 Subject: [PATCH 14/16] Flip the logical expression. --- .../src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index ac6f398585d6..ab45375eff45 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -9,7 +9,7 @@ export default function ({ types: t }) { canBeInserted = true; avail = false; } - if (t.isCallExpression(left) && canBeInserted) { + if (canBeInserted && t.isCallExpression(left)) { left.arguments.push(right); return left; } From c360dc3efcaf78c977c1e533f32c94e55d3a4dfa Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Mon, 5 Jun 2017 15:31:43 +0300 Subject: [PATCH 15/16] Update README for template literals spec option. --- .../README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/README.md b/packages/babel-plugin-transform-es2015-template-literals/README.md index be3ec6531912..e8b79a74b76b 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/README.md +++ b/packages/babel-plugin-transform-es2015-template-literals/README.md @@ -75,16 +75,16 @@ In loose mode, tagged template literal objects aren't frozen. `boolean`, defaults to `false`. -This option combines all template literal expressions and quasis with `String.prototype.concat`. +This option combines all template literal expressions and quasis with `String.prototype.concat`. It allows to pass the correct hint to `ToPrimitive` operation and to throw if template literal expression is a `Symbol()`. See [babel/babel#5791](https://github.com/babel/babel/pull/5791). **In** ```javascript -`foo${bar}`; +`foo${bar}baz${quux}${1}`; ``` **Out** ```javascript -"foo".concat(bar); +"foo".concat(bar, "baz").concat(quux, 1); ``` From 27942abac666217ddbcc4d6c08ec35406c93a1f5 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Mon, 5 Jun 2017 08:52:14 -0400 Subject: [PATCH 16/16] docs [skip ci] --- .../babel-plugin-transform-es2015-template-literals/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-es2015-template-literals/README.md b/packages/babel-plugin-transform-es2015-template-literals/README.md index e8b79a74b76b..cdd6654c1e57 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/README.md +++ b/packages/babel-plugin-transform-es2015-template-literals/README.md @@ -75,7 +75,7 @@ In loose mode, tagged template literal objects aren't frozen. `boolean`, defaults to `false`. -This option combines all template literal expressions and quasis with `String.prototype.concat`. It allows to pass the correct hint to `ToPrimitive` operation and to throw if template literal expression is a `Symbol()`. See [babel/babel#5791](https://github.com/babel/babel/pull/5791). +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). **In**