Skip to content

Commit f8d4f07

Browse files
vigneshshanmugamboopathi
authored andcommitted
replace paths with cloned nodes in builtins transform (#579)
1 parent 78c8208 commit f8d4f07

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/babel-plugin-minify-builtins/src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ module.exports = function({ types: t }) {
7878
if (subpaths.length <= 1) {
7979
continue;
8080
}
81-
const uniqueIdentifier = this.program.scope.generateUidIdentifier(
82-
expName
83-
);
81+
const uniqueIdentifier = parent.scope.generateUidIdentifier(expName);
8482
const newNode = t.variableDeclaration("var", [
8583
t.variableDeclarator(uniqueIdentifier, subpaths[0].node)
8684
]);
8785

8886
for (const path of subpaths) {
89-
path.replaceWith(uniqueIdentifier);
87+
path.replaceWith(t.clone(uniqueIdentifier));
9088
}
9189
// hoist the created var to the top of the function scope
9290
parent.get("body").unshiftContainer("body", newNode);

packages/babel-preset-babili/__tests__/preset-tests.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,20 @@ describe("preset", () => {
131131
`);
132132
expect(transform(source)).toBe(expected);
133133
});
134+
135+
it("should fix bug#568 - conflicts b/w builtIns and mangle", () => {
136+
const source = unpad(`
137+
(function () {
138+
return [Math.pi, Math.pi];
139+
})();
140+
`);
141+
const expected = unpad(`
142+
(function () {
143+
var a = Math.pi;
144+
145+
return [a, a];
146+
})();
147+
`);
148+
expect(transform(source)).toBe(expected);
149+
});
134150
});

0 commit comments

Comments
 (0)