From 230d56e457c129349747f8d8bcc0eddd33661ff3 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Fri, 2 Sep 2016 00:19:03 +0200 Subject: [PATCH 1/6] Fix program binding detection + programPath.traverse is never going to send Program type inside the traverse. A class declaration is bound to the class scope as well as the program scope - so class declarations in global scope is mangled. --- packages/babel-plugin-minify-mangle-names/src/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index 2119feb40..ebd5893af 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -79,7 +79,11 @@ module.exports = ({ types: t }) => { this.program.traverse({ Scopable(path) { - if (path.isProgram()) return; + const programParent = path.scope.getProgramParent().path + + if (programParent === path.parentPath) { + return; + } const {scope} = path; From 6420262ef5761233d7184f8f3e4f906b25a60bea Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Fri, 2 Sep 2016 00:25:54 +0200 Subject: [PATCH 2/6] Add tests --- .../__tests__/mangle-names-test.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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 b31ba48ff..13a915263 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 @@ -903,4 +903,34 @@ 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 b { + constructor() { + new A(); + new B(); + b; + } + } + })(); + `); + expect(transform(source)).toBe(expected); + }); }); From af065aec1b331809fbc522efacb684cc97f46243 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Fri, 2 Sep 2016 05:41:14 +0200 Subject: [PATCH 3/6] Fix lint --- packages/babel-plugin-minify-mangle-names/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index ebd5893af..18e165a40 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -79,7 +79,7 @@ module.exports = ({ types: t }) => { this.program.traverse({ Scopable(path) { - const programParent = path.scope.getProgramParent().path + const programParent = path.scope.getProgramParent().path; if (programParent === path.parentPath) { return; From 9361c2f9404486360f0bbb6c3e9771375aab3d1d Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Sat, 10 Sep 2016 21:52:28 +0200 Subject: [PATCH 4/6] Compare block parent and program parent --- packages/babel-plugin-minify-mangle-names/src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index 18e165a40..a79ababac 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -80,8 +80,9 @@ module.exports = ({ types: t }) => { this.program.traverse({ Scopable(path) { const programParent = path.scope.getProgramParent().path; + const blockParent = path.scope.getBlockParent().path; - if (programParent === path.parentPath) { + if (programParent === blockParent) { return; } From f6d5b3ac74a2b6ad4ea358cccaae61856eae26d3 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Sat, 10 Sep 2016 22:23:22 +0200 Subject: [PATCH 5/6] Fix mangling classes --- .../__tests__/mangle-names-test.js | 32 +++++++++++++++++-- .../src/index.js | 19 ++++++----- 2 files changed, 41 insertions(+), 10 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 13a915263..03a55525f 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 @@ -922,15 +922,43 @@ describe("mangle-names", () => { class A {} class B extends A {} (function () { - class b { + class a { constructor() { new A(); new B(); - 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); + }); }); diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index a79ababac..62222864a 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -79,13 +79,6 @@ module.exports = ({ types: t }) => { this.program.traverse({ Scopable(path) { - const programParent = path.scope.getProgramParent().path; - const blockParent = path.scope.getBlockParent().path; - - if (programParent === blockParent) { - return; - } - const {scope} = path; if (!mangler.eval && mangler.unsafeScopes.has(scope)) return; @@ -120,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; @@ -142,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; } } }); From fe557fe0c30221aa71df8896e317266bfee753dd Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Sat, 10 Sep 2016 22:27:36 +0200 Subject: [PATCH 6/6] Add test for #138 (close #138) --- .../__tests__/mangle-names-test.js | 9 +++++++++ 1 file changed, 9 insertions(+) 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 03a55525f..e38983089 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 @@ -961,4 +961,13 @@ describe("mangle-names", () => { `); 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); + }); });