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

[DCE] Fix var hoisting #169

Merged
merged 1 commit into from
Sep 30, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1999,4 +1999,39 @@ describe("dce-plugin", () => {
const expected = source;
expect(transform(source)).toBe(expected);
});

it("should preserve variabledeclarations(var) after completion statements", () => {
const source = unpad(`
function foo() {
a = 1;
return a;
var a;
}
`);

const expected = source;

expect(transform(source)).toBe(expected);
});

it("should NOT preserve variabledeclarations(let) after completion statements", () => {
const source = unpad(`
function foo() {
a = 1;
b = 2;
return a + b;
let a, b;
}
`);

const expected = unpad(`
function foo() {
a = 1;
b = 2;
return a + b;
}
`);

expect(transform(source)).toBe(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ module.exports = ({ types: t, traverse }) => {
continue;
}

if (purge && !p.isFunctionDeclaration()) {
if (purge && !canExistAfterCompletion(p)) {
removeOrVoid(p);
}
}
Expand All @@ -299,7 +299,7 @@ module.exports = ({ types: t, traverse }) => {

// Not last in it's block? (See BlockStatement visitor)
if (path.container.length - 1 !== path.key &&
!path.getSibling(path.key + 1).isFunctionDeclaration() &&
!canExistAfterCompletion(path.getSibling(path.key + 1)) &&
path.parentPath.isBlockStatement()
) {
// This is probably a new oppurtinity by some other transform
Expand Down Expand Up @@ -900,4 +900,10 @@ module.exports = ({ types: t, traverse }) => {
};
}
}

// things that are hoisted
function canExistAfterCompletion(path) {
Copy link
Member

Choose a reason for hiding this comment

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

So basically anything that doesn't conform to TDZ? What about function?

a = 1;
return a;
function a() { }

Copy link
Member Author

@boopathi boopathi Sep 29, 2016

Choose a reason for hiding this comment

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

That test is already covered in name=should be fine with fun decl after return if that's what you meant.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ah, perfect!

return path.isFunctionDeclaration()
|| path.isVariableDeclaration({ kind: "var" });
}
};