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 "checker/ETSchecker.h"
18 #include "ir/expressions/identifier.h"
19 #include "ir/astNode.h"
20 #include "parser/ETSparser.h"
21
22 #include <gtest/gtest.h>
23
24 using ark::es2panda::compiler::ast_verifier::ASTVerifier;
25 using ark::es2panda::compiler::ast_verifier::InvariantNameSet;
26 using ark::es2panda::ir::AstNode;
27
28 namespace {
TEST_F(ASTVerifierTest,CatchClause)29 TEST_F(ASTVerifierTest, CatchClause)
30 {
31 ASTVerifier verifier {Allocator()};
32
33 char const *text = R"(
34 let a = 10;
35 try {
36 a / 0;
37 } catch (e) {
38 if (e instanceof Error) {
39 }
40 }
41 )";
42
43 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
44 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
45 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
46
47 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
48
49 InvariantNameSet checks;
50 checks.insert("VariableHasEnclosingScopeForAll");
51 const auto &messages = verifier.Verify(ast, checks);
52 ASSERT_EQ(messages.size(), 0);
53
54 impl_->DestroyContext(ctx);
55 }
56
TEST_F(ASTVerifierTest,LambdasHaveCorrectScope)57 TEST_F(ASTVerifierTest, LambdasHaveCorrectScope)
58 {
59 ASTVerifier verifier {Allocator()};
60
61 char const *text = R"(
62 type BenchmarkFunc = () => void;
63
64 function main() {
65 const arr: number[] = [1, 2, 3, 4];
66 const ITERATE_FUNC: BenchmarkFunc = () => {
67 const length = arr.length;
68 };
69 }
70 )";
71
72 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
73 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
74 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
75
76 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
77
78 InvariantNameSet checks;
79 checks.insert("VariableHasEnclosingScopeForAll");
80 const auto &messages = verifier.Verify(ast, checks);
81 ASSERT_EQ(messages.size(), 0);
82
83 impl_->DestroyContext(ctx);
84 }
85
TEST_F(ASTVerifierTest,ParametersInArrowFunctionExpression)86 TEST_F(ASTVerifierTest, ParametersInArrowFunctionExpression)
87 {
88 ASTVerifier verifier {Allocator()};
89
90 char const *text = R"(
91 let b = 1;
92 let f = (p: double) => b + p;
93 function main () {
94 assert f(42) == 43
95 }
96 )";
97
98 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
99 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
100 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
101
102 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
103
104 InvariantNameSet checks;
105 checks.insert("VariableHasEnclosingScopeForAll");
106 const auto &messages = verifier.Verify(ast, checks);
107 ASSERT_EQ(messages.size(), 0);
108
109 impl_->DestroyContext(ctx);
110 }
111 } // namespace
112