From d20a08f697024b2617665e3aba784ed3897e6958 Mon Sep 17 00:00:00 2001 From: huyunhui Date: Tue, 30 Jan 2024 06:51:31 +0000 Subject: [PATCH] fix unexpected error when decoding failed Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8ZWUV Signed-off-by: huyunhui Change-Id: I1cffe20b9394abe69ff1180d403ae2bc24cc7778 --- es2panda/lexer/token/sourceLocation.cpp | 15 ++++++------- ...in-accessing-private-property-expected.txt | 1 + ...tional-chain-accessing-private-property.js | 21 +++++++++++++++++++ ...syntax-error-in-non-utf8-file-expected.txt | 1 + .../unicode/syntax-error-in-non-utf8-file.js | 17 +++++++++++++++ es2panda/test/runner.py | 1 + 6 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 es2panda/test/compiler/js/language/expressions/optional-chain/optional-chain-accessing-private-property-expected.txt create mode 100644 es2panda/test/compiler/js/language/expressions/optional-chain/optional-chain-accessing-private-property.js create mode 100644 es2panda/test/parser/unicode/syntax-error-in-non-utf8-file-expected.txt create mode 100644 es2panda/test/parser/unicode/syntax-error-in-non-utf8-file.js diff --git a/es2panda/lexer/token/sourceLocation.cpp b/es2panda/lexer/token/sourceLocation.cpp index 8cb80a458b..bf4ff240f3 100644 --- a/es2panda/lexer/token/sourceLocation.cpp +++ b/es2panda/lexer/token/sourceLocation.cpp @@ -47,14 +47,15 @@ LineIndex::LineIndex(const util::StringView &source) noexcept bool nextEntry = false; while (true) { - switch (iter.Next()) { - case util::StringView::Iterator::INVALID_CP: { - if (!nextEntry) { - // Add the last entry if the ending character is not LEX_CHAR_LF / LEX_CHAR_PS / LEX_CHAR_LS - entrys_.emplace_back(iter.Index()); - } - return; + if (!iter.HasNext()) { + if (!nextEntry) { + // Add the last entry if the ending character is not LEX_CHAR_LF / LEX_CHAR_PS / LEX_CHAR_LS + entrys_.emplace_back(iter.Index()); } + return; + } + + switch (iter.Next()) { case LEX_CHAR_CR: { if (iter.HasNext() && iter.Peek() == LEX_CHAR_LF) { iter.Forward(1); diff --git a/es2panda/test/compiler/js/language/expressions/optional-chain/optional-chain-accessing-private-property-expected.txt b/es2panda/test/compiler/js/language/expressions/optional-chain/optional-chain-accessing-private-property-expected.txt new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/es2panda/test/compiler/js/language/expressions/optional-chain/optional-chain-accessing-private-property-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/js/language/expressions/optional-chain/optional-chain-accessing-private-property.js b/es2panda/test/compiler/js/language/expressions/optional-chain/optional-chain-accessing-private-property.js new file mode 100644 index 0000000000..569ef35565 --- /dev/null +++ b/es2panda/test/compiler/js/language/expressions/optional-chain/optional-chain-accessing-private-property.js @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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. + */ + +class A { + static #a = 1; + static { + print(this?.#a); + } +} \ No newline at end of file diff --git a/es2panda/test/parser/unicode/syntax-error-in-non-utf8-file-expected.txt b/es2panda/test/parser/unicode/syntax-error-in-non-utf8-file-expected.txt new file mode 100644 index 0000000000..5ed2c53ad9 --- /dev/null +++ b/es2panda/test/parser/unicode/syntax-error-in-non-utf8-file-expected.txt @@ -0,0 +1 @@ +SyntaxError: Primary expression expected [syntax-error-in-non-utf8-file.js:18:1] diff --git a/es2panda/test/parser/unicode/syntax-error-in-non-utf8-file.js b/es2panda/test/parser/unicode/syntax-error-in-non-utf8-file.js new file mode 100644 index 0000000000..38cbe81bf4 --- /dev/null +++ b/es2panda/test/parser/unicode/syntax-error-in-non-utf8-file.js @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2024 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. + */ + +b = {n:"Šo"}; +function a() { \ No newline at end of file diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index 81c2a64419..7782cf91fb 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -1523,6 +1523,7 @@ def main(): runner.add_directory("parser/js/language/statements/for-statement", "js", ["--parse-only", "--dump-ast"]) runner.add_directory("parser/js/language/expressions/optional-chain", "js", ["--parse-only", "--dump-ast"]) runner.add_directory("parser/sendable_class", "ts", ["--dump-assembly", "--dump-literal-buffer", "--module"]) + runner.add_directory("parser/unicode", "js", ["--parse-only"]) runners.append(runner) -- Gitee