]> git.kaiwu.me - njs.git/commitdiff
Parser: improved error message for import statement.
authorDmitry Volyntsev <xeioex@nginx.com>
Sat, 27 May 2023 04:54:12 +0000 (21:54 -0700)
committerDmitry Volyntsev <xeioex@nginx.com>
Sat, 27 May 2023 04:54:12 +0000 (21:54 -0700)
This closes #642 issue on Github.

src/njs_parser.c
src/test/njs_unit_test.c

index 5bf7316e99459e160ffee3a27019a146f3b217ed..68743baac0143240e5ff5f706f3f80a0b299a070 100644 (file)
@@ -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) {
index e6d07d550272f0b2793ec717fc795edfcd285b05..db5e99bcc83b2b45046fab66aaf18b1f9d27de16 100644 (file)
@@ -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") },