Skip to content
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 @@ -246,4 +246,12 @@ describe("constant-folding-plugin", () => {
}
`
);

thePlugin(
"should not fold array literals with spread element",
`
[...bar].reverse();
[...foo].join("a");
`
);
});
32 changes: 21 additions & 11 deletions packages/babel-plugin-minify-constant-folding/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ function getName(member) {

function swap(path, member, handlers, ...args) {
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.get("object"), key);
} else {
return false;
}
if (key === void 0) return false;

let handler;
if (hop(handlers, key) && typeof handlers[key] === "function") {
handler = handlers[key];
} else if (typeof handlers[FALLBACK_HANDLER] === "function") {
handler = handlers[FALLBACK_HANDLER].bind(member.get("object"), key);
} else {
return false;
}

const replacement = handler.apply(member.get("object"), args);
if (replacement) {
path.replaceWith(replacement);
Expand Down Expand Up @@ -172,6 +171,13 @@ module.exports = babel => {
if (t.isMemberExpression(member)) {
const helpers = replacements[member.node.object.type];
if (!helpers || !helpers.calls) return;
// find if the input can be constant folded
if (
typeof helpers.canReplace === "function" &&
!helpers.canReplace.call(member.get("object"))
) {
return;
}
swap(path, member, helpers.calls, ...node.arguments);
}
},
Expand All @@ -184,3 +190,7 @@ module.exports = babel => {
}
};
};

function hop(o, key) {
return Object.prototype.hasOwnProperty.call(o, key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,24 @@ module.exports = ({ types: t }) => {
};
}

function hasSpread(node) {
return node.elements.some(el => t.isSpreadElement(el));
}

return {
ArrayExpression: {
canReplace() {
return !hasSpread(this.node);
},
members: {
length() {
if (this.node.elements.some(el => t.isSpreadElement(el))) {
if (hasSpread(this.node)) {
return;
}
return t.numericLiteral(this.node.elements.length);
},
[FALLBACK_HANDLER](i) {
if (this.node.elements.some(el => t.isSpreadElement(el))) {
if (hasSpread(this.node)) {
return;
}
if (typeof i === "number" || i.match(/^\d+$/)) {
Expand All @@ -51,7 +58,7 @@ module.exports = ({ types: t }) => {
return evaled.value;
})
.join(sep.value);
return bad ? undefined : t.stringLiteral(str);
return bad ? void 0 : t.stringLiteral(str);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this use void 0 instead of undefined?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use void 0 everywhere in the project. Just sticking to that.

},
push(...args) {
return t.numericLiteral(this.node.elements.length + args.length);
Expand Down