diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index f22a4c20d..0fc503c42 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -37,9 +37,9 @@ const plugin = require("../src/index"); const comparisonPlugin = require("../../babel-plugin-transform-simplify-comparison-operators/src"); const unpad = require("../../../utils/unpad"); -function transform(code) { +function transform(code, options) { return babel.transform(code, { - plugins: [plugin], + plugins: [ [plugin, options] ], }).code; } @@ -2643,4 +2643,19 @@ describe("simplify-plugin", () => { } expect(transform(source)).toBe(expected); }); + + it("should convert call expressions with single string param to tagged templates", () => { + const source = unpad(` + foo("bar"); + bar.baz("", ""); + bar(a); + baz(5); + baz.foo.bar(4)("foo"); + baz("bar", "")(5); + (() => {})("bar"); + quux(foo(\`as\${d}f\`)); + `); + const expected = "foo`bar`, bar.baz(\"\", \"\"), bar(a), baz(5), baz.foo.bar(4)`foo`, baz(\"bar\", \"\")(5), (() => {})`bar`, quux(foo(`as${ d }f`));"; + expect(transform(source, { callsToTags: true })).toBe(expected); + }); }); diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index aafec29f0..c840e86da 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -117,6 +117,37 @@ module.exports = ({ types: t }) => { }*/ // }, + CallExpression: { + enter: [ + // Converts + // foo("bar") -> foo`bar` + function CallExprToTaggedTemplate(path, { + opts: { + ecmaVersion = 2015, + callsToTags = false + } + }) { + if (ecmaVersion < 2015 || !callsToTags) return; + + const args = path.get("arguments"); + + if (args.length !== 1) return; + if (!args[0].isStringLiteral()) return; + + const callee = path.get("callee"); + + path.replaceWith( + t.taggedTemplateExpression( + callee.node, + t.templateLiteral( + [ t.templateElement({ raw: args[0].node.value }, true) ], [] + ) + ) + ); + } + ] + }, + UnaryExpression: { enter: [ diff --git a/packages/babel-preset-babili/src/index.js b/packages/babel-preset-babili/src/index.js index 1ae6ba704..45f033dff 100644 --- a/packages/babel-preset-babili/src/index.js +++ b/packages/babel-preset-babili/src/index.js @@ -90,6 +90,10 @@ function preset(context, _opts = {}) { proxy("keepClassName", [ optionsMap.mangle, optionsMap.deadcode + ]), + + proxy("ecmaVersion", [ + optionsMap.simplify ]) ], "some"