From 477153c58bac119cbe056ce3c7215f9d6eb7747f Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 26 May 2023 21:54:12 -0700 Subject: [PATCH] Parser: improved error message for import statement. This closes #642 issue on Github. --- src/njs_parser.c | 9 ++++++++- src/test/njs_unit_test.c | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/njs_parser.c b/src/njs_parser.c index 5bf7316e..68743baa 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -8118,11 +8118,18 @@ njs_parser_import(njs_parser_t *parser, njs_lexer_token_t *token, return NJS_DONE; } - if (token->type != NJS_TOKEN_NAME) { + if (token->type == NJS_TOKEN_MULTIPLICATION + || token->type == NJS_TOKEN_OPEN_BRACE + || token->type == NJS_TOKEN_STRING) + { njs_parser_syntax_error(parser, "Non-default import is not supported"); return NJS_DONE; } + if (token->type != NJS_TOKEN_NAME) { + return njs_parser_failed(parser); + } + name = njs_parser_variable_node(parser, token->unique_id, NJS_VARIABLE_LET, &var); if (name == NULL) { diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index e6d07d55..db5e99bc 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -18903,12 +18903,18 @@ static njs_unit_test_t njs_test[] = /* Module. */ - { njs_str("import;"), + { njs_str("import * from y"), + njs_str("SyntaxError: Non-default import is not supported in 1") }, + + { njs_str("import 'x' from y"), njs_str("SyntaxError: Non-default import is not supported in 1") }, { njs_str("import {x} from y"), njs_str("SyntaxError: Non-default import is not supported in 1") }, + { njs_str("import switch from y"), + njs_str("SyntaxError: Unexpected token \"switch\" in 1") }, + { njs_str("import x from y"), njs_str("SyntaxError: Unexpected token \"y\" in 1") }, -- 2.47.3