Skip to content

Commit

Permalink
Fix mangler binding rename issue with duplicate names (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Feb 12, 2017
1 parent 498ca07 commit 8b86de0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
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

0 comments on commit 8b86de0

Please sign in to comment.