Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constant-folding: Fix some literal types used in .join(), closes #612 #613

Merged
merged 4 commits into from
Aug 15, 2017
Merged
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 @@ -103,6 +103,10 @@ 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");
[\`a\`, \`c\`].join('b');

[1, 2, 3].length;
[1, 2, 3][1];
Expand Down Expand Up @@ -134,6 +138,10 @@ describe("constant-folding-plugin", () => {
"1,2,3";
"a,b,c";
"a@b@c";
"/1";
[/xyz/im, true].join("abc");
[\`a\${xyz}\`].join("1");
"abc";

3;
2;
Expand Down
16 changes: 8 additions & 8 deletions packages/babel-plugin-minify-constant-folding/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ function getName(member) {
}

function swap(path, member, handlers, ...args) {
const key = getName(member);
const key = getName(member.node);
if (key === undefined) return;
let handler = handlers[key];
if (
typeof handler !== "function" ||
!Object.hasOwnProperty.call(handlers, key)
) {
if (typeof handlers[FALLBACK_HANDLER] === "function") {
handler = handlers[FALLBACK_HANDLER].bind(member.object, key);
handler = handlers[FALLBACK_HANDLER].bind(member.get("object"), key);
} else {
return false;
}
}
const replacement = handler.apply(member.object, args);
const replacement = handler.apply(member.get("object"), args);
if (replacement) {
path.replaceWith(replacement);
return true;
Expand Down Expand Up @@ -168,18 +168,18 @@ module.exports = babel => {
},
CallExpression(path) {
const { node } = path;
const { callee: member } = node;
const member = path.get("callee");
if (t.isMemberExpression(member)) {
const helpers = replacements[member.object.type];
const helpers = replacements[member.node.object.type];
if (!helpers || !helpers.calls) return;
swap(path, member, helpers.calls, ...node.arguments);
}
},
MemberExpression(path) {
const { node: member } = path;
const helpers = replacements[member.object.type];
const { node } = path;
const helpers = replacements[node.object.type];
if (!helpers || !helpers.members) return;
swap(path, member, helpers.members);
swap(path, path, helpers.members);
}
}
};
Expand Down
37 changes: 19 additions & 18 deletions packages/babel-plugin-minify-constant-folding/src/replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = ({ types: t }) => {
function defaultZero(cb) {
return function(i = t.numericLiteral(0), ...args) {
if (t.isNumericLiteral(i)) {
return cb.call(this, this, i.value, ...args);
return cb.call(this.node, this.node, i.value, ...args);
}
};
}
Expand All @@ -23,57 +23,58 @@ module.exports = ({ types: t }) => {
ArrayExpression: {
members: {
length() {
if (this.elements.some(el => t.isSpreadElement(el))) {
if (this.node.elements.some(el => t.isSpreadElement(el))) {
return;
}
return t.numericLiteral(this.elements.length);
return t.numericLiteral(this.node.elements.length);
},
[FALLBACK_HANDLER](i) {
if (this.elements.some(el => t.isSpreadElement(el))) {
if (this.node.elements.some(el => t.isSpreadElement(el))) {
return;
}
if (typeof i === "number" || i.match(/^\d+$/)) {
return this.elements[i] || undef;
return this.node.elements[i] || undef;
}
}
},
calls: {
join(sep = t.stringLiteral(",")) {
if (!t.isStringLiteral(sep)) return;
let bad = false;
const str = this.elements
const str = this.get("elements")
.map(el => {
if (!t.isLiteral(el)) {
const evaled = el.evaluate();
if (!evaled.confident) {
bad = true;
return;
}
return el.value;
return evaled.value;
})
.join(sep.value);
return bad ? undefined : t.stringLiteral(str);
},
push(...args) {
return t.numericLiteral(this.elements.length + args.length);
return t.numericLiteral(this.node.elements.length + args.length);
},
shift() {
if (this.elements.length === 0) {
if (this.node.elements.length === 0) {
return undef;
}
return t.numericLiteral(this.elements.length - 1);
return t.numericLiteral(this.node.elements.length - 1);
},
slice(start = t.numericLiteral(0), end) {
if (!t.isNumericLiteral(start) || (end && !t.isNumericLiteral(end))) {
return;
}
return t.arrayExpression(
this.elements.slice(start.value, end && end.value)
this.node.elements.slice(start.value, end && end.value)
);
},
pop() {
return this.elements[this.elements.length - 1] || undef;
return this.node.elements[this.node.elements.length - 1] || undef;
},
reverse() {
return t.arrayExpression(this.elements.reverse());
return t.arrayExpression(this.node.elements.reverse());
},
splice(start, end, ...args) {
if (!t.isNumericLiteral(start) || (end && !t.isNumericLiteral(end))) {
Expand All @@ -83,19 +84,19 @@ module.exports = ({ types: t }) => {
args.unshift(end.value);
}
return t.arrayExpression(
this.elements.slice().splice(start.value, ...args)
this.node.elements.slice().splice(start.value, ...args)
);
}
}
},
StringLiteral: {
members: {
length() {
return t.numericLiteral(this.value.length);
return t.numericLiteral(this.node.value.length);
},
[FALLBACK_HANDLER](i) {
if (typeof i === "number" || i.match(/^\d+$/)) {
const ch = this.value[i];
const ch = this.node.value[i];
return ch ? t.stringLiteral(ch) : undef;
}
}
Expand All @@ -111,7 +112,7 @@ module.exports = ({ types: t }) => {
}
if (realSep !== null) {
return t.arrayExpression(
this.value.split(realSep).map(str => t.stringLiteral(str))
this.node.value.split(realSep).map(str => t.stringLiteral(str))
);
}
},
Expand Down