From 06958bc11e50038da9d1c03753d579e1da78c647 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Tue, 11 Jul 2017 12:28:32 +0200 Subject: [PATCH 1/2] Detect duplicate declaration during one-use replacement Fix #611 --- .../__tests__/dead-code-elimination-test.js | 14 ++++++++++++++ .../src/index.js | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) 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 3e4476b64..abecf1de3 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 @@ -2448,4 +2448,18 @@ describe("dce-plugin", () => { } ` ); + + thePlugin( + "should fix issue#611 - transforming fn decl to expr", + ` + function foo() { + function count() { + let count = 1; + bar(count); + return count; + } + return count; + } + ` + ); }); 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 9f3bdbfc2..e01de55e5 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js +++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js @@ -400,7 +400,8 @@ module.exports = ({ types: t, traverse }) => { const replaced = replace(binding.referencePaths[0], { binding, scope, - replacement + replacement, + replacementPath }); if (replaced) { @@ -936,7 +937,7 @@ module.exports = ({ types: t, traverse }) => { } function replace(path, options) { - const { replacement, scope, binding } = options; + const { replacement, replacementPath, scope, binding } = options; // Same name, different binding. if (scope.getBinding(path.node.name) !== binding) { @@ -976,6 +977,20 @@ module.exports = ({ types: t, traverse }) => { return; } + // https://github.com/babel/babili/issues/611 + // this is valid only for FunctionDeclaration where we convert + // function declaration to expression in the next step + if (replacementPath.isFunctionDeclaration()) { + const id = replacementPath.get("id"); + if (id.isIdentifier()) { + for (let name in replacementPath.scope.bindings) { + if (name === id.node.name) { + return; + } + } + } + } + // https://github.com/babel/babili/issues/130 if (!t.isExpression(replacement)) { t.toExpression(replacement); From 58583b1a399aac71cbf6b06daa962d319ecce471 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Tue, 11 Jul 2017 13:00:19 +0200 Subject: [PATCH 2/2] FnDecls always have id --- .../src/index.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 e01de55e5..796c08787 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js +++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js @@ -981,12 +981,10 @@ module.exports = ({ types: t, traverse }) => { // this is valid only for FunctionDeclaration where we convert // function declaration to expression in the next step if (replacementPath.isFunctionDeclaration()) { - const id = replacementPath.get("id"); - if (id.isIdentifier()) { - for (let name in replacementPath.scope.bindings) { - if (name === id.node.name) { - return; - } + const fnName = replacementPath.get("id").node.name; + for (let name in replacementPath.scope.bindings) { + if (name === fnName) { + return; } } }