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 May 8, 2017
1 parent 673c617 commit 0a36f1c
Show file tree
Hide file tree
Showing 8 changed files with 1,877 additions and 154 deletions.
1,342 changes: 1,342 additions & 0 deletions packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-reuse-test.js

Large diffs are not rendered by default.

Expand Up @@ -5,12 +5,24 @@ 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: [[require("../src/index"), options]]
}).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 @@ -685,20 +697,20 @@ 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 @@ -737,7 +749,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 @@ -1366,4 +1378,54 @@ describe("mangle-names", () => {
);
expect(transform(source, { topLevel: true }, "module")).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
@@ -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
@@ -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);
}
}
};

0 comments on commit 0a36f1c

Please sign in to comment.