minor improvement to string optimisation (#1514)
- "" + "a" => "a" - "" + a + "b" => a + "b" - "a" + "" => "a" (improving on #45)
This commit is contained in:
@@ -1054,12 +1054,6 @@ merge(Compressor.prototype, {
|
|||||||
def(AST_Conditional, function(compressor){
|
def(AST_Conditional, function(compressor){
|
||||||
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
|
return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
|
||||||
});
|
});
|
||||||
def(AST_Call, function(compressor){
|
|
||||||
return compressor.option("unsafe")
|
|
||||||
&& this.expression instanceof AST_SymbolRef
|
|
||||||
&& this.expression.name == "String"
|
|
||||||
&& this.expression.undeclared();
|
|
||||||
});
|
|
||||||
})(function(node, func){
|
})(function(node, func){
|
||||||
node.DEFMETHOD("is_string", func);
|
node.DEFMETHOD("is_string", func);
|
||||||
});
|
});
|
||||||
@@ -2985,10 +2979,25 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (self.operator == "+" && self.right instanceof AST_String
|
if (self.operator == "+") {
|
||||||
&& self.right.getValue() === "" && self.left instanceof AST_Binary
|
if (self.right instanceof AST_String
|
||||||
&& self.left.operator == "+" && self.left.is_string(compressor)) {
|
&& self.right.getValue() == ""
|
||||||
return self.left;
|
&& self.left.is_string(compressor)) {
|
||||||
|
return self.left;
|
||||||
|
}
|
||||||
|
if (self.left instanceof AST_String
|
||||||
|
&& self.left.getValue() == ""
|
||||||
|
&& self.right.is_string(compressor)) {
|
||||||
|
return self.right;
|
||||||
|
}
|
||||||
|
if (self.left instanceof AST_Binary
|
||||||
|
&& self.left.operator == "+"
|
||||||
|
&& self.left.left instanceof AST_String
|
||||||
|
&& self.left.left.getValue() == ""
|
||||||
|
&& self.right.is_string(compressor)) {
|
||||||
|
self.left = self.left.right;
|
||||||
|
return self.transform(compressor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (compressor.option("evaluate")) {
|
if (compressor.option("evaluate")) {
|
||||||
switch (self.operator) {
|
switch (self.operator) {
|
||||||
|
|||||||
@@ -164,3 +164,53 @@ concat_6: {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
concat_7: {
|
||||||
|
input: {
|
||||||
|
console.log(
|
||||||
|
"" + 1,
|
||||||
|
"" + "1",
|
||||||
|
"" + 1 + 2,
|
||||||
|
"" + 1 + "2",
|
||||||
|
"" + "1" + 2,
|
||||||
|
"" + "1" + "2",
|
||||||
|
"" + (x += "foo")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(
|
||||||
|
"" + 1,
|
||||||
|
"1",
|
||||||
|
"" + 1 + 2,
|
||||||
|
1 + "2",
|
||||||
|
"1" + 2,
|
||||||
|
"1" + "2",
|
||||||
|
x += "foo"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
concat_8: {
|
||||||
|
input: {
|
||||||
|
console.log(
|
||||||
|
1 + "",
|
||||||
|
"1" + "",
|
||||||
|
1 + 2 + "",
|
||||||
|
1 + "2" + "",
|
||||||
|
"1" + 2 + "",
|
||||||
|
"1" + "2" + "",
|
||||||
|
(x += "foo") + ""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(
|
||||||
|
1 + "",
|
||||||
|
"1",
|
||||||
|
1 + 2 + "",
|
||||||
|
1 + "2",
|
||||||
|
"1" + 2,
|
||||||
|
"1" + "2",
|
||||||
|
x += "foo"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user