From f8d4f077d17f978703645367e2e6ea5bb2538c65 Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Wed, 14 Jun 2017 17:31:55 +0200 Subject: [PATCH] replace paths with cloned nodes in builtins transform (#579) --- .../babel-plugin-minify-builtins/src/index.js | 6 ++---- .../__tests__/preset-tests.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) 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); + }); });