From c3903238ae0ce33af623ac3ed2a94480e7e82334 Mon Sep 17 00:00:00 2001 From: Vadim Zhestikov Date: Mon, 23 Oct 2023 21:19:03 -0700 Subject: [PATCH] Parser: fixed return statement parsing with invalid expression. --- src/njs_parser.c | 13 ++++++++----- src/test/njs_unit_test.c | 9 +++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/njs_parser.c b/src/njs_parser.c index 5b1b2aa78..f47b80b64 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -6347,10 +6347,12 @@ njs_parser_return_statement(njs_parser_t *parser, njs_lexer_token_t *token, parser->node = NULL; - njs_parser_next(parser, njs_parser_expression); + if (token->type != NJS_TOKEN_CLOSE_BRACE) { + njs_parser_next(parser, njs_parser_expression); - return njs_parser_after(parser, current, node, 0, - njs_parser_return_statement_after); + return njs_parser_after(parser, current, node, 0, + njs_parser_return_statement_after); + } } parser->node = node; @@ -6364,8 +6366,9 @@ njs_parser_return_statement_after(njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current) { if (parser->ret != NJS_OK) { - parser->node = parser->target; - return njs_parser_stack_pop(parser); + njs_parser_syntax_error(parser, "Unexpected token \"%V\"", + &token->text); + return NJS_DONE; } if (njs_parser_expect_semicolon(parser, token) != NJS_OK) { diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index bec1f53f0..014f81037 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -10093,6 +10093,15 @@ static njs_unit_test_t njs_test[] = { njs_str("\n{\nreturn;\n}"), njs_str("SyntaxError: Illegal return statement in 3") }, + { njs_str("function f () {return a +}"), + njs_str("SyntaxError: Unexpected token \"}\" in 1") }, + + { njs_str("`${function(){return n=>}}`"), + njs_str("SyntaxError: Unexpected token \"}\" in 1") }, + + { njs_str("(function(){return a +})"), + njs_str("SyntaxError: Unexpected token \"}\" in 1") }, + { njs_str("if (1) function f(){}"), njs_str("SyntaxError: Functions can only be declared at top level or inside a block in 1") },