[njs] Add missed syntax error for await in template literal.

noreply at nginx.com noreply at nginx.com
Tue Feb 18 17:35:02 UTC 2025


details:   https://github.com/nginx/njs/commit/2abc2a123fd510f6945bc94670f93db2f0aaf659
branches:  master
commit:    2abc2a123fd510f6945bc94670f93db2f0aaf659
user:      Vadim Zhestikov <v.zhestikov at f5.com>
date:      Tue, 11 Feb 2025 15:39:29 -0800
description:
Add missed syntax error for await in template literal.

This fixes issues #836 on github.

---
 src/njs_parser.c         | 20 +++++++++++++++++++-
 src/njs_parser.h         |  1 +
 src/test/njs_unit_test.c |  6 ++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/njs_parser.c b/src/njs_parser.c
index 1f16336f..7fe596c2 100644
--- a/src/njs_parser.c
+++ b/src/njs_parser.c
@@ -2254,6 +2254,15 @@ njs_parser_initializer_assign(njs_parser_t *parser, njs_token_type_t type)
 }
 
 
+static njs_int_t
+njs_parser_tagged_template_literal_after(njs_parser_t *parser,
+    njs_lexer_token_t *token, njs_queue_link_t *current)
+{
+    parser->scope->in_tagged_template--;
+
+    return njs_parser_stack_pop(parser);
+}
+
 /*
  * 12.3 Left-Hand-Side Expressions.
  */
@@ -2334,9 +2343,12 @@ njs_parser_property(njs_parser_t *parser, njs_lexer_token_t *token,
 
         parser->node = node;
 
+        parser->scope->in_tagged_template++;
+
         njs_parser_next(parser, njs_parser_template_literal);
 
-        break;
+        return njs_parser_after(parser, current, node, 1,
+                                njs_parser_tagged_template_literal_after);
 
     default:
         return NJS_DONE;
@@ -3580,6 +3592,12 @@ njs_parser_await(njs_parser_t *parser, njs_lexer_token_t *token,
         return NJS_ERROR;
     }
 
+    if (parser->scope->in_tagged_template > 0) {
+        njs_parser_syntax_error(parser,
+                                "await in tagged template not supported");
+        return NJS_ERROR;
+    }
+
     node = njs_parser_node_new(parser, NJS_TOKEN_AWAIT);
     if (njs_slow_path(node == NULL)) {
         return NJS_ERROR;
diff --git a/src/njs_parser.h b/src/njs_parser.h
index c685d32c..db4db02d 100644
--- a/src/njs_parser.h
+++ b/src/njs_parser.h
@@ -27,6 +27,7 @@ struct njs_parser_scope_s {
     uint8_t                         dest_disable;
     uint8_t                         async;
     uint32_t                        in_args;
+    uint32_t                        in_tagged_template;
 };
 
 
diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c
index e99a6d8a..4f5e5c91 100644
--- a/src/test/njs_unit_test.c
+++ b/src/test/njs_unit_test.c
@@ -19821,6 +19821,12 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("async function f1() {try {f(await f1)} catch(e) {}}"),
       njs_str("SyntaxError: await in arguments not supported in 1") },
 
+    { njs_str("(async () => (function (){}) `${(async () => 1)(await 1)}`)()"),
+      njs_str("SyntaxError: await in arguments not supported in 1") },
+
+    { njs_str("(async () => (function (){}) `${await 1}`)()"),
+      njs_str("SyntaxError: await in tagged template not supported in 1") },
+
     { njs_str("async function af() {await encrypt({},}"),
       njs_str("SyntaxError: Unexpected token \"}\" in 1") },
 


More information about the nginx-devel mailing list