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

replace paths with cloned nodes in builtins transform #579

Merged
merged 1 commit into from Jun 14, 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
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);
});
});