Skip to content

Commit

Permalink
Bail for binary 'in' expressions - fix #691 (#705)
Browse files Browse the repository at this point in the history
* Bail for binary 'in' expressions - fix #691

* Remove unnecessary code
  • Loading branch information
boopathi authored Oct 4, 2017
1 parent b729a41 commit 3e9821c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2503,4 +2503,17 @@ describe("dce-plugin", () => {
plugins: [[deadcode, { tdz: true }]]
}
);

thePlugin(
"should bail for binary `in` expressions - fix #691",
`
function foo(props) {
let bar = "width" in props;
delete props.width;
if (bar) {
console.log("foo");
}
}
`
);
});
30 changes: 30 additions & 0 deletions packages/babel-plugin-minify-dead-code-elimination/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,43 @@ module.exports = ({ types: t, traverse }) => {
t.isFunction(n) ||
t.isObjectExpression(n) ||
t.isArrayExpression(n);

const isReplacementObj =
isObj(replacement) || some(replacement, isObj);

if (!sharesRoot || (isReplacementObj && mayLoop)) {
continue;
}

// check if it's safe to replace
// To solve https://github.com/babel/minify/issues/691
// Here we bail for property checks using the "in" operator
// This is because - `in` is a side-effect-free operation but the property
// could be deleted between the replacementPath and referencePath
// It is expensive to compute the delete operation and we bail for
// all the binary "in" operations
let inExpression = replacementPath.isBinaryExpression({
operator: "in"
});

if (!inExpression) {
replacementPath.traverse({
Function(path) {
path.skip();
},
BinaryExpression(path) {
if (path.node.operator === "in") {
inExpression = true;
path.stop();
}
}
});
}

if (inExpression) {
continue;
}

const replaced = replace(binding.referencePaths[0], {
binding,
scope,
Expand Down

0 comments on commit 3e9821c

Please sign in to comment.