Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix array and object patterns in DCE (close #232) #233

Merged
merged 1 commit into from
Oct 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.
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 @@ -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