From e93d9940669b37bdf4183e84ad6ae6fb08f66943 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Thu, 3 Nov 2016 21:31:31 +0100 Subject: [PATCH] Remove Empty Functions in block (close #247) --- .../__tests__/dead-code-elimination-test.js | 18 +++++++++++++++++- .../src/index.js | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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 9a9d7a22f..4eb96d171 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 @@ -82,7 +82,7 @@ describe("dce-plugin", () => { const source = unpad(` function foo(p) { return 1; - }; + } function bar(q) { return q + 1; } @@ -2193,4 +2193,20 @@ describe("dce-plugin", () => { } })).toBe(expected); }); + it("should remove empty statements when children of block", () => { + const source = unpad(` + (function () { + function foo() {}; + function bar() {}; + function baz() {}; + function ban() {}; + function quux() {}; + function cake() {}; + })(); + `); + const expected = unpad(` + (function () {})(); + `); + expect(transform(source)).toBe(expected); + }); }); 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 e91996c9f..28d7ed53b 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js +++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js @@ -678,6 +678,11 @@ module.exports = ({ types: t, traverse }) => { return { name: "minify-dead-code-elimination", visitor: { + EmptyStatement(path) { + if (path.parentPath.isBlockStatement() || path.parentPath.isProgram()) { + path.remove(); + } + }, Program(path, { opts: { // set defaults