Discussion:
[njs] Fixed parsing of throw statement inside if.
Dmitry Volyntsev
2018-11-22 14:39:04 UTC
Permalink
details: https://hg.nginx.org/njs/rev/b3691d847ff4
branches:
changeset: 665:b3691d847ff4
user: Dmitry Volyntsev <***@nginx.com>
date: Thu Nov 22 17:38:40 2018 +0300
description:
Fixed parsing of throw statement inside if.

diffstat:

njs/njs_parser.c | 18 +++++++++++++++++-
njs/test/njs_unit_test.c | 9 +++++++++
2 files changed, 26 insertions(+), 1 deletions(-)

diffs (47 lines):

diff -r 3b2689be3a57 -r b3691d847ff4 njs/njs_parser.c
--- a/njs/njs_parser.c Thu Nov 22 17:38:25 2018 +0300
+++ b/njs/njs_parser.c Thu Nov 22 17:38:40 2018 +0300
@@ -1696,7 +1696,23 @@ njs_parser_try_statement(njs_vm_t *vm, n

parser->node = try;

- return token;
+ switch (token) {
+
+ case NJS_TOKEN_SEMICOLON:
+ case NJS_TOKEN_LINE_END:
+ return njs_parser_token(parser);
+
+ case NJS_TOKEN_CLOSE_BRACE:
+ case NJS_TOKEN_END:
+ return token;
+
+ default:
+ if (parser->lexer->prev_token == NJS_TOKEN_LINE_END) {
+ return token;
+ }
+
+ return NJS_TOKEN_ILLEGAL;
+ }
}


diff -r 3b2689be3a57 -r b3691d847ff4 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Thu Nov 22 17:38:25 2018 +0300
+++ b/njs/test/njs_unit_test.c Thu Nov 22 17:38:40 2018 +0300
@@ -2159,6 +2159,15 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("(function(){ if(true) return 1\n;\n else return 0; })()"),
nxt_string("1") },

+ { nxt_string("function f(n) {if (n)\n throw 'foo'\nelse return 1}; f(0)"),
+ nxt_string("1") },
+
+ { nxt_string("function f(n) {if (n)\n throw 'foo'\nelse return 1}; f(1)"),
+ nxt_string("foo") },
+
+ { nxt_string("function f(n) {if (n == 1) throw 'foo'\nelse if (n == 2) return 1}; f(2)"),
+ nxt_string("1") },
+
/* do while. */

{ nxt_string("do { break } if (false)"),

Loading...