Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Detect duplicate declaration during one-use replacement #626

Merged
merged 2 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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