From d63f8823fe2dda44041991962d5cd36161106df4 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Sun, 30 Oct 2016 21:43:33 +0100 Subject: [PATCH] Fix array and object patterns in DCE Related previous fix - https://github.com/babel/babili/pull/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) --- .../__tests__/dead-code-elimination-test.js | 29 +++++++++++++++++++ .../src/index.js | 4 +++ 2 files changed, 33 insertions(+) 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 1f803d8bf..e91996c9f 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 {