diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 9e2059c27df99093f40076b6b1ea5a46dc84feab..548e56639acf299e4a0c32127538e53f54237329 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1869,8 +1869,10 @@ void ETSParser::ParseCatchParamTypeAnnotation([[maybe_unused]] ir::AnnotatedExpr ir::Statement *ETSParser::ParseImportDeclaration([[maybe_unused]] StatementParsingFlags flags) { + bool isError = false; if ((flags & StatementParsingFlags::GLOBAL) == 0) { LogError(diagnostic::IMPORT_TOP_LEVEL); + isError = true; } char32_t nextChar = Lexer()->Lookahead(); @@ -1897,7 +1899,7 @@ ir::Statement *ETSParser::ParseImportDeclaration([[maybe_unused]] StatementParsi importDeclaration = ParseImportPathBuildImport(std::move(specifiers), false, startLoc, ir::ImportKinds::ALL); } - return importDeclaration; + return isError ? AllocBrokenStatement(startLoc) : importDeclaration; } ir::Statement *ETSParser::ParseExportDeclaration([[maybe_unused]] StatementParsingFlags flags) diff --git a/ets2panda/test/ast/compiler/ets/import_tests/import_within_scope/scope_import_1.ets b/ets2panda/test/ast/compiler/ets/import_tests/import_within_scope/scope_import_1.ets index f6843e6780a276d26096a0d9f027689546df13a9..e7628a6a7050e85f4cba2a350b55735ecfc3e17e 100644 --- a/ets2panda/test/ast/compiler/ets/import_tests/import_within_scope/scope_import_1.ets +++ b/ets2panda/test/ast/compiler/ets/import_tests/import_within_scope/scope_import_1.ets @@ -18,4 +18,3 @@ namespace ns { } /* @@? 17:5 Error SyntaxError: Import declarations can only be used on the top level and before any other declaration, top level statement or directive. */ -/* @@? 17:12 Error TypeError: Variable 'nsi' has already been declared. */ diff --git a/ets2panda/test/ast/compiler/ets/parser_import.ets b/ets2panda/test/ast/compiler/ets/parser_import.ets new file mode 100644 index 0000000000000000000000000000000000000000..a6035154d0832851a04f8e738e322c16c2795fe0 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/parser_import.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 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. + */ + +let callbackc = (arrElem: JSValue) => { + import {a} from +} + +/* @@? 17:5 Error SyntaxError: Import declarations can only be used on the top level and before any other declaration, top level statement or directive. */ +/* @@? 18:1 Error SyntaxError: Unexpected token, expected 'from' */ +/* @@? 19:1 Error SyntaxError: Unexpected token, expected string literal */ +/* @@? 19:1 Error SyntaxError: Expected '}', got 'end of stream' */