1 /**
2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ir/ets/etsReExportDeclaration.h"
17 #include "ir/astDump.h"
18 #include "checker/checker.h"
19
20 namespace ark::es2panda::ir {
21
ETSReExportDeclaration(ETSImportDeclaration * etsImportDeclarations,const std::vector<std::string> & userPaths,util::StringView programPath,ArenaAllocator * allocator)22 ETSReExportDeclaration::ETSReExportDeclaration(ETSImportDeclaration *etsImportDeclarations,
23 const std::vector<std::string> &userPaths, util::StringView programPath,
24 ArenaAllocator *allocator)
25 : Statement(AstNodeType::REEXPORT_STATEMENT),
26 etsImportDeclarations_(etsImportDeclarations),
27 userPaths_(allocator->Adapter()),
28 programPath_(programPath)
29 {
30 for (const auto &path : userPaths) {
31 userPaths_.emplace_back(util::UString(path, allocator).View());
32 }
33 }
34
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)35 void ETSReExportDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
36 {
37 if (etsImportDeclarations_ != nullptr) {
38 if (auto *transformedNode = cb(etsImportDeclarations_); etsImportDeclarations_ != transformedNode) {
39 etsImportDeclarations_->SetTransformedNode(transformationName, transformedNode);
40 etsImportDeclarations_ = transformedNode->AsETSImportDeclaration();
41 }
42 }
43 }
44
Iterate(const NodeTraverser & cb) const45 void ETSReExportDeclaration::Iterate(const NodeTraverser &cb) const
46 {
47 etsImportDeclarations_->Iterate(cb);
48 }
49
Dump(ir::AstDumper * dumper) const50 void ETSReExportDeclaration::Dump(ir::AstDumper *dumper) const
51 {
52 dumper->Add({{"type", "ETSReExportDeclaration"}, {"ets_import_declarations", etsImportDeclarations_}});
53 }
54
Dump(ir::SrcDumper * dumper) const55 void ETSReExportDeclaration::Dump([[maybe_unused]] ir::SrcDumper *dumper) const
56 {
57 auto importDeclaration = GetETSImportDeclarations();
58 const auto &specifiers = importDeclaration->Specifiers();
59 dumper->Add("export ");
60 if (specifiers.size() == 1 &&
61 (specifiers[0]->IsImportNamespaceSpecifier() || specifiers[0]->IsImportDefaultSpecifier())) {
62 specifiers[0]->Dump(dumper);
63 } else {
64 dumper->Add("{ ");
65 for (auto specifier : specifiers) {
66 specifier->Dump(dumper);
67 if (specifier != specifiers.back()) {
68 dumper->Add(", ");
69 }
70 }
71 dumper->Add(" }");
72 }
73
74 dumper->Add(" from ");
75 importDeclaration->Source()->Dump(dumper);
76 dumper->Add(";");
77 dumper->Endl();
78 }
79
Check(checker::ETSChecker *)80 checker::VerifiedType ETSReExportDeclaration::Check(checker::ETSChecker * /*checker*/)
81 {
82 return {this, nullptr};
83 }
84
Construct(ArenaAllocator * allocator)85 AstNode *ETSReExportDeclaration::Construct(ArenaAllocator *allocator)
86 {
87 return allocator->New<ETSReExportDeclaration>(nullptr, std::vector<std::string> {}, util::StringView {}, allocator);
88 }
89
CopyTo(AstNode * other) const90 void ETSReExportDeclaration::CopyTo(AstNode *other) const
91 {
92 auto otherImpl = other->AsETSReExportDeclaration();
93
94 otherImpl->etsImportDeclarations_ = etsImportDeclarations_;
95 otherImpl->userPaths_ = userPaths_;
96 otherImpl->programPath_ = programPath_;
97
98 Statement::CopyTo(other);
99 }
100 } // namespace ark::es2panda::ir
101