Skip to content

Commit

Permalink
[DCE] Fix removal of ArrayPattern (#622)
Browse files Browse the repository at this point in the history
+ (Fix 617)
  • Loading branch information
boopathi authored Jul 8, 2017
1 parent 2bfa549 commit 83f8819
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2426,4 +2426,26 @@ describe("dce-plugin", () => {
}
`
);

thePlugin(
"should bail out for Array and Object Pattern - fix issue#617",
`
function foo(arr) {
let [a, b] = arr;
console.log(a);
}
`
);

thePlugin(
"should bail out for Array and Object Pattern - fix issue#617",
`
function foo() {
return getPromise().then(arr => {
let { a, b } = arr;
console.log(a);
});
}
`
);
});
32 changes: 20 additions & 12 deletions packages/babel-plugin-minify-dead-code-elimination/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,29 @@ module.exports = ({ types: t, traverse }) => {
continue;
}

if (
binding.path.isVariableDeclarator() &&
binding.path.node.init &&
!scope.isPure(binding.path.node.init) &&
binding.path.parentPath.node.declarations
) {
if (binding.path.parentPath.node.declarations.length !== 1) {
continue;
}
// Bail out for ArrayPattern and ObjectPattern
if (binding.path.isVariableDeclarator()) {
if (!binding.path.get("id").isIdentifier()) {
// deopt for object and array pattern
continue;
}

binding.path.parentPath.replaceWith(binding.path.node.init);
// if declarator has some impure init expression
// var x = foo();
// => foo();
if (
binding.path.node.init &&
!scope.isPure(binding.path.node.init) &&
binding.path.parentPath.node.declarations
) {
// binding path has more than one declarations
if (binding.path.parentPath.node.declarations.length !== 1) {
continue;
}
binding.path.parentPath.replaceWith(binding.path.node.init);
} else {
updateReferences(binding.path, this);
removeOrVoid(binding.path);
}
} else {
updateReferences(binding.path, this);
removeOrVoid(binding.path);
Expand Down Expand Up @@ -403,7 +411,7 @@ module.exports = ({ types: t, traverse }) => {
}
}
}
}
} // end-for-of
}
},

Expand Down

0 comments on commit 83f8819

Please sign in to comment.