[njs] Parser: fixed line counting in template literals.

Alexander Borisov alexander.borisov at nginx.com
Fri Jun 19 16:50:29 UTC 2020


details:   https://hg.nginx.org/njs/rev/7d24976ced90
branches:  
changeset: 1438:7d24976ced90
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Fri Jun 19 19:48:13 2020 +0300
description:
Parser: fixed line counting in template literals.

This closes #321 issue on GitHub.

diffstat:

 src/njs_parser.c         |  28 +++++++++++++++++-----------
 src/test/njs_unit_test.c |   7 +++++++
 2 files changed, 24 insertions(+), 11 deletions(-)

diffs (97 lines):

diff -r 341cd52d4348 -r 7d24976ced90 src/njs_parser.c
--- a/src/njs_parser.c	Fri Jun 19 19:48:12 2020 +0300
+++ b/src/njs_parser.c	Fri Jun 19 19:48:13 2020 +0300
@@ -1141,7 +1141,7 @@ njs_parser_primary_expression_test(njs_p
         parser->node = node;
 
         njs_parser_next(parser, njs_parser_template_literal);
-        break;
+        return NJS_OK;
 
     /* CoverParenthesizedExpressionAndArrowParameterList */
     case NJS_TOKEN_OPEN_PARENTHESIS:
@@ -1322,6 +1322,9 @@ njs_parser_template_literal(njs_parser_t
 
     parser->target = temp;
 
+    token->text.start++;
+    token->text.length = 0;
+
     njs_parser_next(parser, njs_parser_template_literal_string);
 
     return NJS_OK;
@@ -2199,8 +2202,6 @@ njs_parser_property(njs_parser_t *parser
 
         parser->node = node;
 
-        njs_lexer_consume_token(parser->lexer, 1);
-
         njs_parser_next(parser, njs_parser_template_literal);
 
         break;
@@ -7820,28 +7821,33 @@ njs_parser_template_string(njs_parser_t 
 
         c = *p++;
 
-        if (c == '\\') {
+        switch (c) {
+        case '\\':
             if (p == lexer->end) {
-                break;
+                return NJS_ERROR;
             }
 
             p++;
             escape = 1;
 
             continue;
-        }
-
-        if (c == '`') {
+
+        case '`':
             text->length = p - text->start - 1;
             goto done;
-        }
-
-        if (c == '$') {
+
+        case '$':
             if (p < lexer->end && *p == '{') {
                 p++;
                 text->length = p - text->start - 2;
                 goto done;
             }
+
+            break;
+
+        case '\n':
+            parser->lexer->line++;
+            break;
         }
     }
 
diff -r 341cd52d4348 -r 7d24976ced90 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Jun 19 19:48:12 2020 +0300
+++ b/src/test/njs_unit_test.c	Fri Jun 19 19:48:13 2020 +0300
@@ -6330,6 +6330,9 @@ static njs_unit_test_t  njs_test[] =
                  "foo`That ${person} is a ${age}`;"),
       njs_str("That  is a Mike21") },
 
+    { njs_str("`\n`.length"),
+      njs_str("1") },
+
     /* Strings. */
 
     { njs_str("var a = '0123456789' + '012345';"
@@ -17889,6 +17892,10 @@ static njs_unit_test_t  njs_shell_test[]
       njs_str("ReferenceError: \"a\" is not defined in 2\n"
               "    at main (:2)\n") },
 
+    { njs_str("\n`\n${Object}\n${a}`" ENTER),
+      njs_str("ReferenceError: \"a\" is not defined in 4\n"
+              "    at main (:4)\n") },
+
     { njs_str("function log(v) {}\nlog({}\n.a\n.a)" ENTER),
       njs_str("TypeError: cannot get property \"a\" of undefined\n"
               "    at main (:4)\n") },


More information about the nginx-devel mailing list