Skip to content

Commit

Permalink
Detect duplicate declaration during one-use replacement (#626)
Browse files Browse the repository at this point in the history
* Detect duplicate declaration during one-use replacement

Fix #611

* FnDecls always have id
  • Loading branch information
boopathi authored and vigneshshanmugam committed Jul 11, 2017
1 parent 4961540 commit 432c858
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
`
);
});
17 changes: 15 additions & 2 deletions packages/babel-plugin-minify-dead-code-elimination/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ module.exports = ({ types: t, traverse }) => {
const replaced = replace(binding.referencePaths[0], {
binding,
scope,
replacement
replacement,
replacementPath
});

if (replaced) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -976,6 +977,18 @@ 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 fnName = replacementPath.get("id").node.name;
for (let name in replacementPath.scope.bindings) {
if (name === fnName) {
return;
}
}
}

// https://github.com/babel/babili/issues/130
if (!t.isExpression(replacement)) {
t.toExpression(replacement);
Expand Down

0 comments on commit 432c858

Please sign in to comment.