Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
});
});
31 changes: 31 additions & 0 deletions packages/babel-plugin-minify-simplify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [

Expand Down
4 changes: 4 additions & 0 deletions packages/babel-preset-babili/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ function preset(context, _opts = {}) {
proxy("keepClassName", [
optionsMap.mangle,
optionsMap.deadcode
]),

proxy("ecmaVersion", [
optionsMap.simplify
])
],
"some"
Expand Down