synchronise mangle.properties for minify() & test/compress (#2151)

This commit is contained in:
Alex Lam S.L
2017-06-23 15:53:13 +08:00
committed by GitHub
parent d58b184835
commit dc6bcaa18e
8 changed files with 91 additions and 75 deletions

View File

@@ -3114,7 +3114,9 @@ merge(Compressor.prototype, {
var comp = new Compressor(compressor.options); var comp = new Compressor(compressor.options);
ast = ast.transform(comp); ast = ast.transform(comp);
ast.figure_out_scope(mangle); ast.figure_out_scope(mangle);
ast.mangle_names(); base54.reset();
ast.compute_char_frequency(mangle);
ast.mangle_names(mangle);
var fun; var fun;
ast.walk(new TreeWalker(function(node) { ast.walk(new TreeWalker(function(node) {
if (fun) return true; if (fun) return true;

View File

@@ -265,7 +265,7 @@ issue_203: {
} }
expect: { expect: {
var m = {}; var m = {};
var fn = Function("a", "b", "b.exports=42"); var fn = Function("n", "o", "o.exports=42");
fn(null, m, m.exports); fn(null, m, m.exports);
console.log(m.exports); console.log(m.exports);
} }

View File

@@ -10,9 +10,9 @@ issue_1321_no_debug: {
} }
expect: { expect: {
var x = {}; var x = {};
x.b = 1; x.o = 1;
x["a"] = 2 * x.b; x["a"] = 2 * x.o;
console.log(x.b, x["a"]); console.log(x.o, x["a"]);
} }
expect_stdout: true expect_stdout: true
} }
@@ -30,9 +30,9 @@ issue_1321_debug: {
} }
expect: { expect: {
var x = {}; var x = {};
x.a = 1; x.o = 1;
x["_$foo$_"] = 2 * x.a; x["_$foo$_"] = 2 * x.o;
console.log(x.a, x["_$foo$_"]); console.log(x.o, x["_$foo$_"]);
} }
expect_stdout: true expect_stdout: true
} }
@@ -49,9 +49,9 @@ issue_1321_with_quoted: {
} }
expect: { expect: {
var x = {}; var x = {};
x.a = 1; x.o = 1;
x["b"] = 2 * x.a; x["x"] = 2 * x.o;
console.log(x.a, x["b"]); console.log(x.o, x["x"]);
} }
expect_stdout: true expect_stdout: true
} }

View File

@@ -82,7 +82,7 @@ numeric_literal: {
' 42: 2,', ' 42: 2,',
' "42": 3,', ' "42": 3,',
' 37: 4,', ' 37: 4,',
' a: 5,', ' o: 5,',
' 1e42: 6,', ' 1e42: 6,',
' b: 7,', ' b: 7,',
' "1e+42": 8', ' "1e+42": 8',
@@ -92,7 +92,7 @@ numeric_literal: {
'', '',
'console.log(obj[42], obj["42"]);', 'console.log(obj[42], obj["42"]);',
'', '',
'console.log(obj[37], obj["a"], obj[37], obj["37"]);', 'console.log(obj[37], obj["o"], obj[37], obj["37"]);',
'', '',
'console.log(obj[1e42], obj["b"], obj["1e+42"]);', 'console.log(obj[1e42], obj["b"], obj["1e+42"]);',
] ]
@@ -173,32 +173,32 @@ identifier: {
} }
expect: { expect: {
var obj = { var obj = {
a: 1, e: 1,
b: 2, t: 2,
c: 3, n: 3,
d: 4, a: 4,
e: 5, i: 5,
f: 6, o: 6,
g: 7, r: 7,
h: 8, l: 8,
i: 9, s: 9,
j: 10, c: 10,
k: 11, f: 11,
l: 12, u: 12,
m: 13, d: 13,
n: 14, h: 14,
o: 15, p: 15,
p: 16, b: 16,
q: 17, v: 17,
r: 18, w: 18,
s: 19, y: 19,
t: 20, g: 20,
u: 21, m: 21,
v: 22, k: 22,
w: 23, x: 23,
x: 24, j: 24,
y: 25, z: 25,
z: 26, q: 26,
A: 27, A: 27,
B: 28, B: 28,
C: 29, C: 29,
@@ -229,11 +229,11 @@ identifier: {
Z: 54, Z: 54,
$: 55, $: 55,
_: 56, _: 56,
aa: 57, ee: 57,
ba: 58, te: 58,
ca: 59, ne: 59,
da: 60, ae: 60,
ea: 61, ie: 61,
}; };
} }
} }

View File

@@ -1,37 +1,41 @@
dont_reuse_prop: { dont_reuse_prop: {
mangle_props = { mangle_props = {
regex: /asd/ regex: /asd/
}; }
input: { input: {
"aaaaaaaaaabbbbb";
var obj = {}; var obj = {};
obj.a = 123; obj.a = 123;
obj.asd = 256; obj.asd = 256;
console.log(obj.a); console.log(obj.a);
} }
expect: { expect: {
"aaaaaaaaaabbbbb";
var obj = {}; var obj = {};
obj.a = 123; obj.a = 123;
obj.b = 256; obj.b = 256;
console.log(obj.a); console.log(obj.a);
} }
expect_stdout: "123"
} }
unmangleable_props_should_always_be_reserved: { unmangleable_props_should_always_be_reserved: {
mangle_props = { mangle_props = {
regex: /asd/ regex: /asd/
}; }
input: { input: {
"aaaaaaaaaabbbbb";
var obj = {}; var obj = {};
obj.asd = 256; obj.asd = 256;
obj.a = 123; obj.a = 123;
console.log(obj.a); console.log(obj.a);
} }
expect: { expect: {
"aaaaaaaaaabbbbb";
var obj = {}; var obj = {};
obj.b = 256; obj.b = 256;
obj.a = 123; obj.a = 123;
console.log(obj.a); console.log(obj.a);
} }
expect_stdout: "123"
} }

View File

@@ -135,11 +135,11 @@ mangle_properties: {
a['run']({color: "blue", foo: "baz"}); a['run']({color: "blue", foo: "baz"});
} }
expect: { expect: {
a["a"] = "bar"; a["o"] = "bar";
a.b = "red"; a.a = "red";
x = {c: 10}; x = {r: 10};
a.d(x.c, a.a); a.b(x.r, a.o);
a['d']({b: "blue", a: "baz"}); a['b']({a: "blue", o: "baz"});
} }
} }
@@ -177,16 +177,16 @@ mangle_unquoted_properties: {
function f1() { function f1() {
a["foo"] = "bar"; a["foo"] = "bar";
a.color = "red"; a.color = "red";
a.b = 2; a.o = 2;
x = {"bar": 10, c: 7}; x = {"bar": 10, f: 7};
a.c = 9; a.f = 9;
} }
function f2() { function f2() {
a.foo = "bar"; a.foo = "bar";
a['color'] = "red"; a['color'] = "red";
x = {bar: 10, c: 7}; x = {bar: 10, f: 7};
a.c = 9; a.f = 9;
a.b = 3; a.o = 3;
} }
} }
} }

View File

@@ -2,29 +2,37 @@ var Uglify = require('../../');
var assert = require("assert"); var assert = require("assert");
describe("let", function() { describe("let", function() {
it("Should not produce `let` as a variable name in mangle", function(done) { it("Should not produce reserved keywords as variable name in mangle", function(done) {
this.timeout(10000); this.timeout(10000);
// Produce a lot of variables in a function and run it through mangle. // Produce a lot of variables in a function and run it through mangle.
var s = '"use strict"; function foo() {'; var s = '"dddddeeeeelllllooooottttt"; function foo() {';
for (var i = 0; i < 21000; ++i) { for (var i = 0; i < 18000; i++) {
s += "var v" + i + "=0;"; s += "var v" + i + "=0;";
} }
s += '}'; s += '}';
var result = Uglify.minify(s, {compress: false}); var result = Uglify.minify(s, {compress: false});
// Verify that select keywords and reserved keywords not produced // Verify that select keywords and reserved keywords not produced
assert.strictEqual(result.code.indexOf("var let="), -1); [
assert.strictEqual(result.code.indexOf("var do="), -1); "do",
assert.strictEqual(result.code.indexOf("var var="), -1); "let",
"var",
].forEach(function(name) {
assert.strictEqual(result.code.indexOf("var " + name + "="), -1);
});
// Verify that the variable names that appeared immediately before // Verify that the variable names that appeared immediately before
// and after the erroneously generated `let` variable name still exist // and after the erroneously generated variable name still exist
// to show the test generated enough symbols. // to show the test generated enough symbols.
assert(result.code.indexOf("var ket=") >= 0); [
assert(result.code.indexOf("var met=") >= 0); "to", "eo",
"eet", "fet",
"rar", "oar",
].forEach(function(name) {
assert.ok(result.code.indexOf("var " + name + "=") >= 0);
});
done(); done();
}); });
}); });

View File

@@ -86,7 +86,6 @@ function run_compress_tests() {
log_start_file(file); log_start_file(file);
function test_case(test) { function test_case(test) {
log_test(test.name); log_test(test.name);
U.base54.reset();
var output_options = test.beautify || {}; var output_options = test.beautify || {};
var expect; var expect;
if (test.expect) { if (test.expect) {
@@ -101,9 +100,6 @@ function run_compress_tests() {
quote_style: 3, quote_style: 3,
keep_quoted_props: true keep_quoted_props: true
}); });
if (test.mangle_props) {
input = U.mangle_properties(input, test.mangle_props);
}
var options = U.defaults(test.options, { var options = U.defaults(test.options, {
warnings: false warnings: false
}); });
@@ -118,10 +114,16 @@ function run_compress_tests() {
var cmp = new U.Compressor(options, true); var cmp = new U.Compressor(options, true);
var output = cmp.compress(input); var output = cmp.compress(input);
output.figure_out_scope(test.mangle); output.figure_out_scope(test.mangle);
if (test.mangle) { if (test.mangle || test.mangle_props) {
U.base54.reset();
output.compute_char_frequency(test.mangle); output.compute_char_frequency(test.mangle);
}
if (test.mangle) {
output.mangle_names(test.mangle); output.mangle_names(test.mangle);
} }
if (test.mangle_props) {
output = U.mangle_properties(output, test.mangle_props);
}
output = make_code(output, output_options); output = make_code(output, output_options);
if (expect != output) { if (expect != output) {
log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", { log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", {