• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 "ir/astNode.h"
18 
19 #include <gtest/gtest.h>
20 
21 using ark::es2panda::compiler::ast_verifier::ASTVerifier;
22 using ark::es2panda::compiler::ast_verifier::InvariantNameSet;
23 using ark::es2panda::ir::AstNode;
24 
25 namespace {
26 
TEST_F(ASTVerifierTest,FunctionScope)27 TEST_F(ASTVerifierTest, FunctionScope)
28 {
29     ASTVerifier verifier {Allocator()};
30 
31     char const *text = R"(
32         function test(a: int) {
33             console.log(a)
34         }
35     )";
36 
37     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
38     impl_->ProceedToState(ctx, ES2PANDA_STATE_LOWERED);
39     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_LOWERED);
40 
41     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
42 
43     InvariantNameSet checks;
44     checks.insert("CheckScopeDeclarationForAll");
45     const auto &messages = verifier.Verify(ast, checks);
46 
47     // Expecting no warnings
48     ASSERT_EQ(messages.size(), 0);
49 
50     impl_->DestroyContext(ctx);
51 }
52 
TEST_F(ASTVerifierTest,ForUpdateLoopScope)53 TEST_F(ASTVerifierTest, ForUpdateLoopScope)
54 {
55     ASTVerifier verifier {Allocator()};
56 
57     char const *text = R"(
58         function main() {
59             for (let i = 0; i < 10; i++) {}
60         }
61     )";
62 
63     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
64     impl_->ProceedToState(ctx, ES2PANDA_STATE_LOWERED);
65     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_LOWERED);
66 
67     auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
68 
69     InvariantNameSet checks;
70     checks.insert("CheckScopeDeclarationForAll");
71     const auto &messages = verifier.Verify(ast, checks);
72 
73     // Expecting no warnings
74     ASSERT_EQ(messages.size(), 0);
75 
76     impl_->DestroyContext(ctx);
77 }
78 
79 }  // namespace
80