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 "tsIndexSignature.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 {
Kind() const23 TSIndexSignature::TSIndexSignatureKind TSIndexSignature::Kind() const noexcept
24 {
25 return param_->AsIdentifier()->TypeAnnotation()->IsTSNumberKeyword() ? TSIndexSignatureKind::NUMBER
26 : TSIndexSignatureKind::STRING;
27 }
28
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)29 void TSIndexSignature::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
30 {
31 if (auto *transformedNode = cb(param_); param_ != transformedNode) {
32 param_->SetTransformedNode(transformationName, transformedNode);
33 param_ = transformedNode->AsExpression();
34 }
35
36 if (auto *transformedNode = cb(typeAnnotation_); typeAnnotation_ != transformedNode) {
37 typeAnnotation_->SetTransformedNode(transformationName, transformedNode);
38 typeAnnotation_ = static_cast<TypeNode *>(transformedNode);
39 }
40 }
41
Iterate(const NodeTraverser & cb) const42 void TSIndexSignature::Iterate(const NodeTraverser &cb) const
43 {
44 cb(param_);
45 cb(typeAnnotation_);
46 }
47
Dump(ir::AstDumper * dumper) const48 void TSIndexSignature::Dump(ir::AstDumper *dumper) const
49 {
50 dumper->Add({{"type", "TSIndexSignature"},
51 {"parameters", param_},
52 {"typeAnnotation", typeAnnotation_},
53 {"readonly", readonly_}});
54 }
55
Dump(ir::SrcDumper * dumper) const56 void TSIndexSignature::Dump(ir::SrcDumper *dumper) const
57 {
58 dumper->Add("TSIndexSignature");
59 }
60
Compile(compiler::PandaGen * pg) const61 void TSIndexSignature::Compile(compiler::PandaGen *pg) const
62 {
63 pg->GetAstCompiler()->Compile(this);
64 }
65
Compile(compiler::ETSGen * etsg) const66 void TSIndexSignature::Compile(compiler::ETSGen *etsg) const
67 {
68 etsg->GetAstCompiler()->Compile(this);
69 }
70
Check(checker::TSChecker * checker)71 checker::Type *TSIndexSignature::Check(checker::TSChecker *checker)
72 {
73 return checker->GetAnalyzer()->Check(this);
74 }
75
Check(checker::ETSChecker * checker)76 checker::VerifiedType TSIndexSignature::Check(checker::ETSChecker *checker)
77 {
78 return {this, checker->GetAnalyzer()->Check(this)};
79 }
80
Clone(ArenaAllocator * const allocator,AstNode * const parent)81 TSIndexSignature *TSIndexSignature::Clone(ArenaAllocator *const allocator, AstNode *const parent)
82 {
83 auto *const param = param_ != nullptr ? param_->Clone(allocator, nullptr)->AsExpression() : nullptr;
84 auto *const typeAnnotation = typeAnnotation_->Clone(allocator, nullptr);
85 auto *const clone = allocator->New<TSIndexSignature>(param, typeAnnotation, readonly_);
86 ES2PANDA_ASSERT(clone != nullptr);
87
88 if (parent != nullptr) {
89 clone->SetParent(parent);
90 }
91 if (param != nullptr) {
92 param->SetParent(clone);
93 }
94 typeAnnotation->SetParent(clone);
95
96 return clone;
97 }
98 } // namespace ark::es2panda::ir
99