From fedba5b2a2540b9dfc136911528c55b73c7dc97b Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Mon, 29 Aug 2016 17:18:43 +0200 Subject: [PATCH 1/5] Add fix for other types of nodes in referencePaths + (Close #122) + (Close #105) --- .../src/index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index e045130cf..837a73ccc 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -163,9 +163,21 @@ module.exports = ({ types: t }) => { const path = refs[i]; const {node} = path; if (!path.isIdentifier()) { - // if this occurs, then it is - // probably an upstream bug (in babel) - throw new Error("Unexpected " + path.node.type + ". Expected an Identifier"); + // Ideally, this should not happen + // it happens in these places now - + // case 1: Export Statements + // This is a bug in babel + // https://github.com/babel/babel/pull/3629 + // case 2: Replacements in other plugins + // eg: https://github.com/babel/babili/issues/122 + // replacement in dce from `x` to `!x` gives referencePath as `!x` + path.traverse({ + ReferencedIdentifier(refPath) { + if (refPath.node.name === oldName && refPath.scope === scope) { + refPath.node.name = newName; + } + } + }); } if (!isLabelIdentifier(path)) { node.name = newName; From c8a9f6d9d3f5ef7efcbe2bc5f1f3da8f174376b5 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Mon, 29 Aug 2016 17:24:53 +0200 Subject: [PATCH 2/5] return after renaming --- packages/babel-plugin-minify-mangle-names/src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index 837a73ccc..464ab437a 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -178,6 +178,7 @@ module.exports = ({ types: t }) => { } } }); + return; } if (!isLabelIdentifier(path)) { node.name = newName; From 6db0b1a96b646a89ff4f50c9aeeaa4b9d3dbaf2a Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Mon, 29 Aug 2016 17:26:36 +0200 Subject: [PATCH 3/5] It's not a return --- packages/babel-plugin-minify-mangle-names/src/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index 464ab437a..c7fa5638f 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -178,9 +178,7 @@ module.exports = ({ types: t }) => { } } }); - return; - } - if (!isLabelIdentifier(path)) { + } else if (!isLabelIdentifier(path)) { node.name = newName; } } From 8af82a82b6365237989efec9764681b0a436da54 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Mon, 29 Aug 2016 17:43:08 +0200 Subject: [PATCH 4/5] Add tests for export statements --- .../__tests__/mangle-names-test.js | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-test.js b/packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-test.js index dba6acc27..b31ba48ff 100644 --- a/packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-test.js +++ b/packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-test.js @@ -4,9 +4,9 @@ const traverse = require("babel-traverse").default; const babel = require("babel-core"); const unpad = require("../../../utils/unpad"); -function transform(code, options = {}) { +function transform(code, options = {}, sourceType = "script") { return babel.transform(code, { - sourceType: "script", + sourceType, plugins: [ [require("../src/index"), options], ], @@ -873,4 +873,34 @@ describe("mangle-names", () => { `); expect(transform(source)).toBe(expected); }); + + it("should handle export declarations", () => { + const source = unpad(` + const foo = 1; + export { foo }; + export const bar = 2; + export function baz(bar, foo) { + bar(); + foo(); + }; + export default function (bar, baz) { + bar(); + baz(); + } + `); + const expected = unpad(` + const foo = 1; + export { foo }; + export const bar = 2; + export function baz(a, b) { + a(); + b(); + }; + export default function (a, b) { + a(); + b(); + } + `); + expect(transform(source, {}, "module")).toBe(expected); + }); }); From 705f1cb429aabe1ac3fd1f823af2ed6caa24eaad Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Mon, 29 Aug 2016 17:55:08 +0200 Subject: [PATCH 5/5] Add tests for #122 --- .../__tests__/preset-tests.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 packages/babel-preset-babili/__tests__/preset-tests.js diff --git a/packages/babel-preset-babili/__tests__/preset-tests.js b/packages/babel-preset-babili/__tests__/preset-tests.js new file mode 100644 index 000000000..ef4e53f3c --- /dev/null +++ b/packages/babel-preset-babili/__tests__/preset-tests.js @@ -0,0 +1,42 @@ +jest.autoMockOff(); + +const babel = require("babel-core"); +const unpad = require("../../../utils/unpad"); + +function transform(code, options = {}, sourceType = "script") { + return babel.transform(code, { + sourceType, + minified: false, + presets: [ + require("../src/index") + ], + }).code; +} + +describe("preset", () => { + // https://github.com/babel/babili/issues/122 + it ("should fix issue#122", () => { + const source = unpad(` + function foo() { + var a, b, c; + if (a) { + if (b) { + if (c) {} + } + } else { + if (b) { + } else { + if (c) {} + } + } + } + `); + const expected = unpad(` + function foo() { + var d, e, f; + d ? e && f : !b && f; + } + `); + expect(transform(source)).toBe(expected); + }); +});