diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js index 7ab49dc13..9a9d7a22f 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js +++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js @@ -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() { diff --git a/packages/babel-plugin-minify-dead-code-elimination/src/index.js b/packages/babel-plugin-minify-dead-code-elimination/src/index.js index 0fbffb2c7..224c03cd9 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js +++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js @@ -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 {