Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[fix #574] deopt when binding is present in diff scope #597

Merged
merged 4 commits into from
Aug 7, 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 @@ -2462,4 +2462,14 @@ describe("dce-plugin", () => {
}
`
);

thePlugin(
"should deopt when binding is on different scope - issue #574",
`
function foo(v) {
if (v) var w = 10;
if (w) console.log("hello", v);
}
`
);
});
47 changes: 39 additions & 8 deletions packages/babel-plugin-minify-dead-code-elimination/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,13 @@ module.exports = ({ types: t, traverse }) => {
) {
continue;
} else if (binding.path.isVariableDeclarator()) {
const declaration = binding.path.parentPath;
const maybeBlockParent = declaration.parentPath;
if (
binding.path.parentPath.parentPath &&
binding.path.parentPath.parentPath.isForXStatement()
maybeBlockParent &&
maybeBlockParent.isForXStatement({
left: declaration.node
})
) {
// Can't remove if in a for-in/for-of/for-await statement `for (var x in wat)`.
continue;
Expand Down Expand Up @@ -293,14 +297,10 @@ module.exports = ({ types: t, traverse }) => {
let replacementPath = binding.path;
let isReferencedBefore = false;

if (binding.referencePaths.length > 1) {
Copy link
Member Author

Choose a reason for hiding this comment

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

unnecessary check.

throw new Error("Expected only one reference");
}
const refPath = binding.referencePaths[0];

if (t.isVariableDeclarator(replacement)) {
const _prevSiblings = prevSiblings(replacementPath);

// traverse ancestors of a reference checking if it's before declaration
forEachAncestor(refPath, ancestor => {
if (_prevSiblings.indexOf(ancestor) > -1) {
Expand All @@ -309,7 +309,7 @@ module.exports = ({ types: t, traverse }) => {
});

// deopt if reference is in different scope than binding
// since we don't know if it's sync or async execition
// since we don't know if it's sync or async execution
// (i.e. whether value has been assigned to a reference or not)
if (isReferencedBefore && refPath.scope !== binding.scope) {
continue;
Expand Down Expand Up @@ -774,8 +774,39 @@ module.exports = ({ types: t, traverse }) => {
const evalResult = test.evaluate();
const isPure = test.isPure();

const replacements = [];
const binding = path.scope.getBinding(test.node.name);

// Ref - https://github.com/babel/babili/issues/574
// deopt if var is declared in other scope
// if (a) { var b = blahl;} if (b) { //something }
if (
binding &&
binding.path.parentPath.isVariableDeclaration({ kind: "var" })
) {
let ifStatementParent = null;

const fnParent =
binding.path.getFunctionParent() ||
binding.path.getProgramParent();

forEachAncestor(binding.path.parentPath, parent => {
if (fnParent === parent) return;
if (parent.isIfStatement()) {
ifStatementParent = parent;
}
});

if (
ifStatementParent &&
binding.referencePaths.some(
ref => !ref.isDescendant(ifStatementParent)
)
) {
return;
}
}

const replacements = [];
if (evalResult.confident && !isPure && test.isSequenceExpression()) {
replacements.push(
t.expressionStatement(extractSequenceImpure(test))
Expand Down