Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[DCE] Fix removal of ArrayPattern #622

Merged
merged 1 commit into from
Jul 8, 2017
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 @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently getting the error Cannot read property 'declarations' of null here. Doing steps to reproduce is a lot of work (big repo). So I thought I would take a wild stab in the dark and ask if you could see that it would in any way make sense to add a guard here for if binding.path.parentPath.node is null? If that doesn't make sense I apologise for wasting your time...

) {
// 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