1 /*
2 * Copyright (c) 2024-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 "getterSetterValidation.h"
17 #include "ir/expression.h"
18 #include "ir/expressions/functionExpression.h"
19 #include "ir/typeNode.h"
20 #include "ir/base/methodDefinition.h"
21 #include "ir/base/scriptFunction.h"
22
23 namespace ark::es2panda::compiler::ast_verifier {
24
operator ()(const ir::AstNode * ast)25 [[nodiscard]] CheckResult GetterSetterValidation::operator()(const ir::AstNode *ast)
26 {
27 if (!ast->IsMethodDefinition()) {
28 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
29 }
30
31 bool errorFound = false;
32 auto const validateMethod = [&errorFound, this](ir::MethodDefinition const *const method) {
33 auto const kind = method->Kind();
34 if (kind == ir::MethodDefinitionKind::GET) {
35 errorFound |= !ValidateGetter(method);
36 } else if (kind == ir::MethodDefinitionKind::SET) {
37 errorFound |= !ValidateSetter(method);
38 };
39 };
40
41 auto const *const method = ast->AsMethodDefinition();
42 validateMethod(method);
43 for (auto const *const overload : method->Overloads()) {
44 validateMethod(overload);
45 }
46
47 if (errorFound) {
48 return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
49 }
50
51 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
52 }
53
ValidateGetter(ir::MethodDefinition const * const method)54 bool GetterSetterValidation::ValidateGetter(ir::MethodDefinition const *const method)
55 {
56 if (!method->Value()->IsFunctionExpression()) {
57 return true;
58 }
59
60 bool result = true;
61 auto const report = [this, &result, method](const std::string &msg) {
62 AddCheckMessage(msg, *method);
63 result = false;
64 };
65
66 auto const *const function = method->Value()->AsFunctionExpression()->Function();
67
68 // Check getter flag
69 if (!function->IsGetter()) {
70 report("GETTER METHOD DOES NOT HAVE GETTER FLAG");
71 }
72
73 // Check return type annotation if it exists
74 if (function->ReturnTypeAnnotation() != nullptr) {
75 auto const *const type = function->ReturnTypeAnnotation()->TsType();
76 if (type != nullptr && type->IsETSVoidType()) {
77 report("GETTER METHOD HAS VOID RETURN TYPE IN RETURN TYPE ANNOTATION");
78 }
79 }
80
81 // For non-abstract, non-ambient and non-native getters return statement should always exist
82 if (!function->HasReturnStatement() && !function->HasThrowStatement() && !function->IsAbstract() &&
83 !function->IsDeclare() && !function->IsNative()) {
84 report("MISSING RETURN STATEMENT IN GETTER METHOD");
85 }
86
87 // Check return statements
88 auto const &returns = function->ReturnStatements();
89 if (function->ReturnTypeAnnotation() == nullptr) {
90 if (returns.empty()) {
91 report("MISSING RETURN TYPE ANNOTATION AND RETURN STATEMENT IN GETTER METHOD");
92 }
93 }
94
95 // Check that all return statements are not void
96 for (auto const *const stmt : returns) {
97 if (stmt->ReturnType()->IsETSVoidType()) {
98 // All getters should have non-void return type
99 report("GETTER METHOD HAS VOID RETURN TYPE");
100 }
101 }
102
103 // Check number of arguments
104 auto const ¶ms = function->Params();
105 if (!params.empty()) {
106 report("GETTER METHOD HAS INCORRECT NUMBER OF ARGUMENTS");
107 }
108
109 return result;
110 }
111
ValidateSetter(ir::MethodDefinition const * const method)112 bool GetterSetterValidation::ValidateSetter(ir::MethodDefinition const *const method)
113 {
114 if (!method->Value()->IsFunctionExpression()) {
115 return true;
116 }
117
118 bool result = true;
119 auto const report = [this, &result, method](const std::string &msg) {
120 AddCheckMessage(msg, *method);
121 result = false;
122 };
123
124 auto const *const function = method->Value()->AsFunctionExpression()->Function();
125
126 // Check setter flag
127 if (!function->IsSetter()) {
128 report("SETTER METHOD DOES NOT HAVE SETTER FLAG");
129 }
130
131 // Check return type annotation
132 if (function->ReturnTypeAnnotation() != nullptr) {
133 report("SETTER METHOD MUST NOT HAVE RETURN TYPE");
134 }
135
136 // Check number of arguments
137 auto const ¶ms = function->Params();
138 if (params.size() != 1) {
139 report("SETTER METHOD HAS INCORRECT NUMBER OF ARGUMENTS");
140 }
141
142 return result;
143 }
144
145 } // namespace ark::es2panda::compiler::ast_verifier
146