Skip to content

Commit

Permalink
constant-folding: Fix some literal types used in .join()
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Jul 2, 2017
1 parent 64bb89f commit 481d9f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions packages/babel-plugin-minify-constant-folding/src/replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 481d9f9

Please sign in to comment.