From d40d1c4313b9e9f1388b932525b129bf0c58e059 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Wed, 2 Jul 2025 10:09:24 +0800 Subject: [PATCH] =?UTF-8?q?import=20type=E5=BC=8F=E7=9A=84=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=8F=AA=E8=83=BD=E6=89=BF=E6=8E=A5export=20type?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yp9522 --- .../ast/compiler/ets/import_export/file1.ets | 17 +++++++++++++++ .../ast/compiler/ets/import_export/file2.ets | 21 +++++++++++++++++++ ets2panda/varbinder/ETSBinder.cpp | 9 ++++++-- ets2panda/varbinder/ETSBinder.h | 2 +- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/import_export/file1.ets create mode 100644 ets2panda/test/ast/compiler/ets/import_export/file2.ets diff --git a/ets2panda/test/ast/compiler/ets/import_export/file1.ets b/ets2panda/test/ast/compiler/ets/import_export/file1.ets new file mode 100644 index 0000000000..f5ba4c3944 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/import_export/file1.ets @@ -0,0 +1,17 @@ +/* + * 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. + */ + +export default class A {} +export class B {} \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/import_export/file2.ets b/ets2panda/test/ast/compiler/ets/import_export/file2.ets new file mode 100644 index 0000000000..01cd224637 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/import_export/file2.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +import type { B } from /* @@ label*/'./file1'; +import type { A } from /* @@ label2*/'./file1'; + +/* @@? 16:37 SyntaxError: Type import requires selective binding to define the required imported elements. */ +/* @@? 17:38 SyntaxError: Type import requires selective binding to define the required imported elements. */ +/* @@? 17:38 TypeError: Use the default import syntax to import a default exported element */ \ No newline at end of file diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index ce4d9475a3..92a53ff25e 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -871,8 +871,13 @@ std::pair ETSBinder::FindImportDeclIn } void ETSBinder::ValidateImportVariable(const ir::AstNode *node, const util::StringView &imported, - const ir::StringLiteral *const importPath) + const ir::StringLiteral *const importPath, + const ir::ETSImportDeclaration *import) { + bool isTypeImport = import != nullptr && import->IsTypeKind(); + if (isTypeImport && !node->IsExportedType()) { + ThrowError(importPath->Start(), diagnostic::TYPE_IMPORT_MISSING_SELECTIVE_BINDING, {imported}); + } if (node->IsDefaultExported()) { ThrowError(importPath->Start(), diagnostic::DEFAULT_EXPORT_DIRECT_IMPORTED); } else if (!node->IsExported() && !node->IsDefaultExported()) { @@ -978,7 +983,7 @@ bool ETSBinder::AddImportSpecifiersToTopBindings(Span re auto *node = FindNodeInAliasMap(import->ResolvedSource(), imported); - ValidateImportVariable(node != nullptr ? node : var->Declaration()->Node(), imported, importPath); + ValidateImportVariable(node != nullptr ? node : var->Declaration()->Node(), imported, importPath, import); const auto localName = importSpecifier->Local()->Name(); auto varInGlobalClassScope = Program()->GlobalClassScope()->FindLocal(localName, ResolveBindingOptions::ALL); diff --git a/ets2panda/varbinder/ETSBinder.h b/ets2panda/varbinder/ETSBinder.h index 001a02cb10..f82935bf25 100644 --- a/ets2panda/varbinder/ETSBinder.h +++ b/ets2panda/varbinder/ETSBinder.h @@ -174,7 +174,7 @@ public: ir::ImportDefaultSpecifier *importDefaultSpecifier, const ir::ETSImportDeclaration *import); void ValidateImportVariable(const ir::AstNode *node, const util::StringView &imported, - const ir::StringLiteral *const importPath); + const ir::StringLiteral *const importPath, const ir::ETSImportDeclaration *import); Variable *FindImportSpecifiersVariable(const util::StringView &imported, const varbinder::Scope::VariableMap &globalBindings, Span record); -- Gitee