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 */
15import { fooOh, barOh } from './oh_modules/ohos_lib'
16type ESObject = any
17
18class A<T> {}
19
20let g1: ESObject
21let g2: ESObject[]
22let g3: A<ESObject>
23
24class B {
25    f1: ESObject
26    f2: ESObject[]
27    f3: A<ESObject>
28
29    constructor(p1: ESObject, p2: ESObject[], p3: A<ESObject>) {
30        this.f1 = p1
31        this.f2 = p2
32        this.f3 = p3
33    }
34
35    foo1(p1: ESObject, p2: ESObject[], p3: A<ESObject>): ESObject {
36        return p1
37    }
38
39    foo2(p1: ESObject, p2: ESObject[], p3: A<ESObject>): ESObject[] {
40        return p2
41    }
42
43    foo3(p1: ESObject, p2: ESObject[], p3: A<ESObject>): A<ESObject> {
44        return p3
45    }
46}
47
48function bar1(p1: ESObject, p2: ESObject[], p3: A<ESObject>): ESObject {
49    return p1
50}
51
52function bar2(p1: ESObject, p2: ESObject[], p3: A<ESObject>): ESObject[] {
53    return p2
54}
55
56function bar3(p1: ESObject, p2: ESObject[], p3: A<ESObject>): A<ESObject> {
57    return p3
58}
59
60function ff(): {x: number} {
61    return {x: 10}
62}
63
64function baz(p1: ESObject, p2: ESObject[], p3: A<ESObject>): void {
65    const c1: ESObject = p1;
66    const c2: ESObject[] = p2
67    const c3: A<ESObject> = p3
68
69    let v1: ESObject = p1
70    let v2: ESObject[] = p2
71    let v3: A<ESObject> = p3
72
73    v1 = c1
74    v2 = c2
75    v3 = c3
76
77    v1.x = 10
78    v1.foo()
79    v1[10] = 20
80    v1(20)
81
82    v1 = {}
83    v1 = "abc"
84    v1 = ff()
85    v1 = [1, 2]
86    v1 = [p1, c1]
87    v1 = [p1, c1, "abc"]
88    v1 = new A<string>()
89
90    let v11: ESObject = {}
91    let v12: ESObject = "abc"
92    let v13: ESObject = ff()
93    let v14: ESObject = [1, 2]
94    let v15: ESObject = [p1, c1]
95    let v16: ESObject = [p1, c1, "abc"]
96    let v17: ESObject = new A<string>()
97
98    let n1: number = v1
99    n1 = v1
100    let n2: number = p1 as number
101}
102
103export let obj = new ESObject();
104
105type t1 = ESObject
106type t2 = ESObject[]
107
108export type t3 = ESObject
109export type t4 = ESObject[]
110
111export type t5 = t3
112export type t6 = t4[]
113
114export function foo1(): any {
115    let a: ESObject = "STRING";
116    return a
117}
118
119export function foo2(a: ESObject): ESObject {
120    return a;
121}
122
123export function foo3(a: t3): t3 {
124    return a;
125}
126
127foo2(5)
128foo3(5)
129foo2("asd")
130foo3("asd")
131foo2(null)
132foo3(null)
133foo2(undefined)
134foo3(undefined)
135
136export function foo4(a: ESObject[]): ESObject {
137    return a;
138}
139
140export function foo5(a: t3[]): t3 {
141    return a;
142}
143
144foo4([2, 3])
145foo5([2, 3])
146foo4(["str1", "str2"])
147foo5(["str1", "str2"])
148let n: ESObject
149n = null
150
151foo4(n)
152foo5(n)
153
154export function foo6<T extends ESObject>(a: ESObject[]): ESObject {
155    return a;
156}
157
158export function foo7<T extends t3>(a: t3[]): t3 {
159    return a;
160}
161
162export function foo8<T extends ESObject[]>(a: ESObject[]): ESObject {
163    return a;
164}
165
166export function foo9<T extends t3[]>(a: t3[]): t3 {
167    return a;
168}
169
170export class Cls<T extends ESObject> {}
171
172interface CL extends ESObject {}
173
174export interface CLS extends ESObject {}
175
176foo2({ k: 'k', h: {t: 1}}) // we can assign anything to the esobject, even untyped literal
177let q1: ESObject = 1; // CTE - ``ESObject`` typed variable can only be local
178let q2: ESObject = fooOh(); // CTE - ``ESObject`` typed variable can only be local
179let q3: ESObject = q2; // CTE - ``ESObject`` typed variable can only be local
180function f() {
181    let e1 = fooOh(); // CTE - type of e1 is `any`
182    let e2: ESObject = 1; // CTE - can't initialize ESObject with not dynamic values
183    let e3: ESObject = {}; // CTE - can't initialize ESObject with not dynamic values
184    let e4: ESObject = []; // CTE - can't initialize ESObject with not dynamic values
185    let e5: ESObject = ""; // CTE - can't initialize ESObject with not dynamic values
186    let e6: ESObject = fooOh(); // OK - explicitly annotaded as ESObject
187    let e7: ESObject = e6; // OK - initialize ESObject with ESObject
188    e6['prop'] // CTE - can't access dynamic properties of ESObject
189    e6[1] // CTE - can't access dynamic properties of ESObject
190    e6.prop // CTE - can't access dynamic properties of ESObject
191    barOh(e6) // OK - ESObject is passed to interop call
192    e6 = e7 // OK - ESObject is assigned to ESObject
193}
194