refactor Compressor.toplevel (#2149)
This commit is contained in:
@@ -124,13 +124,13 @@ function Compressor(options, false_by_default) {
|
||||
};
|
||||
}
|
||||
var toplevel = this.options["toplevel"];
|
||||
if (typeof toplevel == "string") {
|
||||
this.toplevel.funcs = /funcs/.test(toplevel);
|
||||
this.toplevel.vars = /vars/.test(toplevel);
|
||||
} else {
|
||||
this.toplevel = toplevel ? return_true : return_false;
|
||||
this.toplevel.funcs = this.toplevel.vars = toplevel;
|
||||
}
|
||||
this.toplevel = typeof toplevel == "string" ? {
|
||||
funcs: /funcs/.test(toplevel),
|
||||
vars: /vars/.test(toplevel)
|
||||
} : {
|
||||
funcs: toplevel,
|
||||
vars: toplevel
|
||||
};
|
||||
var sequences = this.options["sequences"];
|
||||
this.sequences_limit = sequences == 1 ? 800 : sequences | 0;
|
||||
this.warnings_produced = {};
|
||||
@@ -139,11 +139,11 @@ function Compressor(options, false_by_default) {
|
||||
Compressor.prototype = new TreeTransformer;
|
||||
merge(Compressor.prototype, {
|
||||
option: function(key) { return this.options[key] },
|
||||
toplevel: function(def) {
|
||||
for (var i = 0, len = def.orig.length; i < len; i++)
|
||||
exposed: function(def) {
|
||||
if (def.global) for (var i = 0, len = def.orig.length; i < len; i++)
|
||||
if (!this.toplevel[def.orig[i] instanceof AST_SymbolDefun ? "funcs" : "vars"])
|
||||
return false;
|
||||
return true;
|
||||
return true;
|
||||
return false;
|
||||
},
|
||||
compress: function(node) {
|
||||
if (this.option("expression")) {
|
||||
@@ -345,7 +345,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (node instanceof AST_Defun) {
|
||||
var d = node.name.definition();
|
||||
if (d.global && !compressor.toplevel(d) || safe_to_read(d)) {
|
||||
if (compressor.exposed(d) || safe_to_read(d)) {
|
||||
d.fixed = false;
|
||||
} else {
|
||||
d.fixed = node;
|
||||
@@ -517,7 +517,7 @@ merge(Compressor.prototype, {
|
||||
def.escaped = false;
|
||||
if (def.scope.uses_eval) {
|
||||
def.fixed = false;
|
||||
} else if (!def.global || compressor.toplevel(def)) {
|
||||
} else if (!compressor.exposed(def)) {
|
||||
def.fixed = undefined;
|
||||
} else {
|
||||
def.fixed = false;
|
||||
@@ -748,7 +748,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (candidate instanceof AST_VarDef) {
|
||||
var def = candidate.name.definition();
|
||||
if (def.references.length == 1 && (!def.global || compressor.toplevel(def))) {
|
||||
if (def.references.length == 1 && !compressor.exposed(def)) {
|
||||
return maintain_this_binding(parent, node, candidate.value);
|
||||
}
|
||||
return make_node(AST_Assign, candidate, {
|
||||
@@ -810,7 +810,7 @@ merge(Compressor.prototype, {
|
||||
if (expr instanceof AST_VarDef) {
|
||||
var def = expr.name.definition();
|
||||
if (def.orig.length > 1
|
||||
|| def.references.length == 1 && (!def.global || compressor.toplevel(def))) {
|
||||
|| def.references.length == 1 && !compressor.exposed(def)) {
|
||||
return make_node(AST_SymbolRef, expr.name, expr.name);
|
||||
}
|
||||
} else {
|
||||
@@ -3918,7 +3918,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
var name_length = d.name.length;
|
||||
var overhead = 0;
|
||||
if (compressor.option("unused") && (!d.global || compressor.toplevel(d))) {
|
||||
if (compressor.option("unused") && !compressor.exposed(d)) {
|
||||
overhead = (name_length + 2 + value_length) / d.references.length;
|
||||
}
|
||||
d.should_replace = value_length <= name_length + overhead ? fn : false;
|
||||
|
||||
Reference in New Issue
Block a user