Skip to content

Commit

Permalink
replace paths with cloned nodes in builtins transform (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam authored and boopathi committed Jun 14, 2017
1 parent 78c8208 commit f8d4f07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 2 additions & 4 deletions packages/babel-plugin-minify-builtins/src/index.js
Expand Up @@ -78,15 +78,13 @@ module.exports = function({ types: t }) {
if (subpaths.length <= 1) {
continue;
}
const uniqueIdentifier = this.program.scope.generateUidIdentifier(
expName
);
const uniqueIdentifier = parent.scope.generateUidIdentifier(expName);
const newNode = t.variableDeclaration("var", [
t.variableDeclarator(uniqueIdentifier, subpaths[0].node)
]);

for (const path of subpaths) {
path.replaceWith(uniqueIdentifier);
path.replaceWith(t.clone(uniqueIdentifier));
}
// hoist the created var to the top of the function scope
parent.get("body").unshiftContainer("body", newNode);
Expand Down
16 changes: 16 additions & 0 deletions packages/babel-preset-babili/__tests__/preset-tests.js
Expand Up @@ -131,4 +131,20 @@ describe("preset", () => {
`);
expect(transform(source)).toBe(expected);
});

it("should fix bug#568 - conflicts b/w builtIns and mangle", () => {
const source = unpad(`
(function () {
return [Math.pi, Math.pi];
})();
`);
const expected = unpad(`
(function () {
var a = Math.pi;
return [a, a];
})();
`);
expect(transform(source)).toBe(expected);
});
});

0 comments on commit f8d4f07

Please sign in to comment.