diff --git a/packages/babel-plugin-minify-builtins/src/index.js b/packages/babel-plugin-minify-builtins/src/index.js index ea73fb545..5f1119e01 100644 --- a/packages/babel-plugin-minify-builtins/src/index.js +++ b/packages/babel-plugin-minify-builtins/src/index.js @@ -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); diff --git a/packages/babel-preset-babili/__tests__/preset-tests.js b/packages/babel-preset-babili/__tests__/preset-tests.js index 6e6fb4324..e7fefe395 100644 --- a/packages/babel-preset-babili/__tests__/preset-tests.js +++ b/packages/babel-preset-babili/__tests__/preset-tests.js @@ -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); + }); });