Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix for other types of nodes in referencePaths #123

Merged
merged 5 commits into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should add this to DCE as well... or shall I add it to preset tests ?

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 @@ -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;
}
}
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") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would make sense to move transform to utils and share among tests to avoid duplication?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.. Like discussed here #76 ... separate PR for all these things ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I forgot :) If you can make a PR for that, I'd gladly merge — would make tests a bit cleaner.

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);
});
});