Skip to content

Commit

Permalink
fix(dce): extract only binding identifiers during block removal
Browse files Browse the repository at this point in the history
+ Fix #710
  • Loading branch information
boopathi committed Oct 25, 2017
1 parent 3e9821c commit d7a9756
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2516,4 +2516,17 @@ describe("dce-plugin", () => {
}
`
);

thePlugin(
"extractVars should extract only identifiers & ignore ObjectPattern/ArrayPattern",
`
if (false) {
var {x, y} = foo();
var [a, b] = bar();
}
`,
`
var x, y, a, b;
`
);
});
13 changes: 11 additions & 2 deletions packages/babel-plugin-minify-dead-code-elimination/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,11 @@ module.exports = ({ types: t, traverse }) => {

if (path.isVariableDeclaration({ kind: "var" })) {
for (const decl of path.node.declarations) {
declarators.push(t.variableDeclarator(decl.id));
const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id));

declarators.push(
...bindingIds.map(name => t.variableDeclarator(t.identifier(name)))
);
}
} else {
path.traverse({
Expand All @@ -957,7 +961,12 @@ module.exports = ({ types: t, traverse }) => {
if (!isSameFunctionScope(varPath, path)) return;

for (const decl of varPath.node.declarations) {
declarators.push(t.variableDeclarator(decl.id));
const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id));
declarators.push(
...bindingIds.map(name =>
t.variableDeclarator(t.identifier(name))
)
);
}
}
});
Expand Down

0 comments on commit d7a9756

Please sign in to comment.