From 3214536bcc24ba2cd0bd554313efd48ab7984da6 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Tue, 14 Jul 2020 17:20:29 +0000 Subject: [PATCH] Parser: fixed regexp-literals parsing with '=' characters. Previously, njs lexer decoded '/=' as an assignment token. This closes #329 issue on Github. --- src/njs_parser.c | 5 +++++ src/test/njs_unit_test.c | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/njs_parser.c b/src/njs_parser.c index 5fb5a863..4803292c 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -1115,6 +1115,7 @@ njs_parser_primary_expression_test(njs_parser_t *parser, /* RegularExpressionLiteral */ case NJS_TOKEN_DIVISION: + case NJS_TOKEN_DIVISION_ASSIGNMENT: node = njs_parser_node_new(parser, NJS_TOKEN_REGEXP); if (node == NULL) { return NJS_ERROR; @@ -1201,6 +1202,10 @@ njs_parser_regexp_literal(njs_parser_t *parser, njs_lexer_token_t *token, value = &parser->node->u.value; lexer = parser->lexer; + if (token->type == NJS_TOKEN_DIVISION_ASSIGNMENT) { + lexer->start--; + } + for (p = lexer->start; p < lexer->end; p++) { switch (*p) { diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 1a6dee39..5b184808 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -7582,6 +7582,9 @@ static njs_unit_test_t njs_test[] = { njs_str("'abc'.replace(/f/, 'X')"), njs_str("abc") }, + { njs_str("'AB=C==='.replace(/=*$/, '')"), + njs_str("AB=C") }, + { njs_str("('a'.repeat(33) + 'bb').replace(/bb/, 'CC').slice(31)"), njs_str("aaCC") }, @@ -7784,6 +7787,9 @@ static njs_unit_test_t njs_test[] = { njs_str("/]/"), njs_str("/\\]/") }, + { njs_str("/=/"), + njs_str("/=/") }, + { njs_str("/["), njs_str("SyntaxError: Unterminated RegExp \"/[\" in 1") }, -- 2.47.3