[Pkg-javascript-commits] [uglifyjs] 163/190: Stop dropping args in new expressions

Antonio Terceiro terceiro at moszumanska.debian.org
Sun Aug 7 23:17:23 UTC 2016


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

terceiro pushed a commit to annotated tag upstream/2.7.0
in repository uglifyjs.

commit 6c8e001feeeb957279814aa58be44d1ece8bdb6e
Author: Anthony Van de Gejuchte <anthonyvdgent at gmail.com>
Date:   Fri Jun 10 15:42:55 2016 +0200

    Stop dropping args in new expressions
---
 lib/output.js        | 14 +++++---------
 test/compress/new.js | 32 ++++++++++++++++++++++++++++++++
 test/mocha/new.js    | 16 ++++++++++++++--
 3 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/lib/output.js b/lib/output.js
index 907818e..7ddee48 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -594,7 +594,7 @@ function OutputStream(options) {
 
     PARENS(AST_New, function(output){
         var p = output.parent();
-        if (no_constructor_parens(this, output)
+        if (!need_constructor_parens(this, output)
             && (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]()
                 || p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
             return true;
@@ -995,7 +995,7 @@ function OutputStream(options) {
     /* -----[ other expressions ]----- */
     DEFPRINT(AST_Call, function(self, output){
         self.expression.print(output);
-        if (self instanceof AST_New && no_constructor_parens(self, output))
+        if (self instanceof AST_New && !need_constructor_parens(self, output))
             return;
         output.with_parens(function(){
             self.args.forEach(function(expr, i){
@@ -1285,13 +1285,9 @@ function OutputStream(options) {
     };
 
     // self should be AST_New.  decide if we want to show parens or not.
-    function no_constructor_parens(self, output) {
-        return self.args.length == 0 && !output.option("beautify") ||
-            !(self.expression instanceof AST_SymbolRef ||
-              self.expression instanceof AST_Call ||
-              self.expression instanceof AST_Function ||
-              self.expression instanceof AST_Assign
-            );
+    function need_constructor_parens(self, output) {
+        // Always print parentheses with arguments
+        return self.args.length > 0;
     };
 
     function best_of(a) {
diff --git a/test/compress/new.js b/test/compress/new.js
index d956ae2..78a1026 100644
--- a/test/compress/new.js
+++ b/test/compress/new.js
@@ -11,6 +11,30 @@ new_statement: {
     expect_exact: "new x(1);new x(1)(2);new x(1)(2)(3);new new x(1);new new x(1)(2);new new x(1)(2);(new new x(1))(2);"
 }
 
+new_statements_2: {
+    input: {
+        new x;
+        new new x;
+        new new new x;
+        new true;
+        new (0);
+        new (!0);
+        new (bar = function(foo) {this.foo=foo;})(123);
+        new (bar = function(foo) {this.foo=foo;})();
+    }
+    expect_exact: "new x;new(new x);new(new(new x));new true;new 0;new(!0);new(bar=function(foo){this.foo=foo})(123);new(bar=function(foo){this.foo=foo});"
+}
+
+new_statements_3: {
+    input: {
+        new (function(foo){this.foo=foo;})(1);
+        new (function(foo){this.foo=foo;})();
+        new (function test(foo){this.foo=foo;})(1);
+        new (function test(foo){this.foo=foo;})();
+    }
+    expect_exact: "new function(foo){this.foo=foo}(1);new function(foo){this.foo=foo};new function test(foo){this.foo=foo}(1);new function test(foo){this.foo=foo};"
+}
+
 new_with_rewritten_true_value: {
     options = { booleans: true }
     input: {
@@ -18,3 +42,11 @@ new_with_rewritten_true_value: {
     }
     expect_exact: "new(!0);"
 }
+
+new_with_many_parameters: {
+    input: {
+        new foo.bar("baz");
+        new x(/123/, 456);
+    }
+    expect_exact: 'new foo.bar("baz");new x(/123/,456);'
+}
diff --git a/test/mocha/new.js b/test/mocha/new.js
index 7e7aea7..8c0f24b 100644
--- a/test/mocha/new.js
+++ b/test/mocha/new.js
@@ -5,19 +5,31 @@ describe("New", function() {
     it("Should attach callback parens after some tokens", function() {
         var tests = [
             "new x(1);",
+            "new x;",
+            "new new x;",
             "new (function(foo){this.foo=foo;})(1);",
+            "new (function(foo){this.foo=foo;})();",
+            "new (function test(foo){this.foo=foo;})(1);",
+            "new (function test(foo){this.foo=foo;})();",
             "new true;",
             "new (0);",
             "new (!0);",
-            "new (bar = function(foo) {this.foo=foo;})(123);"
+            "new (bar = function(foo) {this.foo=foo;})(123);",
+            "new (bar = function(foo) {this.foo=foo;})();"
         ];
         var expected = [
             "new x(1);",
+            "new x;",
+            "new (new x);",
             "new function(foo) {\n    this.foo = foo;\n}(1);",
+            "new function(foo) {\n    this.foo = foo;\n};",
+            "new function test(foo) {\n    this.foo = foo;\n}(1);",
+            "new function test(foo) {\n    this.foo = foo;\n};",
             "new true;",
             "new 0;",
             "new (!0);",
-            "new (bar = function(foo) {\n    this.foo = foo;\n})(123);"
+            "new (bar = function(foo) {\n    this.foo = foo;\n})(123);",
+            "new (bar = function(foo) {\n    this.foo = foo;\n});"
         ];
         for (var i = 0; i < tests.length; i++) {
             assert.strictEqual(

-- 
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