Skip to content

Commit

Permalink
Add fix for other types of nodes in referencePaths (#123)
Browse files Browse the repository at this point in the history
* Add fix for other types of nodes in referencePaths

+ (Close #122)
+ (Close #105)

* return after renaming

* It's not a return

* Add tests for export statements

* Add tests for #122
  • Loading branch information
boopathi authored and kangax committed Aug 29, 2016
1 parent 2da1cc5 commit 96e90e5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
Expand Down Expand Up @@ -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);
});
});
21 changes: 16 additions & 5 deletions packages/babel-plugin-minify-mangle-names/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,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;
}
}
Expand Down
42 changes: 42 additions & 0 deletions packages/babel-preset-babili/__tests__/preset-tests.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 96e90e5

Please sign in to comment.