1/**
2 * Copyright (c) 2021 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 <typescript/checker.h>
19#include <ir/expressions/literals/numberLiteral.h>
20#include <ir/astDump.h>
21#include <ir/typeNode.h>
22#include <binder/scope.h>
23
24namespace panda::es2panda::ir {
25
26void TSPropertySignature::Iterate(const NodeTraverser &cb) const
27{
28    cb(key_);
29
30    if (typeAnnotation_) {
31        cb(typeAnnotation_);
32    }
33}
34
35void TSPropertySignature::Dump(ir::AstDumper *dumper) const
36{
37    dumper->Add({{"type", "TSPropertySignature"},
38                 {"computed", computed_},
39                 {"optional", optional_},
40                 {"readonly", readonly_},
41                 {"key", key_},
42                 {"typeAnnotation", AstDumper::Optional(typeAnnotation_)}});
43}
44
45void TSPropertySignature::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
46
47checker::Type *TSPropertySignature::Check(checker::Checker *checker) const
48{
49    if (typeAnnotation_) {
50        typeAnnotation_->Check(checker);
51    }
52
53    if (computed_) {
54        checker->CheckComputedPropertyName(key_);
55    }
56
57    if (typeAnnotation_) {
58        Variable()->SetTsType(typeAnnotation_->AsTypeNode()->GetType(checker));
59        return nullptr;
60    }
61
62    checker->ThrowTypeError("Property implicitly has an 'any' type.", Start());
63    return nullptr;
64}
65
66void TSPropertySignature::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
67{
68    key_ = std::get<ir::AstNode *>(cb(key_))->AsExpression();
69
70    if (typeAnnotation_) {
71        typeAnnotation_ = std::get<ir::AstNode *>(cb(typeAnnotation_))->AsExpression();
72    }
73}
74
75}  // namespace panda::es2panda::ir
76