This commit is contained in:
Alex Lam S.L 2017-07-10 18:34:35 +00:00 committed by GitHub
commit 4118fdd801
5 changed files with 23 additions and 21 deletions

View File

@ -415,14 +415,10 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
var to_mangle = []; var to_mangle = [];
if (options.cache) { if (options.cache) {
this.globals.each(function(symbol){ push_symbols(this.globals);
if (options.reserved.indexOf(symbol.name) < 0) {
to_mangle.push(symbol);
}
});
} }
var tw = new TreeWalker(function(node, descend){ this.walk(new TreeWalker(function(node, descend) {
if (node instanceof AST_LabeledStatement) { if (node instanceof AST_LabeledStatement) {
// lname is incremented when we get to the AST_Label // lname is incremented when we get to the AST_Label
var save_nesting = lname; var save_nesting = lname;
@ -431,13 +427,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
return true; // don't descend again in TreeWalker return true; // don't descend again in TreeWalker
} }
if (node instanceof AST_Scope) { if (node instanceof AST_Scope) {
var p = tw.parent(), a = []; push_symbols(node.variables);
node.variables.each(function(symbol){
if (options.reserved.indexOf(symbol.name) < 0) {
a.push(symbol);
}
});
to_mangle.push.apply(to_mangle, a);
return; return;
} }
if (node instanceof AST_Label) { if (node instanceof AST_Label) {
@ -450,13 +440,25 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
to_mangle.push(node.definition()); to_mangle.push(node.definition());
return; return;
} }
}); }));
this.walk(tw);
to_mangle.forEach(function(def){ def.mangle(options) }); to_mangle.forEach(function(def){ def.mangle(options) });
if (options.cache) { if (options.cache) {
options.cache.cname = this.cname; options.cache.cname = this.cname;
} }
function push_symbols(dict) {
var a = [];
dict.each(function(symbol) {
if (options.reserved.indexOf(symbol.name) < 0) {
a.push(symbol);
}
});
a.sort(function(m, n) {
return n.references.length - m.references.length;
});
to_mangle.push.apply(to_mangle, a);
}
}); });
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){

View File

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

View File

@ -17,8 +17,8 @@ mangle_keep_fnames_false: {
expect: { expect: {
"use strict"; "use strict";
function total() { function total() {
return function t(n, r, u) { return function c(t, r, u) {
return n + r + u; return t + r + u;
}; };
} }
} }

View File

@ -71,5 +71,5 @@ undefined_redefined_mangle: {
return typeof n == "undefined"; return typeof n == "undefined";
} }
} }
expect_exact: "function f(n){var r=1;return void 0===r}" expect_exact: "function f(r){var n=1;return void 0===n}"
} }

View File

@ -43,7 +43,7 @@ describe("minify", function() {
compressed += result.code; compressed += result.code;
}); });
assert.strictEqual(JSON.stringify(cache).slice(0, 20), '{"cname":5,"props":{'); assert.strictEqual(JSON.stringify(cache).slice(0, 20), '{"cname":5,"props":{');
assert.strictEqual(compressed, 'function n(n){return 3*n}function r(n){return n/2}function c(o){l("Foo:",2*o)}var l=console.log.bind(console);var f=n(3),i=r(12);l("qux",f,i),c(11);'); assert.strictEqual(compressed, 'function n(n){return 3*n}function r(n){return n/2}function l(o){c("Foo:",2*o)}var c=console.log.bind(console);var f=n(3),i=r(12);c("qux",f,i),l(11);');
assert.strictEqual(run_code(compressed), run_code(original)); assert.strictEqual(run_code(compressed), run_code(original));
}); });
@ -69,7 +69,7 @@ describe("minify", function() {
compressed += result.code; compressed += result.code;
}); });
assert.strictEqual(JSON.stringify(cache).slice(0, 28), '{"vars":{"cname":5,"props":{'); assert.strictEqual(JSON.stringify(cache).slice(0, 28), '{"vars":{"cname":5,"props":{');
assert.strictEqual(compressed, 'function n(n){return 3*n}function r(n){return n/2}function c(o){l("Foo:",2*o)}var l=console.log.bind(console);var f=n(3),i=r(12);l("qux",f,i),c(11);'); assert.strictEqual(compressed, 'function n(n){return 3*n}function r(n){return n/2}function l(o){c("Foo:",2*o)}var c=console.log.bind(console);var f=n(3),i=r(12);c("qux",f,i),l(11);');
assert.strictEqual(run_code(compressed), run_code(original)); assert.strictEqual(run_code(compressed), run_code(original));
}); });