[Pkg-javascript-commits] [uglifyjs] 05/77: Fix #597

Jonas Smedegaard dr at jones.dk
Tue May 19 00:02:26 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag v2.4.18
in repository uglifyjs.

commit c75f5a1fd85368edb3456637c544dee0cad1011e
Author: Richard van Velzen <rvanvelzen1 at gmail.com>
Date:   Wed Dec 31 12:23:00 2014 +0100

    Fix #597
    
    NaN and Infinity were replaced in the output generation, instead of
    during compression. This could lead to results where `1/0` was inserted
    without parens leading to invalid output.
    
    The nodes are replaced in the compression step now, and the output
    generation returns their regular names. This should not be a problem,
    since they're already only constructed from the original name.
---
 lib/compress.js            | 20 ++++++++++++++++++--
 lib/output.js              | 10 ++--------
 test/compress/issue-597.js | 25 +++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index e63ce14..f803072 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2203,14 +2203,30 @@ merge(Compressor.prototype, {
               case "undefined":
                 return make_node(AST_Undefined, self);
               case "NaN":
-                return make_node(AST_NaN, self);
+                return make_node(AST_NaN, self).transform(compressor);
               case "Infinity":
-                return make_node(AST_Infinity, self);
+                return make_node(AST_Infinity, self).transform(compressor);
             }
         }
         return self;
     });
 
+    OPT(AST_Infinity, function (self, compressor) {
+        return make_node(AST_Binary, self, {
+            operator : '/',
+            left     : make_node(AST_Number, null, {value: 1}),
+            right    : make_node(AST_Number, null, {value: 0})
+        });
+    });
+
+    OPT(AST_NaN, function (self, compressor) {
+        return make_node(AST_Binary, self, {
+            operator : '/',
+            left     : make_node(AST_Number, null, {value: 0}),
+            right    : make_node(AST_Number, null, {value: 0})
+        });
+    });
+
     OPT(AST_Undefined, function(self, compressor){
         if (compressor.option("unsafe")) {
             var scope = compressor.find_parent(AST_Scope);
diff --git a/lib/output.js b/lib/output.js
index 7fe61af..5a8f603 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -549,12 +549,6 @@ function OutputStream(options) {
             return true;
     });
 
-    PARENS(AST_NaN, function(output){
-        var p = output.parent();
-        if (p instanceof AST_PropAccess && p.expression === this)
-            return true;
-    });
-
     PARENS([ AST_Assign, AST_Conditional ], function (output){
         var p = output.parent();
         // !(a = false) → true
@@ -1109,10 +1103,10 @@ function OutputStream(options) {
     });
     DEFPRINT(AST_Hole, noop);
     DEFPRINT(AST_Infinity, function(self, output){
-        output.print("1/0");
+        output.print("Infinity");
     });
     DEFPRINT(AST_NaN, function(self, output){
-        output.print("0/0");
+        output.print("NaN");
     });
     DEFPRINT(AST_This, function(self, output){
         output.print("this");
diff --git a/test/compress/issue-597.js b/test/compress/issue-597.js
new file mode 100644
index 0000000..3c651a4
--- /dev/null
+++ b/test/compress/issue-597.js
@@ -0,0 +1,25 @@
+NaN_and_Infinity_must_have_parens: {
+    options = {};
+    input: {
+        Infinity.toString();
+        NaN.toString();
+    }
+    expect: {
+        (1/0).toString();
+        (0/0).toString();
+    }
+}
+
+NaN_and_Infinity_should_not_be_replaced_when_they_are_redefined: {
+    options = {};
+    input: {
+        var Infinity, NaN;
+        Infinity.toString();
+        NaN.toString();
+    }
+    expect: {
+        var Infinity, NaN;
+        Infinity.toString();
+        NaN.toString();
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list