diff --git a/.travis.yml b/.travis.yml index 3d9a761d..b0243cf7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - "0.4" - "0.8" - "0.10" - "0.11" diff --git a/lib/compress.js b/lib/compress.js index b589aca5..fd3f7a21 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -775,6 +775,14 @@ merge(Compressor.prototype, { if (d && d.constant && d.init) return ev(d.init, compressor); 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){ node.DEFMETHOD("_eval", func); }); @@ -2349,7 +2357,7 @@ merge(Compressor.prototype, { return make_node(AST_Dot, self, { expression : self.expression, property : prop - }); + }).optimize(compressor); } var v = parseFloat(prop); if (!isNaN(v) && v.toString() == prop) { @@ -2361,6 +2369,10 @@ merge(Compressor.prototype, { return self; }); + OPT(AST_Dot, function(self, compressor){ + return self.evaluate(compressor)[0]; + }); + function literals_in_boolean_context(self, compressor) { if (compressor.option("booleans") && compressor.in_boolean_context()) { return make_node(AST_True, self); diff --git a/lib/output.js b/lib/output.js index b9637929..6c8f15aa 100644 --- a/lib/output.js +++ b/lib/output.js @@ -656,7 +656,7 @@ function OutputStream(options) { output.print("for"); output.space(); output.with_parens(function(){ - if (self.init) { + if (self.init && !(self.init instanceof AST_EmptyStatement)) { if (self.init instanceof AST_Definitions) { self.init.print(output); } else { diff --git a/package.json b/package.json index 031e3395..26d8f766 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "JavaScript parser, mangler/compressor and beautifier toolkit", "homepage": "http://lisperator.net/uglifyjs", "main": "tools/node.js", - "version": "2.4.13", + "version": "2.4.15", "engines": { "node" : ">=0.4.0" }, "maintainers": [{ "name": "Mihai Bazon", @@ -16,7 +16,7 @@ }, "dependencies": { "async" : "~0.2.6", - "source-map" : "~0.1.33", + "source-map" : "0.1.34", "optimist" : "~0.3.5", "uglify-to-browserify": "~1.0.0" }, diff --git a/test/compress/properties.js b/test/compress/properties.js index 736d9d88..39470738 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -52,3 +52,23 @@ dot_properties_es5: { 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; + } +}