Skip to content

Commit

Permalink
Fix DCE for ArrayPatterns and ObjectPatterns (#155)
Browse files Browse the repository at this point in the history
+ Bail out for replacement of single use variable inside a function

TODO: maybe a more intelligent approach to replace - for ex,

// from
const [a] = b; return a;
const {a} = b; return a;

// to
return b[0];
return b.a;

+ (Close #151)
  • Loading branch information
boopathi authored and kangax committed Sep 29, 2016
1 parent 738a325 commit 32c809b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1975,4 +1975,28 @@ describe("dce-plugin", () => {
`);
expect(transform(source)).toBe(expected);
});

// https://github.com/babel/babili/issues/151
it("should fix issue#151 - array patterns and object patterns", () => {
const source = unpad(`
const me = lyfe => {
const [swag] = lyfe;
return swag;
};
`);
const expected = source;
expect(transform(source)).toBe(expected);
});

// https://github.com/babel/babili/issues/151
it("should fix issue#151 - array patterns and object patterns 2", () => {
const source = unpad(`
const me = lyfe => {
const [swag, yolo] = lyfe;
return swag && yolo;
};
`);
const expected = source;
expect(transform(source)).toBe(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ module.exports = ({ types: t, traverse }) => {
let replacementPath = binding.path;
if (t.isVariableDeclarator(replacement)) {
replacement = replacement.init;
// Bail out for ArrayPattern and ObjectPattern
// TODO: maybe a more intelligent approach instead of simply bailing out
if (!replacementPath.get("id").isIdentifier()) {
continue;
}
replacementPath = replacementPath.get("init");
}
if (!replacement) {
Expand Down

0 comments on commit 32c809b

Please sign in to comment.