[njs] Fixed "return" usage in a true branch of an "if" statement.

Igor Sysoev igor at sysoev.ru
Wed Dec 7 12:18:02 UTC 2016


details:   http://hg.nginx.org/njs/rev/56d6fc12dc31
branches:  
changeset: 275:56d6fc12dc31
user:      Igor Sysoev <igor at sysoev.ru>
date:      Wed Dec 07 15:02:00 2016 +0300
description:
Fixed "return" usage in a true branch of an "if" statement.

diffstat:

 njs/njs_parser.c         |   4 ++++
 njs/test/njs_unit_test.c |  15 +++++++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)

diffs (39 lines):

diff -r 709ac03a4bfd -r 56d6fc12dc31 njs/njs_parser.c
--- a/njs/njs_parser.c	Mon Dec 05 17:47:00 2016 +0300
+++ b/njs/njs_parser.c	Wed Dec 07 15:02:00 2016 +0300
@@ -629,6 +629,10 @@ njs_parser_return_statement(njs_vm_t *vm
         node->right = parser->node;
         parser->node = node;
 
+        if (token == NJS_TOKEN_SEMICOLON) {
+            return njs_parser_token(parser);
+        }
+
         return token;
     }
 }
diff -r 709ac03a4bfd -r 56d6fc12dc31 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Dec 05 17:47:00 2016 +0300
+++ b/njs/test/njs_unit_test.c	Wed Dec 07 15:02:00 2016 +0300
@@ -1514,6 +1514,21 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var a = [3], b; if (1==1||2==2) { b = '1'+'2'+a[0] }; b }"),
       nxt_string("123") },
 
+    { nxt_string("(function(){ if(true) return 1 else return 0; })()"),
+      nxt_string("1") },
+
+    { nxt_string("(function(){ if(true) return 1; else return 0; })()"),
+      nxt_string("1") },
+
+    { nxt_string("(function(){ if(true) return 1;; else return 0; })()"),
+      nxt_string("SyntaxError: Unexpected token \"else\" in 1") },
+
+    { nxt_string("(function(){ if(true) return 1\n else return 0; })()"),
+      nxt_string("1") },
+
+    { nxt_string("(function(){ if(true) return 1\n;\n else return 0; })()"),
+      nxt_string("1") },
+
     /* do while. */
 
     { nxt_string("do { break } if (false)"),


More information about the nginx-devel mailing list