Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix mangler binding rename issue with duplicate names #414

Merged
merged 1 commit into from Feb 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1105,4 +1105,28 @@ describe("mangle-names", () => {
`);
expect(transform(source)).toBe(expected);
});

it("should rename binding.identifier - issue#411", () => {
const source = unpad(`
!function () {
function e(e) {
foo(e);
}
return function () {
return e();
};
}();
`);
const expected = unpad(`
!function () {
function a(b) {
foo(b);
}
return function () {
return a();
};
}();
`);
expect(transform(source)).toBe(expected);
});
});
22 changes: 13 additions & 9 deletions packages/babel-plugin-minify-mangle-names/src/index.js
Expand Up @@ -186,16 +186,20 @@ module.exports = ({ types: t, traverse }) => {
const binding = scope.getBinding(oldName);

// rename at the declaration level
const bindingPaths = binding.path.getBindingIdentifierPaths();

Object
.keys(bindingPaths)
.map((b) => {
if (b === oldName && !bindingPaths[b][PATH_RENAME_MARKER]) {
bindingPaths[b].replaceWith(t.identifier(newName));
bindingPaths[b][PATH_RENAME_MARKER] = true;
const bindingPaths = binding.path.getBindingIdentifierPaths(true, false);

// we traverse through all bindingPaths because,
// there is no binding.identifierPath in babel
for (const name in bindingPaths) {
if (name !== oldName) continue;
for (const idPath of bindingPaths[name]) {
if (binding.identifier === idPath.node) {
idPath.replaceWith(t.identifier(newName));
binding.identifier = idPath.node;
idPath[PATH_RENAME_MARKER] = true;
}
});
}
}

const {bindings} = scope;
bindings[newName] = binding;
Expand Down