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

support for for..of/for..await in DCE #214

Merged
merged 3 commits into from
Oct 22, 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.
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 @@ -3,10 +3,10 @@ jest.autoMockOff();
const babel = require("babel-core");
const unpad = require("../../../utils/unpad");

function transform(code, options) {
return babel.transform(code, {
function transform(code, options, babelOpts) {
return babel.transform(code, Object.assign({}, {
plugins: [[require("../src/index"), options]],
}).code.trim();
}, babelOpts)).code.trim();
}

describe("dce-plugin", () => {
Expand Down Expand Up @@ -2115,4 +2115,29 @@ describe("dce-plugin", () => {

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

it("should not remove var from for..in/for..of statements", () => {
const source = unpad(`
function foo() {
for (var i in x) console.log("foo");
for (var j of y) console.log("foo");
}
`);
const expected = source;
expect(transform(source)).toBe(expected);
});

it("should not remove var from for..await statements", () => {
const source = unpad(`
async function foo() {
for await (var x of y) console.log("bar");
}
`);
const expected = source;
expect(transform(source, {}, {
parserOpts: {
plugins: ["asyncGenerators"]
}
})).toBe(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ module.exports = ({ types: t, traverse }) => {
continue;
} else if (binding.path.isVariableDeclarator()) {
if (binding.path.parentPath.parentPath &&
binding.path.parentPath.parentPath.isForInStatement()
binding.path.parentPath.parentPath.isForXStatement()
) {
// Can't remove if in a for in statement `for (var x in wat)`.
// Can't remove if in a for-in/for-of/for-await statement `for (var x in wat)`.
continue;
}
} else if (!scope.isPure(binding.path.node)) {
Expand Down