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