From 225bd6de5de4dde8baf52a8685adb4479a0e20c4 Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Tue, 7 Feb 2023 10:28:54 +0800 Subject: [PATCH] fixed f7525d9 from https://gitee.com/gavin1012_hw/ark_ts2abc/pulls/829 Fix bracket validation in ParseUnicodePropertyEscape and special character processing in ScanTemplateString Issue:I6DCWS Signed-off-by: gavin1012_hw Change-Id: Ib659ecd0a599c41f8ced5dad316f874ea3e775c5 --- es2panda/lexer/lexer.cpp | 2 +- es2panda/lexer/regexp/regexp.cpp | 2 +- .../test/parser/js/test-regexp-p-expected.txt | 71 +++++++ es2panda/test/parser/js/test-regexp-p.js | 16 ++ ...processing-in-template-string-expected.txt | 178 ++++++++++++++++++ ...character-processing-in-template-string.js | 25 +++ 6 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 es2panda/test/parser/js/test-regexp-p-expected.txt create mode 100644 es2panda/test/parser/js/test-regexp-p.js create mode 100644 es2panda/test/parser/js/test-special-character-processing-in-template-string-expected.txt create mode 100644 es2panda/test/parser/js/test-special-character-processing-in-template-string.js diff --git a/es2panda/lexer/lexer.cpp b/es2panda/lexer/lexer.cpp index 529ddea26a..3a83fb4d25 100644 --- a/es2panda/lexer/lexer.cpp +++ b/es2panda/lexer/lexer.cpp @@ -489,7 +489,7 @@ LexerTemplateString Lexer::ScanTemplateString() Iterator().Backward(1); } - if (nextCp == LEX_CHAR_BACK_TICK || nextCp == LEX_CHAR_BACKSLASH) { + if (nextCp == LEX_CHAR_BACK_TICK || nextCp == LEX_CHAR_BACKSLASH || nextCp == LEX_CHAR_DOLLAR_SIGN) { templateStr.str.Append(cp); templateStr.str.Append(nextCp); Iterator().Forward(1); diff --git a/es2panda/lexer/regexp/regexp.cpp b/es2panda/lexer/regexp/regexp.cpp index 630555df54..ea4356ce4d 100644 --- a/es2panda/lexer/regexp/regexp.cpp +++ b/es2panda/lexer/regexp/regexp.cpp @@ -680,7 +680,7 @@ void RegExpParser::ParseUnicodePropertyEscape() } char32_t ch = Next(); - if (ch == LEX_CHAR_LEFT_BRACE) { + if (ch == LEX_CHAR_RIGHT_BRACE) { break; } diff --git a/es2panda/test/parser/js/test-regexp-p-expected.txt b/es2panda/test/parser/js/test-regexp-p-expected.txt new file mode 100644 index 0000000000..31af86465c --- /dev/null +++ b/es2panda/test/parser/js/test-regexp-p-expected.txt @@ -0,0 +1,71 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "RegularExpressionLiteral", + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 25 + } + } + }, + "right": { + "type": "RegExpLiteral", + "source": "\u200C\u200D\p{ID_Continue}", + "flags": "u", + "loc": { + "start": { + "line": 16, + "column": 29 + }, + "end": { + "line": 16, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 59 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 16, + "column": 59 + } + } +} diff --git a/es2panda/test/parser/js/test-regexp-p.js b/es2panda/test/parser/js/test-regexp-p.js new file mode 100644 index 0000000000..d2e67d9dd0 --- /dev/null +++ b/es2panda/test/parser/js/test-regexp-p.js @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +RegularExpressionLiteral = /\u200C\u200D\p{ID_Continue}/u; \ No newline at end of file diff --git a/es2panda/test/parser/js/test-special-character-processing-in-template-string-expected.txt b/es2panda/test/parser/js/test-special-character-processing-in-template-string-expected.txt new file mode 100644 index 0000000000..978a429353 --- /dev/null +++ b/es2panda/test/parser/js/test-special-character-processing-in-template-string-expected.txt @@ -0,0 +1,178 @@ +{ + "type": "Program", + "statements": [ + { + "type": "LabelledStatement", + "label": { + "type": "Identifier", + "name": "iflnlf", + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 7 + } + } + }, + "body": { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "string", + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + "right": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "TemplateLiteral", + "expressions": [], + "quasis": [ + { + "type": "TemplateElement", + "value": { + "raw": " +if( + \`\${ + (()=>{ + if(true) /r/i + return Math.random() + })() + }\` > 0.5 +) /a/g +", + "cooked": " +if( + `${ + (()=>{ + if(true) /r/i + return Math.random() + })() + }` > 0.5 +) /a/g +" + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 25, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "property": { + "type": "Identifier", + "name": "trim", + "loc": { + "start": { + "line": 25, + "column": 3 + }, + "end": { + "line": 25, + "column": 7 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 25, + "column": 7 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 25, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 25, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 25, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 25, + "column": 10 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 25, + "column": 10 + } + } +} diff --git a/es2panda/test/parser/js/test-special-character-processing-in-template-string.js b/es2panda/test/parser/js/test-special-character-processing-in-template-string.js new file mode 100644 index 0000000000..9374109bdb --- /dev/null +++ b/es2panda/test/parser/js/test-special-character-processing-in-template-string.js @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +iflnlf: string = ` +if( + \`\${ + (()=>{ + if(true) /r/i + return Math.random() + })() + }\` > 0.5 +) /a/g +`.trim(); \ No newline at end of file -- Gitee