Skip to content

Commit

Permalink
Fix array and object patterns in DCE (#233)
Browse files Browse the repository at this point in the history
Related previous fix - #155 does
the same thing except for a different case - the binding.constant is
true for that and for the current fix binding is not referenced but is
an array pattern or an object pattern.

+ (close #232)
  • Loading branch information
boopathi authored and hzoo committed Oct 30, 2016
1 parent 371a7e2 commit e6fc16e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,35 @@ describe("dce-plugin", () => {
expect(transform(source)).toBe(expected);
});

// https://github.com/babel/babili/issues/232
it("should fix issue#232 - array patterns and object patterns with non constant init", () => {
const source = unpad(`
const a = {
lol: input => {
const [hello, world] = input.split('|');
if (hello === 't' || hello === 'top') {
return 'top';
}
return 'bottom';
}
};
`);
const expected = source;
expect(transform(source)).toBe(expected);
});

// https://github.com/babel/babili/issues/232
it("should fix issue#232 - array & object patterns with non-constant init", () => {
const source = unpad(`
function foo() {
const { bar1, bar2 } = baz();
return bar1;
}
`);
const expected = source;
expect(transform(source)).toBe(expected);
});

it("should preserve variabledeclarations(var) after completion statements", () => {
const source = unpad(`
function foo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ module.exports = ({ types: t, traverse }) => {
if (binding.path.parentPath.node.declarations.length !== 1) {
continue;
}
// Bail out for ArrayPattern and ObjectPattern
if (!binding.path.get("id").isIdentifier()) {
continue;
}

binding.path.parentPath.replaceWith(binding.path.node.init);
} else {
Expand Down

0 comments on commit e6fc16e

Please # to comment.