• 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_test.h"
17 #include "checker/ETSchecker.h"
18 
19 namespace {
20 
21 using ark::es2panda::compiler::ast_verifier::ModifierAccessValid;
22 
TEST_F(ASTVerifierTest,PrivateAccessTestNegative1)23 TEST_F(ASTVerifierTest, PrivateAccessTestNegative1)
24 {
25     char const *text = R"(
26         class Base {
27             public a: int = 1;
28         }
29         class Derived extends Base {
30             public b: int = this.a;
31         }
32     )";
33     CONTEXT(ES2PANDA_STATE_CHECKED, text)
34     {
35         GetAst()
36             ->AsETSModule()
37             ->Statements()[1]
38             ->AsClassDeclaration()
39             ->Definition()
40             ->AsClassDefinition()
41             ->Body()[0]
42             ->AsClassProperty()
43             ->AddModifier(ark::es2panda::ir::ModifierFlags::PRIVATE);
44 
45         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
46     }
47 }
48 
TEST_F(ASTVerifierTest,PrivateAccessTestNegative2)49 TEST_F(ASTVerifierTest, PrivateAccessTestNegative2)
50 {
51     char const *text = R"(
52         class Base {
53             public a: int = 1;
54         }
55         function main(): void {
56             let base: Base = new Base();
57             let a = base.a;
58         }
59     )";
60     CONTEXT(ES2PANDA_STATE_CHECKED, text)
61     {
62         GetAst()
63             ->AsETSModule()
64             ->Statements()[1]
65             ->AsClassDeclaration()
66             ->Definition()
67             ->AsClassDefinition()
68             ->Body()[0]
69             ->AsClassProperty()
70             ->AddModifier(ark::es2panda::ir::ModifierFlags::PRIVATE);
71 
72         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
73     }
74 }
75 
TEST_F(ASTVerifierTest,PrivateAccessTestNegative3)76 TEST_F(ASTVerifierTest, PrivateAccessTestNegative3)
77 {
78     char const *text = R"(
79         class Base {
80             public a: int = 1;
81         }
82         class Derived extends Base {}
83         function main(): void {
84             let derived: Derived = new Derived();
85             let a = derived.a;
86         }
87     )";
88     CONTEXT(ES2PANDA_STATE_CHECKED, text)
89     {
90         GetAst()
91             ->AsETSModule()
92             ->Statements()[1]
93             ->AsClassDeclaration()
94             ->Definition()
95             ->AsClassDefinition()
96             ->Body()[0]
97             ->AsClassProperty()
98             ->AddModifier(ark::es2panda::ir::ModifierFlags::PRIVATE);
99 
100         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
101     }
102 }
103 
TEST_F(ASTVerifierTest,PrivateAccessTestNegative4)104 TEST_F(ASTVerifierTest, PrivateAccessTestNegative4)
105 {
106     char const *text = R"(
107         class Base {
108             public a: int = 1;
109         }
110         class Derived extends Base {}
111         function main(): void {
112             let derived: Base = new Derived();
113             let a = derived.a;
114         }
115     )";
116     CONTEXT(ES2PANDA_STATE_CHECKED, text)
117     {
118         GetAst()
119             ->AsETSModule()
120             ->Statements()[1]
121             ->AsClassDeclaration()
122             ->Definition()
123             ->AsClassDefinition()
124             ->Body()[0]
125             ->AsClassProperty()
126             ->AddModifier(ark::es2panda::ir::ModifierFlags::PRIVATE);
127 
128         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
129     }
130 }
131 
TEST_F(ASTVerifierTest,PrivateAccessTestNegative5)132 TEST_F(ASTVerifierTest, PrivateAccessTestNegative5)
133 {
134     char const *text = R"(
135         class Base {
136             public a: int = 1;
137             public privateMethod() {
138                 this.a = 2;
139             }
140         }
141         function main(): void {
142             let base: Base = new Base();
143             base.privateMethod();
144         }
145     )";
146     CONTEXT(ES2PANDA_STATE_CHECKED, text)
147     {
148         GetAst()
149             ->AsETSModule()
150             ->Statements()[0]
151             ->AsClassDeclaration()
152             ->Definition()
153             ->AsClassDefinition()
154             ->Body()[1]
155             ->AsClassElement()
156             ->Value()
157             ->AsFunctionExpression()
158             ->Function()
159             ->AsScriptFunction()
160             ->Body()
161             ->AsBlockStatement()
162             ->Statements()[1]
163             ->AsExpressionStatement()
164             ->GetExpression()
165             ->AsCallExpression()
166             ->Signature()
167             ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PRIVATE);
168 
169         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
170     }
171 }
172 
TEST_F(ASTVerifierTest,PrivateAccessTestNegative6)173 TEST_F(ASTVerifierTest, PrivateAccessTestNegative6)
174 {
175     char const *text = R"(
176         class Base {
177             public a: int = 1;
178             public privateMethod() {
179                 this.a = 2;
180             }
181         }
182         class Derived extends Base {}
183         function main(): void {
184             let derived: Derived = new Derived();
185             derived.privateMethod();
186         }
187     )";
188     CONTEXT(ES2PANDA_STATE_CHECKED, text)
189     {
190         GetAst()
191             ->AsETSModule()
192             ->Statements()[0]
193             ->AsClassDeclaration()
194             ->Definition()
195             ->AsClassDefinition()
196             ->Body()[1]
197             ->AsClassElement()
198             ->Value()
199             ->AsFunctionExpression()
200             ->Function()
201             ->AsScriptFunction()
202             ->Body()
203             ->AsBlockStatement()
204             ->Statements()[1]
205             ->AsExpressionStatement()
206             ->GetExpression()
207             ->AsCallExpression()
208             ->Signature()
209             ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PRIVATE);
210 
211         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
212     }
213 }
214 
TEST_F(ASTVerifierTest,PrivateAccessTestNegative7)215 TEST_F(ASTVerifierTest, PrivateAccessTestNegative7)
216 {
217     char const *text = R"(
218         class Base {
219             public a: int = 1;
220             public privateMethod() {
221                 this.a = 2;
222             }
223         }
224         class Derived extends Base {}
225         function main(): void {
226             let derived: Base = new Derived();
227             derived.privateMethod();
228         }
229     )";
230     CONTEXT(ES2PANDA_STATE_CHECKED, text)
231     {
232         GetAst()
233             ->AsETSModule()
234             ->Statements()[0]
235             ->AsClassDeclaration()
236             ->Definition()
237             ->AsClassDefinition()
238             ->Body()[1]
239             ->AsClassElement()
240             ->Value()
241             ->AsFunctionExpression()
242             ->Function()
243             ->AsScriptFunction()
244             ->Body()
245             ->AsBlockStatement()
246             ->Statements()[1]
247             ->AsExpressionStatement()
248             ->GetExpression()
249             ->AsCallExpression()
250             ->Signature()
251             ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PRIVATE);
252 
253         EXPECT_TRUE(Verify<ModifierAccessValid>(ExpectVerifierMessage {"PROPERTY_NOT_VISIBLE_HERE"}));
254     }
255 }
256 
257 }  // namespace
258