[njs] Parser: improved error message for import statement.

Dmitry Volyntsev xeioex at nginx.com
Sat May 27 17:06:18 UTC 2023


details:   https://hg.nginx.org/njs/rev/2e8563c8143b
branches:  
changeset: 2142:2e8563c8143b
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 26 21:54:12 2023 -0700
description:
Parser: improved error message for import statement.

This closes #642 issue on Github.

diffstat:

 src/njs_parser.c         |  9 ++++++++-
 src/test/njs_unit_test.c |  8 +++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diffs (46 lines):

diff -r 9213a609cb17 -r 2e8563c8143b src/njs_parser.c
--- a/src/njs_parser.c	Fri May 26 21:05:15 2023 -0700
+++ b/src/njs_parser.c	Fri May 26 21:54:12 2023 -0700
@@ -8118,11 +8118,18 @@ njs_parser_import(njs_parser_t *parser, 
         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 -r 9213a609cb17 -r 2e8563c8143b src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri May 26 21:05:15 2023 -0700
+++ b/src/test/njs_unit_test.c	Fri May 26 21:54:12 2023 -0700
@@ -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") },
 


More information about the nginx-devel mailing list