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 "ir/astNode.h"
18
19 #include <gtest/gtest.h>
20
21 using ark::es2panda::compiler::ast_verifier::VariableHasScope;
22
23 namespace {
TEST_F(ASTVerifierTest,ParametersInAsyncFunction)24 TEST_F(ASTVerifierTest, ParametersInAsyncFunction)
25 {
26 char const *text = R"(
27 async function bar(flag: boolean): Promise<double> {
28 if (flag) {
29 return 5.0;
30 } else {
31 return 1.0;
32 }
33 }
34 )";
35
36 CONTEXT(ES2PANDA_STATE_CHECKED, text)
37 {
38 EXPECT_TRUE(Verify<VariableHasScope>());
39 }
40 }
41
TEST_F(ASTVerifierTest,TestUnions)42 TEST_F(ASTVerifierTest, TestUnions)
43 {
44 char const *text = R"(
45 function assert_ccexc(f: () => void) {
46 try {
47 f();
48 assertTrue(false, "exception expected");
49 } catch (e) {
50 assertTrue(e instanceof ClassCastError);
51 }
52 }
53
54 class A { }
55
56 function test_unions() {
57 assert_ccexc(() => { let f = ((x: A | undefined) => x as A | null); f(undefined); });
58 }
59
60 function main() {
61 test_unions();
62 }
63 )";
64
65 CONTEXT(ES2PANDA_STATE_CHECKED, text)
66 {
67 EXPECT_TRUE(Verify<VariableHasScope>());
68 }
69 }
70
TEST_F(ASTVerifierTest,LambdasHaveCorrectScope)71 TEST_F(ASTVerifierTest, LambdasHaveCorrectScope)
72 {
73 char const *text = R"(
74 type BenchmarkFunc = () => void;
75
76 function main() {
77 const arr: number[] = [1, 2, 3, 4];
78 const ITERATE_FUNC: BenchmarkFunc = () => {
79 const length = arr.length;
80 };
81 }
82 )";
83
84 CONTEXT(ES2PANDA_STATE_CHECKED, text)
85 {
86 EXPECT_TRUE(Verify<VariableHasScope>());
87 }
88 }
89
TEST_F(ASTVerifierTest,AsyncLambda1)90 TEST_F(ASTVerifierTest, AsyncLambda1)
91 {
92 char const *text = R"(
93 let fs: ((p: int) => int)[] = [];
94 async function foo(i: int): Promise<((p: int) => int)> {
95 return fs[i]
96 }
97
98 function main() {
99 fs = [
100 (p: int): int => p + 1,
101 ]
102
103 let ps: Object = new Object()
104 ps = foo(0)
105
106 let cnt = 0
107 cnt += (await (ps as Promise<(p: int) => int>))(0)
108 }
109 )";
110
111 CONTEXT(ES2PANDA_STATE_CHECKED, text)
112 {
113 EXPECT_TRUE(Verify<VariableHasScope>());
114 }
115 }
116
TEST_F(ASTVerifierTest,AsyncLambda2)117 TEST_F(ASTVerifierTest, AsyncLambda2)
118 {
119 char const *text = R"(
120 let global: int;
121 async function func(param: int): Promise<String | null> {
122 let local: int;
123 let async_lambda: () => Promise<Object | null> = async (): Promise<Object | null> => {
124 param;
125 local;
126 global;
127 let x = 0;
128 return null;
129 }
130
131 return null;
132 }
133 )";
134
135 CONTEXT(ES2PANDA_STATE_LOWERED, text)
136 {
137 EXPECT_TRUE(Verify<VariableHasScope>());
138 }
139 }
140
141 } // namespace
142