Skip to content

Commit

Permalink
Parser: fixed return statement parsing with invalid expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimZhestikov committed Oct 24, 2023
1 parent 2c2f29d commit c390323
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/njs_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/test/njs_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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") },

Expand Down

0 comments on commit c390323

Please sign in to comment.