aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-06-23 13:11:40 +0800
committerGitHub <noreply@github.com>2017-06-23 13:11:40 +0800
commitd58b184835d9c2316bfb4411d0907ebb4446aaf0 (patch)
tree807806a9b0bf6f6b930c3801673d134bc71f526b /lib/compress.js
parentb3a57ff019bf5c64783f55e581aa4270a52d9d13 (diff)
downloadtracifyjs-d58b184835d9c2316bfb4411d0907ebb4446aaf0.tar.gz
tracifyjs-d58b184835d9c2316bfb4411d0907ebb4446aaf0.zip
refactor `Compressor.toplevel` (#2149)
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js32
1 files changed, 16 insertions, 16 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 8890759f..c24510f6 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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;