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 @@ -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