diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js index 2cacfd768..3ddbea7f8 100644 --- a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js +++ b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js @@ -103,6 +103,9 @@ describe("constant-folding-plugin", () => { [1, 2, 3].join(); ["a", "b", "c"].join(); ["a", "b", "c"].join("@"); + [null, 1].join("/"); + [/xyz/im, true].join("abc"); + [\`a\${xyz}\`].join("1"); [1, 2, 3].length; [1, 2, 3][1]; @@ -134,6 +137,9 @@ describe("constant-folding-plugin", () => { "1,2,3"; "a,b,c"; "a@b@c"; + "/1"; + "/xyz/imabctrue"; + [\`a\${xyz}\`].join("1"); 3; 2; diff --git a/packages/babel-plugin-minify-constant-folding/src/replacements.js b/packages/babel-plugin-minify-constant-folding/src/replacements.js index 0cbc591b0..429683746 100644 --- a/packages/babel-plugin-minify-constant-folding/src/replacements.js +++ b/packages/babel-plugin-minify-constant-folding/src/replacements.js @@ -43,11 +43,21 @@ module.exports = ({ types: t }) => { let bad = false; const str = this.elements .map(el => { - if (!t.isLiteral(el)) { - bad = true; - return; + if (t.isRegExpLiteral(el)) { + return `/${el.pattern}/${el.flags}`; } - return el.value; + if (t.isNullLiteral(el)) { + return null; + } + if ( + t.isStringLiteral(el) || + t.isBooleanLiteral(el) || + t.isNumericLiteral(el) + ) { + return el.value; + } + bad = true; + return; }) .join(sep.value); return bad ? undefined : t.stringLiteral(str);