1 /**
2 * Copyright (c) 2021-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 "tsPropertySignature.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)23 void TSPropertySignature::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
24 {
25 if (auto *transformedNode = cb(key_); key_ != transformedNode) {
26 key_->SetTransformedNode(transformationName, transformedNode);
27 key_ = transformedNode->AsExpression();
28 }
29
30 if (auto *const typeAnnotation = TypeAnnotation(); typeAnnotation != nullptr) {
31 if (auto *transformedNode = cb(typeAnnotation); typeAnnotation != transformedNode) {
32 typeAnnotation->SetTransformedNode(transformationName, transformedNode);
33 SetTsTypeAnnotation(static_cast<TypeNode *>(transformedNode));
34 }
35 }
36 }
37
Iterate(const NodeTraverser & cb) const38 void TSPropertySignature::Iterate(const NodeTraverser &cb) const
39 {
40 cb(key_);
41
42 if (TypeAnnotation() != nullptr) {
43 cb(TypeAnnotation());
44 }
45 }
46
Dump(ir::AstDumper * dumper) const47 void TSPropertySignature::Dump(ir::AstDumper *dumper) const
48 {
49 dumper->Add({{"type", "TSPropertySignature"},
50 {"computed", computed_},
51 {"optional", optional_},
52 {"readonly", readonly_},
53 {"key", key_},
54 {"typeAnnotation", AstDumper::Optional(TypeAnnotation())}});
55 }
56
Dump(ir::SrcDumper * dumper) const57 void TSPropertySignature::Dump(ir::SrcDumper *dumper) const
58 {
59 dumper->Add("TSPropertySignature");
60 }
61
Compile(compiler::PandaGen * pg) const62 void TSPropertySignature::Compile(compiler::PandaGen *pg) const
63 {
64 pg->GetAstCompiler()->Compile(this);
65 }
66
Compile(compiler::ETSGen * etsg) const67 void TSPropertySignature::Compile(compiler::ETSGen *etsg) const
68 {
69 etsg->GetAstCompiler()->Compile(this);
70 }
71
Check(checker::TSChecker * checker)72 checker::Type *TSPropertySignature::Check(checker::TSChecker *checker)
73 {
74 return checker->GetAnalyzer()->Check(this);
75 }
76
Check(checker::ETSChecker * checker)77 checker::VerifiedType TSPropertySignature::Check(checker::ETSChecker *checker)
78 {
79 return {this, checker->GetAnalyzer()->Check(this)};
80 }
81
Clone(ArenaAllocator * const allocator,AstNode * const parent)82 TSPropertySignature *TSPropertySignature::Clone(ArenaAllocator *const allocator, AstNode *const parent)
83 {
84 auto *const key = key_ != nullptr ? key_->Clone(allocator, nullptr)->AsExpression() : nullptr;
85 auto *const typeAnnotation = TypeAnnotation()->Clone(allocator, nullptr);
86
87 auto *const clone = allocator->New<TSPropertySignature>(key, typeAnnotation, computed_, optional_, readonly_);
88 ES2PANDA_ASSERT(clone != nullptr);
89
90 if (parent != nullptr) {
91 clone->SetParent(parent);
92 }
93 if (key != nullptr) {
94 key->SetParent(clone);
95 }
96 typeAnnotation->SetParent(clone);
97
98 return clone;
99 }
100 } // namespace ark::es2panda::ir
101