Evaluate "foo".length ==> 3
This commit is contained in:
@@ -775,6 +775,14 @@ merge(Compressor.prototype, {
|
|||||||
if (d && d.constant && d.init) return ev(d.init, compressor);
|
if (d && d.constant && d.init) return ev(d.init, compressor);
|
||||||
throw def;
|
throw def;
|
||||||
});
|
});
|
||||||
|
def(AST_Dot, function(compressor){
|
||||||
|
if (compressor.option("unsafe") && this.property == "length") {
|
||||||
|
var str = ev(this.expression, compressor);
|
||||||
|
if (typeof str == "string")
|
||||||
|
return str.length;
|
||||||
|
}
|
||||||
|
throw def;
|
||||||
|
});
|
||||||
})(function(node, func){
|
})(function(node, func){
|
||||||
node.DEFMETHOD("_eval", func);
|
node.DEFMETHOD("_eval", func);
|
||||||
});
|
});
|
||||||
@@ -2349,7 +2357,7 @@ merge(Compressor.prototype, {
|
|||||||
return make_node(AST_Dot, self, {
|
return make_node(AST_Dot, self, {
|
||||||
expression : self.expression,
|
expression : self.expression,
|
||||||
property : prop
|
property : prop
|
||||||
});
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
var v = parseFloat(prop);
|
var v = parseFloat(prop);
|
||||||
if (!isNaN(v) && v.toString() == prop) {
|
if (!isNaN(v) && v.toString() == prop) {
|
||||||
@@ -2361,6 +2369,10 @@ merge(Compressor.prototype, {
|
|||||||
return self;
|
return self;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
OPT(AST_Dot, function(self, compressor){
|
||||||
|
return self.evaluate(compressor)[0];
|
||||||
|
});
|
||||||
|
|
||||||
function literals_in_boolean_context(self, compressor) {
|
function literals_in_boolean_context(self, compressor) {
|
||||||
if (compressor.option("booleans") && compressor.in_boolean_context()) {
|
if (compressor.option("booleans") && compressor.in_boolean_context()) {
|
||||||
return make_node(AST_True, self);
|
return make_node(AST_True, self);
|
||||||
|
|||||||
@@ -52,3 +52,23 @@ dot_properties_es5: {
|
|||||||
a[""] = "whitespace";
|
a[""] = "whitespace";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
evaluate_length: {
|
||||||
|
options = {
|
||||||
|
properties: true,
|
||||||
|
unsafe: true,
|
||||||
|
evaluate: true
|
||||||
|
};
|
||||||
|
input: {
|
||||||
|
a = "foo".length;
|
||||||
|
a = ("foo" + "bar")["len" + "gth"];
|
||||||
|
a = b.length;
|
||||||
|
a = ("foo" + b).length;
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
a = 3;
|
||||||
|
a = 6;
|
||||||
|
a = b.length;
|
||||||
|
a = ("foo" + b).length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user