Skip to content

Commit

Permalink
enhance comparisons (#3347)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Mar 18, 2019
1 parent 615ae37 commit c520e99
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
60 changes: 31 additions & 29 deletions lib/compress.js
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
Expand Down
22 changes: 22 additions & 0 deletions test/compress/comparing.js → test/compress/comparisons.js
Expand Up @@ -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"
}

0 comments on commit c520e99

Please sign in to comment.