Skip to content

Commit

Permalink
lift the declarations without mutating the node[fix #402] (#403)
Browse files Browse the repository at this point in the history
* lift the declarations without mutating the node[fix #402]

* add test case for capturing regression
  • Loading branch information
vigneshshanmugam authored and boopathi committed Feb 2, 2017
1 parent d7324f7 commit 1211936
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Expand Up @@ -24,9 +24,7 @@ module.exports = function({ types: t }) {
return;
}

init.node.declarations = init.node.declarations.concat(
firstNode.id
);
init.pushContainer("declarations", t.variableDeclarator(firstNode.id));

body[0].replaceWith(t.assignmentExpression(
"=",
Expand Down
19 changes: 19 additions & 0 deletions packages/babel-preset-babili/__tests__/preset-tests.js
Expand Up @@ -75,4 +75,23 @@ describe("preset", () => {
`);
expect(transform(source)).toBe(expected);
});

it("should fix issue#402 - lifting var decl & DCE", () => {
const source = unpad(`
function a() {
if (0) {
for (var i;;) {
var something = 5;
}
}
}
a();
`);
const expected = unpad(`
function a() {}
a();
`);
expect(transform(source)).toBe(expected);
});

});

0 comments on commit 1211936

Please sign in to comment.