introduce assignments (#3345)

This commit is contained in:
Alex Lam S.L
2019-03-18 21:28:41 +08:00
committed by GitHub
parent 7aa7f21872
commit 615ae37ca3
8 changed files with 124 additions and 9 deletions

View File

@@ -49,6 +49,7 @@ function Compressor(options, false_by_default) {
TreeTransformer.call(this, this.before, this.after);
this.options = defaults(options, {
arguments : !false_by_default,
assignments : !false_by_default,
booleans : !false_by_default,
collapse_vars : !false_by_default,
comparisons : !false_by_default,
@@ -2415,6 +2416,10 @@ merge(Compressor.prototype, {
def(AST_Sequence, function(compressor) {
return this.tail_node().is_number(compressor);
});
def(AST_SymbolRef, function(compressor) {
var fixed = this.fixed_value();
return fixed && fixed.is_number(compressor);
});
var unary = makePredicate("+ - ~ ++ --");
def(AST_Unary, function() {
return unary[this.operator];
@@ -2426,22 +2431,26 @@ merge(Compressor.prototype, {
// methods to determine if an expression has a string result type
(function(def) {
def(AST_Node, return_false);
def(AST_String, return_true);
def(AST_UnaryPrefix, function() {
return this.operator == "typeof";
def(AST_Assign, function(compressor) {
return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
});
def(AST_Binary, function(compressor) {
return this.operator == "+" &&
(this.left.is_string(compressor) || this.right.is_string(compressor));
});
def(AST_Assign, function(compressor) {
return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
def(AST_Conditional, function(compressor) {
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
});
def(AST_Sequence, function(compressor) {
return this.tail_node().is_string(compressor);
});
def(AST_Conditional, function(compressor) {
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
def(AST_String, return_true);
def(AST_SymbolRef, function(compressor) {
var fixed = this.fixed_value();
return fixed && fixed.is_string(compressor);
});
def(AST_UnaryPrefix, function() {
return this.operator == "typeof";
});
})(function(node, func) {
node.DEFMETHOD("is_string", func);
@@ -6010,6 +6019,7 @@ merge(Compressor.prototype, {
|| parent instanceof AST_Sequence && parent.tail_node() === node);
}
self = self.lift_sequences(compressor);
if (!compressor.option("assignments")) return self;
if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
// x = expr1 OP expr2
if (self.right.left instanceof AST_SymbolRef
@@ -6028,6 +6038,16 @@ merge(Compressor.prototype, {
self.right = self.right.left;
}
}
if ((self.operator == "+=" || self.operator == "-=")
&& self.left.is_number(compressor)
&& self.right instanceof AST_Number
&& self.right.getValue() === 1) {
var op = self.operator.slice(0, -1);
return make_node(AST_UnaryPrefix, self, {
operator: op + op,
expression: self.left
});
}
return self;
function in_try(level, node) {