Skip to content

Commit

Permalink
Reuse removed vars in mangler
Browse files Browse the repository at this point in the history
ResetNext identifier only when reuse is true

Fix tests - add keepClassName

Reuse vars as default

I dont know why it works - #326

Extract tracker to a separate file, Add topLevel Option
  • Loading branch information
boopathi committed Feb 3, 2017
1 parent 1211936 commit cf98f73
Show file tree
Hide file tree
Showing 8 changed files with 1,803 additions and 221 deletions.
1,210 changes: 1,210 additions & 0 deletions packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-reuse-test.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const babel = require("babel-core");
const unpad = require("../../../utils/unpad");

function transform(code, options = {}, sourceType = "script") {
options.reuse = false;
return babel.transform(code, {
sourceType,
plugins: [
Expand All @@ -13,6 +14,17 @@ function transform(code, options = {}, sourceType = "script") {
}).code;
}

function transformWithSimplify(code, options = {}, sourceType = "script") {
options.reuse = false;
return babel.transform(code, {
sourceType,
plugins: [
require("../../babel-plugin-minify-simplify/src/index"),
[require("../src/index"), options]
]
}).code;
}

describe("mangle-names", () => {
it("should not mangle names in the global namespace", () => {
const source = unpad(`
Expand Down Expand Up @@ -587,19 +599,19 @@ describe("mangle-names", () => {
traverse.clearCache();

const actual = babel.transformFromAst(first.ast, null, {
plugins: [require("../src/index")],
plugins: [[require("../src/index"), { reuse: false }]],
}).code;

const expected = unpad(`
function f(a) {
var b = function (d) {
var e = void 0;
if (e) {
var b = function (e) {
var h = void 0;
if (h) {
return {
v: void 0
};
}
g(() => e);
g(() => h);
};
for (var d = 0; d; d++) {
Expand Down Expand Up @@ -635,7 +647,7 @@ describe("mangle-names", () => {
traverse.clearCache();

const actual = babel.transformFromAst(first.ast, null, {
plugins: [require("../src/index")],
plugins: [[require("../src/index"), { reuse: false }]],
}).code;

const expected = unpad(`
Expand Down Expand Up @@ -1105,4 +1117,55 @@ describe("mangle-names", () => {
`);
expect(transform(source)).toBe(expected);
});

it("should handle constant violations across multiple blocks", () => {
const source = unpad(`
function foo() {
var x;x;x;
{
var x;x;x;
function y() {
var x;x;x;
{
var x;x;x;
}
}
}
}
`);
const expected = unpad(`
function foo() {
var a;a;a;
{
var a;a;a;
function b() {
var c;c;c;
{
var c;c;c;
}
}
}
}
`);
expect(transform(source)).toBe(expected);
});

it("should work with if_return optimization changing fn scope", () => {
const source = unpad(`
function foo() {
if (x)
return;
function bar() {}
bar(a);
}
`);
const expected = unpad(`
function foo() {
function b() {}
x || b(a);
}
`);
expect(transformWithSimplify(source)).toBe(expected);
});

});
51 changes: 51 additions & 0 deletions packages/babel-plugin-minify-mangle-names/src/charset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

const CHARSET = ("abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ$_").split("");

module.exports = class Charset {
constructor(shouldConsider) {
this.shouldConsider = shouldConsider;
this.chars = CHARSET.slice();
this.frequency = {};
this.chars.forEach((c) => { this.frequency[c] = 0; });
this.finalized = false;
}

consider(str) {
if (!this.shouldConsider) {
return;
}

str.split("").forEach((c) => {
if (this.frequency[c] != null) {
this.frequency[c]++;
}
});
}

sort() {
if (this.shouldConsider) {
this.chars = this.chars.sort(
(a, b) => this.frequency[b] - this.frequency[a]
);
}

this.finalized = true;
}

getIdentifier(num) {
if (!this.finalized) {
throw new Error("Should sort first");
}

let ret = "";
num++;
do {
num--;
ret += this.chars[num % this.chars.length];
num = Math.floor(num / this.chars.length);
} while (num > 0);
return ret;
}
};
28 changes: 28 additions & 0 deletions packages/babel-plugin-minify-mangle-names/src/counted-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Set that counts
module.exports = class CountedSet {
constructor() {
// because you can't simply extend Builtins yet
this.map = new Map;
}
keys() {
return [...this.map.keys()];
}
has(value) {
return this.map.has(value);
}
add(value) {
if (!this.has(value)) {
this.map.set(value, 0);
}
this.map.set(value, this.map.get(value) + 1);
}
delete(value) {
if (!this.has(value)) return;
const count = this.map.get(value);
if (count <= 1) {
this.map.delete(value);
} else {
this.map.set(value, count - 1);
}
}
};
Loading

0 comments on commit cf98f73

Please sign in to comment.