• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "ast_verifier/helpers.h"
17 #include "nodeHasType.h"
18 #include "ir/base/classDefinition.h"
19 #include "ir/base/methodDefinition.h"
20 #include "ir/base/scriptFunction.h"
21 #include "ir/base/spreadElement.h"
22 #include "ir/expressions/functionExpression.h"
23 #include "ir/expressions/identifier.h"
24 #include "ir/ets/etsParameterExpression.h"
25 #include "ir/statements/annotationDeclaration.h"
26 #include "ir/ts/tsEnumDeclaration.h"
27 #include "ir/ts/tsInterfaceBody.h"
28 #include "ir/ts/tsInterfaceDeclaration.h"
29 
30 namespace ark::es2panda::compiler::ast_verifier {
31 
32 // NOTE(dkofanov): These exceptions are inadequate and are not actual exceptions.
33 class NodeHasType::ExceptionsMatcher {
34 public:
ExceptionsMatcher(const ir::AstNode * ast)35     explicit ExceptionsMatcher(const ir::AstNode *ast) : nulltypeNode_(ast) {}
36 
ShouldSkipNode() const37     auto ShouldSkipNode() const
38     {
39         return nulltypeNode_->IsIdentifier() || MatchFunctionExpression() || nulltypeNode_->IsTSClassImplements() ||
40                nulltypeNode_->IsSpreadElement() || nulltypeNode_->IsTSThisType() || nulltypeNode_->IsETSNullType() ||
41                nulltypeNode_->IsStringLiteral() || AnyChildStringLiteral();
42     }
43 
ShouldSkipSubtree() const44     auto ShouldSkipSubtree() const
45     {
46         return nulltypeNode_->IsAnnotationUsage() || nulltypeNode_->IsVariableDeclarator() || MatchTypeParameter() ||
47                nulltypeNode_->IsTSEnumDeclaration() || nulltypeNode_->IsTSInterfaceDeclaration() ||
48                nulltypeNode_->IsTSQualifiedName() || nulltypeNode_->IsETSParameterExpression() ||
49                nulltypeNode_->IsETSTypeReference() || MatchImportExport() || nulltypeNode_->IsTryStatement() ||
50                nulltypeNode_->IsAssignmentExpression();
51     }
52 
53 private:
MatchFunctionExpression() const54     bool MatchFunctionExpression() const
55     {
56         if (!nulltypeNode_->IsFunctionExpression()) {
57             return false;
58         }
59         auto fe = nulltypeNode_->AsFunctionExpression();
60         return (fe->Parent() != nullptr) && (fe->Parent()->IsMethodDefinition() || fe->Parent()->IsClassStaticBlock());
61     }
62 
MatchTypeParameter() const63     bool MatchTypeParameter() const
64     {
65         return nulltypeNode_->IsTSTypeParameterInstantiation() || nulltypeNode_->IsTSTypeParameterDeclaration();
66     }
67 
MatchImportExport() const68     bool MatchImportExport() const
69     {
70         return nulltypeNode_->IsETSReExportDeclaration() || nulltypeNode_->IsETSImportDeclaration();
71     }
72 
AnyChildStringLiteral() const73     bool AnyChildStringLiteral() const
74     {
75         return nulltypeNode_->IsAnyChild([](auto *child) { return child->IsStringLiteral(); });
76     }
77 
78 private:
79     const ir::AstNode *nulltypeNode_ {};
80 };
81 
operator ()(const ir::AstNode * ast)82 CheckResult NodeHasType::operator()(const ir::AstNode *ast)
83 {
84     type_ = nullptr;
85 
86     if (ExceptionsMatcher {ast}.ShouldSkipSubtree()) {
87         return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
88     }
89 
90     if (!ast->IsTyped()) {
91         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
92     }
93 
94     type_ = ast->AsTyped()->TsType();
95     if (type_ != nullptr || ExceptionsMatcher {ast}.ShouldSkipNode()) {
96         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
97     }
98 
99     AddCheckMessage("NULL_TS_TYPE", *ast);
100     return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
101 }
102 
103 }  // namespace ark::es2panda::compiler::ast_verifier
104