diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index c4aecfef86e1043e117561ccd6fb00498686b119..5f4a27cc9c75ac5cf82750f4f358ba9cef2e2b83 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1582,8 +1582,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(); @@ -1610,7 +1612,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/parser_import.ets b/ets2panda/test/ast/compiler/ets/parser_import.ets new file mode 100644 index 0000000000000000000000000000000000000000..1bfb0b2f608f4bf929da0a9d407b86e92c698ad1 --- /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' */ \ No newline at end of file