diff --git a/lib/squeeze-more.js b/lib/squeeze-more.js index 0908db3e..cedf74c0 100644 --- a/lib/squeeze-more.js +++ b/lib/squeeze-more.js @@ -44,6 +44,37 @@ function ast_squeeze_more(ast) { } } }, + + // shorten Number constants: + // Number.POSITIVE_INFINITY to 1/0, Number.NEGATIVE_INFINITY to 1/-0, Number.NaN to 0/0 + "dot": function(expr, name) { + if (expr[0] == "name" && expr[1] == "Number" && !scope.has("Number")) { + if (name == "POSITIVE_INFINITY") { + return [ "binary", "/", [ "num", 1 ], [ "num", 0 ] ]; + } + if (name == "NEGATIVE_INFINITY") { + return [ "binary", "/", [ "num", 1 ], [ "num", -0 ] ]; + } + if (name == "NaN") { + return [ "binary", "/", [ "num", 0 ], [ "num", 0 ] ]; + } + } + }, + + // shorten Infinity constant to 1/0 + "name": function(name) { + if (name == "Infinity" && !scope.has("Infinity")) { + return [ "binary", "/", [ "num", 1 ], [ "num", 0 ] ]; + } + }, + + // shorten -Infinity constant to 1/-0 + "unary-prefix": function(op, expr) { + if (op == "-" && expr[0] == "name" && expr[1] == "Infinity" && !scope.has("Infinity")) { + return [ "binary", "/", [ "num", 1 ], [ "num", -0 ] ]; + } + }, + "call": function(expr, args) { if (expr[0] == "dot" && expr[1][0] == "string" && args.length == 1 && (args[0][1] > 0 && expr[2] == "substring" || expr[2] == "substr")) { diff --git a/test/unit/compress/expected/infinity.js b/test/unit/compress/expected/infinity.js new file mode 100644 index 00000000..e91a1ef3 --- /dev/null +++ b/test/unit/compress/expected/infinity.js @@ -0,0 +1 @@ +function test(){var a={};return a}1/0,-1/0; diff --git a/test/unit/compress/expected/number1.js b/test/unit/compress/expected/number1.js new file mode 100644 index 00000000..51a2a73b --- /dev/null +++ b/test/unit/compress/expected/number1.js @@ -0,0 +1 @@ +1/0,1/-0,0/0 diff --git a/test/unit/compress/expected/number2.js b/test/unit/compress/expected/number2.js new file mode 100644 index 00000000..3cc412a1 --- /dev/null +++ b/test/unit/compress/expected/number2.js @@ -0,0 +1 @@ +(function(){var a={};return a.POSITIVE_INFINITY||a.NEGATIVE_INFINITY||a.NaN})() diff --git a/test/unit/compress/test/infinity.js b/test/unit/compress/test/infinity.js new file mode 100644 index 00000000..363c616d --- /dev/null +++ b/test/unit/compress/test/infinity.js @@ -0,0 +1,8 @@ +Infinity; +-Infinity; + +function test() +{ + var Infinity = {}; + return Infinity; +} diff --git a/test/unit/compress/test/number1.js b/test/unit/compress/test/number1.js new file mode 100644 index 00000000..4753466a --- /dev/null +++ b/test/unit/compress/test/number1.js @@ -0,0 +1,3 @@ +Number.POSITIVE_INFINITY; +Number.NEGATIVE_INFINITY; +Number.NaN; diff --git a/test/unit/compress/test/number2.js b/test/unit/compress/test/number2.js new file mode 100644 index 00000000..9355cec4 --- /dev/null +++ b/test/unit/compress/test/number2.js @@ -0,0 +1,4 @@ +(function(){ + var Number = {}; + return Number.POSITIVE_INFINITY || Number.NEGATIVE_INFINITY || Number.NaN; +})();