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 {
TEST_F(ASTVerifierTest,ParametersInAsyncFunction)26 TEST_F(ASTVerifierTest, ParametersInAsyncFunction)
27 {
28 ASTVerifier verifier {Allocator()};
29
30 char const *text = R"(
31 async function bar(flag: boolean): Promise<double> {
32 if (flag) {
33 return 5.0;
34 } else {
35 return 1.0;
36 }
37 }
38 )";
39
40 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
41 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
42 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
43
44 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
45
46 InvariantNameSet checks;
47 checks.insert("VariableHasScopeForAll");
48 const auto &messages = verifier.Verify(ast, checks);
49 ASSERT_EQ(messages.size(), 0);
50
51 impl_->DestroyContext(ctx);
52 }
53
TEST_F(ASTVerifierTest,TestUnions)54 TEST_F(ASTVerifierTest, TestUnions)
55 {
56 ASTVerifier verifier {Allocator()};
57
58 char const *text = R"(
59 function assert_ccexc(f: () => void) {
60 try {
61 f();
62 assert false : "exception expected";
63 } catch (e) {
64 assert(e instanceof ClassCastError);
65 }
66 }
67
68 class A { }
69
70 function test_unions() {
71 assert_ccexc(() => { let f = ((x: A | undefined) => x as A | null); f(undefined); });
72 }
73
74 function main() {
75 test_unions();
76 }
77 )";
78
79 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
80 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
81 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
82
83 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
84
85 InvariantNameSet checks;
86 checks.insert("VariableHasScopeForAll");
87 const auto &messages = verifier.Verify(ast, checks);
88 ASSERT_EQ(messages.size(), 0);
89
90 impl_->DestroyContext(ctx);
91 }
92
TEST_F(ASTVerifierTest,LambdasHaveCorrectScope)93 TEST_F(ASTVerifierTest, LambdasHaveCorrectScope)
94 {
95 ASTVerifier verifier {Allocator()};
96
97 char const *text = R"(
98 type BenchmarkFunc = () => void;
99
100 function main() {
101 const arr: number[] = [1, 2, 3, 4];
102 const ITERATE_FUNC: BenchmarkFunc = () => {
103 const length = arr.length;
104 };
105 }
106 )";
107
108 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
109 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
110 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
111
112 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
113
114 InvariantNameSet checks;
115 checks.insert("VariableHasScopeForAll");
116 const auto &messages = verifier.Verify(ast, checks);
117 ASSERT_EQ(messages.size(), 0);
118
119 impl_->DestroyContext(ctx);
120 }
121
TEST_F(ASTVerifierTest,AsyncLambda)122 TEST_F(ASTVerifierTest, AsyncLambda)
123 {
124 ASTVerifier verifier {Allocator()};
125
126 char const *text = R"(
127 let fs: ((p: int) => int)[]
128 function foo(i: int): ((p: int) => int) {
129 return fs[i]
130 }
131
132 function main() {
133 fs = [
134 (p: int): int => p + 1,
135 ]
136
137 let ps: Object = new Object()
138 ps = launch foo(0)
139
140 let cnt = 0
141 cnt += (await ps as Promise<(p: int) => int>)(0)
142 }
143 )";
144
145 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets");
146 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
147 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
148
149 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
150
151 InvariantNameSet checks;
152 checks.insert("VariableHasScopeForAll");
153 const auto &messages = verifier.Verify(ast, checks);
154 ASSERT_EQ(messages.size(), 0);
155
156 impl_->DestroyContext(ctx);
157 }
158 } // namespace
159