aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-03-19 01:34:25 +0800
committerGitHub <noreply@github.com>2019-03-19 01:34:25 +0800
commitc520e99eda6a516ea275b58fc100eff378261145 (patch)
tree2c24f3634c9266b887efd05f0a5e281e9892f697
parent615ae37ca3a659df2ed304d7d30a43704fdd43ab (diff)
downloadtracifyjs-c520e99eda6a516ea275b58fc100eff378261145.tar.gz
tracifyjs-c520e99eda6a516ea275b58fc100eff378261145.zip
enhance `comparisons` (#3347)
-rw-r--r--lib/compress.js60
-rw-r--r--test/compress/comparisons.js (renamed from test/compress/comparing.js)22
2 files changed, 53 insertions, 29 deletions
diff --git a/lib/compress.js b/lib/compress.js
index a31ce865..69a900cd 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2252,45 +2252,38 @@ merge(Compressor.prototype, {
return !compressor.option("pure_getters")
|| this._dot_throw(compressor);
});
-
function is_strict(compressor) {
return /strict/.test(compressor.option("pure_getters"));
}
-
def(AST_Node, is_strict);
- def(AST_Null, return_true);
- def(AST_Undefined, return_true);
- def(AST_Constant, return_false);
def(AST_Array, return_false);
- def(AST_Object, function(compressor) {
- if (!is_strict(compressor)) return false;
- for (var i = this.properties.length; --i >=0;)
- if (this.properties[i].value instanceof AST_Accessor) return true;
- return false;
- });
- def(AST_Lambda, return_false);
- def(AST_UnaryPostfix, return_false);
- def(AST_UnaryPrefix, function() {
- return this.operator == "void";
- });
- def(AST_Binary, function(compressor) {
- return (this.operator == "&&" || this.operator == "||")
- && (this.left._dot_throw(compressor) || this.right._dot_throw(compressor));
- })
def(AST_Assign, function(compressor) {
return this.operator == "="
&& this.right._dot_throw(compressor);
})
+ def(AST_Binary, function(compressor) {
+ return (this.operator == "&&" || this.operator == "||")
+ && (this.left._dot_throw(compressor) || this.right._dot_throw(compressor));
+ })
def(AST_Conditional, function(compressor) {
return this.consequent._dot_throw(compressor)
|| this.alternative._dot_throw(compressor);
})
+ def(AST_Constant, return_false);
def(AST_Dot, function(compressor) {
if (!is_strict(compressor)) return false;
var exp = this.expression;
if (exp instanceof AST_SymbolRef) exp = exp.fixed_value();
return !(exp instanceof AST_Lambda && this.property == "prototype");
});
+ def(AST_Lambda, return_false);
+ def(AST_Null, return_true);
+ def(AST_Object, function(compressor) {
+ if (!is_strict(compressor)) return false;
+ for (var i = this.properties.length; --i >=0;)
+ if (this.properties[i].value instanceof AST_Accessor) return true;
+ return false;
+ });
def(AST_Sequence, function(compressor) {
return this.tail_node()._dot_throw(compressor);
});
@@ -2302,6 +2295,11 @@ merge(Compressor.prototype, {
var fixed = this.fixed_value();
return !fixed || fixed._dot_throw(compressor);
});
+ def(AST_UnaryPrefix, function() {
+ return this.operator == "void";
+ });
+ def(AST_UnaryPostfix, return_false);
+ def(AST_Undefined, return_true);
})(function(node, func) {
node.DEFMETHOD("_dot_throw", func);
});
@@ -2335,6 +2333,10 @@ merge(Compressor.prototype, {
def(AST_Sequence, function(compressor) {
return this.tail_node().is_boolean(compressor);
});
+ def(AST_SymbolRef, function(compressor) {
+ var fixed = this.fixed_value();
+ return fixed && fixed.is_boolean(compressor);
+ });
var unary = makePredicate("! delete");
def(AST_UnaryPrefix, function() {
return unary[this.operator];
@@ -3249,6 +3251,12 @@ merge(Compressor.prototype, {
return true;
}
def(AST_Node, return_false);
+ def(AST_Array, function() {
+ return all(this.elements);
+ });
+ def(AST_Binary, function() {
+ return this.left.is_constant_expression() && this.right.is_constant_expression();
+ });
def(AST_Constant, return_true);
def(AST_Lambda, function(scope) {
var self = this;
@@ -3277,21 +3285,15 @@ merge(Compressor.prototype, {
}));
return result;
});
- def(AST_Unary, function() {
- return this.expression.is_constant_expression();
- });
- def(AST_Binary, function() {
- return this.left.is_constant_expression() && this.right.is_constant_expression();
- });
- def(AST_Array, function() {
- return all(this.elements);
- });
def(AST_Object, function() {
return all(this.properties);
});
def(AST_ObjectProperty, function() {
return this.value.is_constant_expression();
});
+ def(AST_Unary, function() {
+ return this.expression.is_constant_expression();
+ });
})(function(node, func) {
node.DEFMETHOD("is_constant_expression", func);
});
diff --git a/test/compress/comparing.js b/test/compress/comparisons.js
index 1ff29565..434f09c9 100644
--- a/test/compress/comparing.js
+++ b/test/compress/comparisons.js
@@ -323,3 +323,25 @@ is_number_unsafe: {
}
expect_stdout: "true"
}
+
+is_boolean_var: {
+ options = {
+ comparisons: true,
+ reduce_vars: true,
+ }
+ input: {
+ console.log(function(a, b) {
+ for (var i = 0, c = !b; i < a.length; i++)
+ if (!a[i] === c)
+ return i;
+ }([ false, true ], 42));
+ }
+ expect: {
+ console.log(function(a, b) {
+ for (var i = 0, c = !b; i < a.length; i++)
+ if (!a[i] == c)
+ return i;
+ }([ false, true ], 42));
+ }
+ expect_stdout: "1"
+}