1 /**
2  * Copyright (c) 2021-2024 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 "exportNamedDeclaration.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 
24 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer &cb, std::string_view transformationName)25 void ExportNamedDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
26 {
27     for (auto *&it : decorators_) {
28         if (auto *transformedNode = cb(it); it != transformedNode) {
29             it->SetTransformedNode(transformationName, transformedNode);
30             it = transformedNode->AsDecorator();
31         }
32     }
33 
34     if (decl_ != nullptr) {
35         if (auto *transformedNode = cb(decl_); decl_ != transformedNode) {
36             decl_->SetTransformedNode(transformationName, transformedNode);
37             decl_ = transformedNode;
38         }
39     } else {
40         if (source_ != nullptr) {
41             if (auto *transformedNode = cb(source_); source_ != transformedNode) {
42                 source_->SetTransformedNode(transformationName, transformedNode);
43                 source_ = transformedNode->AsStringLiteral();
44             }
45         }
46 
47         for (auto *&it : specifiers_) {
48             if (auto *transformedNode = cb(it); it != transformedNode) {
49                 it->SetTransformedNode(transformationName, transformedNode);
50                 it = transformedNode->AsExportSpecifier();
51             }
52         }
53     }
54 }
55 
Iterate(const NodeTraverser &cb) const56 void ExportNamedDeclaration::Iterate(const NodeTraverser &cb) const
57 {
58     for (auto *it : decorators_) {
59         cb(it);
60     }
61 
62     if (decl_ != nullptr) {
63         cb(decl_);
64     } else {
65         if (source_ != nullptr) {
66             cb(source_);
67         }
68 
69         for (auto *it : specifiers_) {
70             cb(it);
71         }
72     }
73 }
74 
Dump(ir::AstDumper *dumper) const75 void ExportNamedDeclaration::Dump(ir::AstDumper *dumper) const
76 {
77     dumper->Add({{"type", "ExportNamedDeclaration"},
78                  {"decorators", AstDumper::Optional(decorators_)},
79                  {"declaration", AstDumper::Nullish(decl_)},
80                  {"source", AstDumper::Nullish(source_)},
81                  {"specifiers", specifiers_}});
82 }
83 
Dump(ir::SrcDumper *dumper) const84 void ExportNamedDeclaration::Dump(ir::SrcDumper *dumper) const
85 {
86     dumper->Add("export { ");
87     for (const auto *spec : specifiers_) {
88         spec->Dump(dumper);
89 
90         if (spec != specifiers_.back()) {
91             dumper->Add(", ");
92         }
93     }
94     dumper->Add(" }");
95 }
96 
Compile(compiler::PandaGen *pg) const97 void ExportNamedDeclaration::Compile(compiler::PandaGen *pg) const
98 {
99     pg->GetAstCompiler()->Compile(this);
100 }
101 
Compile(compiler::ETSGen *etsg) const102 void ExportNamedDeclaration::Compile(compiler::ETSGen *etsg) const
103 {
104     etsg->GetAstCompiler()->Compile(this);
105 }
106 
Check(checker::TSChecker *checker)107 checker::Type *ExportNamedDeclaration::Check(checker::TSChecker *checker)
108 {
109     return checker->GetAnalyzer()->Check(this);
110 }
111 
Check(checker::ETSChecker *checker)112 checker::Type *ExportNamedDeclaration::Check(checker::ETSChecker *checker)
113 {
114     return checker->GetAnalyzer()->Check(this);
115 }
116 }  // namespace ark::es2panda::ir
117