• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ast_verifier/helpers.h"
17 #include "variableHasScope.h"
18 #include "ir/base/scriptFunction.h"
19 #include "ir/ts/tsEnumDeclaration.h"
20 #include "ir/typeNode.h"
21 
22 namespace ark::es2panda::compiler::ast_verifier {
23 
operator ()(const ir::AstNode * ast)24 CheckResult VariableHasScope::operator()(const ir::AstNode *ast)
25 {
26     if (!ast->IsIdentifier()) {
27         // Check invariant of Identifier only
28         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
29     }
30 
31     // we will check invariant for only local variables of identifiers
32     if (const auto var = TryGetLocalScopeVariable(ast->AsIdentifier()); var != nullptr) {
33         const auto scope = var->GetScope();
34         if (scope == nullptr) {
35             AddCheckMessage("NULL_SCOPE_LOCAL_VAR", *ast);
36             return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
37         }
38 
39         auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE);
40         if (!ScopeEncloseVariable(var)) {
41             result = {CheckDecision::INCORRECT, CheckAction::CONTINUE};
42         }
43 
44         return result;
45     }
46 
47     return {CheckDecision::CORRECT, CheckAction::CONTINUE};
48 }
49 
ScopeEncloseVariable(const varbinder::LocalVariable * var)50 bool VariableHasScope::ScopeEncloseVariable(const varbinder::LocalVariable *var)
51 {
52     ES2PANDA_ASSERT(var);
53 
54     const auto scope = var->GetScope();
55     if (scope == nullptr || var->Declaration() == nullptr) {
56         return true;
57     }
58 
59     const auto node = var->Declaration()->Node();
60     if (node == nullptr) {
61         return true;
62     }
63 
64     bool isOk = true;
65     if (scope->Bindings().count(var->Name()) == 0) {
66         AddCheckMessage("SCOPE_DO_NOT_ENCLOSE_LOCAL_VAR", *node);
67         isOk = false;
68     }
69 
70     const auto scopeNode = scope->Node();
71     const auto varNode = node;
72     bool skip = CheckAstExceptions(varNode);
73 
74     if (!IsContainedIn(varNode, scopeNode) || scopeNode == nullptr) {
75         if (!skip) {
76             AddCheckMessage("SCOPE_NODE_DONT_DOMINATE_VAR_NODE", *node);
77             isOk = false;
78         }
79     }
80 
81     const auto &decls = scope->Decls();
82     const auto declDominate = std::count(decls.begin(), decls.end(), var->Declaration());
83     if (declDominate == 0) {
84         if (!skip) {
85             AddCheckMessage("SCOPE_DECL_DONT_DOMINATE_VAR_DECL", *node);
86             isOk = false;
87         }
88     }
89 
90     return isOk;
91 }
92 
CheckAstExceptions(const ir::AstNode * ast)93 bool VariableHasScope::CheckAstExceptions(const ir::AstNode *ast)
94 {
95     // Labels are attached to loop scopes,
96     // however label identifier is outside of loop.
97     // Example:
98     //
99     // ```
100     // loop: for (let i = 0; i < 10; i++) {
101     // }
102     // ```
103     return ast->IsLabelledStatement();
104 }
105 
106 }  // namespace ark::es2panda::compiler::ast_verifier
107