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); + }); }); diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index e045130cf..c7fa5638f 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -163,11 +163,22 @@ 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"); - } - if (!isLabelIdentifier(path)) { + // 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; + } + } + }); + } else if (!isLabelIdentifier(path)) { node.name = newName; } } 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); + }); +});