Skip to content

Commit

Permalink
fix builtins - bail on polyfilled members (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam authored and boopathi committed Dec 20, 2017
1 parent 6ae655c commit 169f874
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Math["a"] = "blah";
Math.a();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Math["a"] = "blah";
Math.a();
26 changes: 25 additions & 1 deletion packages/babel-plugin-minify-builtins/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ module.exports = function({ types: t }) {
const context = this;

const collectVisitor = {
AssignmentExpression(path) {
const left = path.get("left");

// Should bail and not run the plugin
// when builtin is polyfilled
if (t.isMemberExpression(left) && isBuiltInComputed(left)) {
let parent = path;
do {
parent.stop();
} while ((parent = parent.parentPath));
}
},

MemberExpression(path) {
if (path.parentPath.isCallExpression()) {
return;
Expand All @@ -46,7 +59,7 @@ module.exports = function({ types: t }) {
return;
}

// computed property should be not optimized
// computed property should not be optimized
// Math[max]() -> Math.max()
if (!isComputed(callee) && isBuiltin(callee)) {
const result = evaluate(path, { tdz: context.tdz });
Expand Down Expand Up @@ -115,6 +128,17 @@ module.exports = function({ types: t }) {
return result;
}

function isBuiltInComputed(memberExpr) {
const { node } = memberExpr;
const { object, computed } = node;

return (
computed &&
t.isIdentifier(object) &&
VALID_CALLEES.indexOf(object.name) >= 0
);
}

function isBuiltin(memberExpr) {
const { object, property } = memberExpr.node;

Expand Down

0 comments on commit 169f874

Please sign in to comment.