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

Mangle - Fix for classes and program scope #140

Merged
merged 6 commits into from
Sep 12, 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 @@ -903,4 +903,71 @@ describe("mangle-names", () => {
`);
expect(transform(source, {}, "module")).toBe(expected);
});

it("should find global scope properly", () => {
const source = unpad(`
class A {}
class B extends A {}
(function () {
class C {
constructor() {
new A();
new B();
C;
}
}
})();
`);
const expected = unpad(`
class A {}
class B extends A {}
(function () {
class a {
constructor() {
new A();
new B();
a;
}
}
})();
`);
expect(transform(source)).toBe(expected);
});

it("should mangle classes properly", () => {
const source = unpad(`
class A {}
class B {}
new A();
new B();
function a() {
class A {}
class B {}
new A();
new B();
}
`);
const expected = unpad(`
class A {}
class B {}
new A();
new B();
function a() {
class b {}
class c {}
new b();
new c();
}
`);
expect(transform(source)).toBe(expected);
});

// https://github.com/babel/babili/issues/138
it("should handle class exports in modules - issue#138", () => {
const source = unpad(`
export class App extends Object {};
`);
const expected = source;
expect(transform(source, {}, "module")).toBe(expected);
});
});
14 changes: 11 additions & 3 deletions packages/babel-plugin-minify-mangle-names/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ module.exports = ({ types: t }) => {

this.program.traverse({
Scopable(path) {
if (path.isProgram()) return;

const {scope} = path;

if (!mangler.eval && mangler.unsafeScopes.has(scope)) return;
Expand Down Expand Up @@ -115,9 +113,17 @@ module.exports = ({ types: t }) => {
const binding = bindings[oldName];

if (
!scope.hasOwnBinding(oldName)
// already renamed bindings
binding.renamed
// globals
|| mangler.program.scope.bindings[oldName] === binding
// other scope bindings
|| !scope.hasOwnBinding(oldName)
// labels
|| binding.path.isLabeledStatement()
// blacklisted
|| mangler.isBlacklist(oldName)
// function names
|| (mangler.keepFnames ? isFunction(binding.path) : false)
) {
continue;
Expand All @@ -137,6 +143,8 @@ module.exports = ({ types: t }) => {
// re-enable this - check above
// resetNext();
mangler.rename(scope, oldName, next);
// mark the binding as renamed
binding.renamed = true;
}
}
});
Expand Down