diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/Ambient_Class_Declarations/Ambient_Class_Declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/Ambient_Class_Declarations/Ambient_Class_Declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..40efacf774c1e109af6126be5a81b7e0fe9860e7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/Ambient_Class_Declarations/Ambient_Class_Declarations.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An ambient class declaration declares a class type and a constructor function in the containing declaration space. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +declare class Student { + constructor(sname?: string, sage?: number); + public sname: string; + public sage: number; + cl():number; +} +let s1 :Student ={ + sname:"static", + sage:12, + cl(){ + return 1; + } +} +Assert.equal(s1.sname,"static"); +Assert.equal(s1.sage,"12"); +Assert.equal(s1.cl(),1); +Assert.isFunction(s1.cl); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..60d3b91412fb396f14c249b95f00ef63c5b6ae5a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an ambient function declaration introduces a function in the containing declaration space. + ambient functions may be overloaded by specifying multiple ambient function declarations with the same name, but it is an error to declare multiple overloads that are considered identical or differ only in their return types. + ambient function declarations cannot specify a function bodies and do not permit default parameter values. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +declare type DF1 = () => void; +declare type DF2 = (a: string, b: string) => string; +declare type DF3 = (a: number, b?: number) => string; +const dFun1: DF1 = () => { Assert.isString("dFun1"); } +const dFun2: DF2 = (a: string, b: string) => { Assert.isString("dFun2"); return a + b; } +const dFun3: DF3 = (a: number, b?: number) => { + let c: any; + if (b != undefined) { + c = a + b; + } else { + c = a; + } + c = c.toString(); + Assert.isString("dFun3"); + return "$" + c; +} +dFun1(); +Assert.equal(dFun2("A", "B"), 'AB'); +Assert.equal(dFun3(10, 20), "$30"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..ccc60c491ccc96f6bb3d3ee10f257072bd06497a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an ambient namespace declaration declares a namespace. + except for ImportAliasDeclarations, AmbientNamespaceElements always declare exported entities regardless of whether they include the optional export modifier. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +declare namespace AmbientNamespace { + var an1: string; + export var an2: string; + interface ColorInterface { + Red: number; + Green: number; + Blue: number; + } +} +var colorI: AmbientNamespace.ColorInterface = { Red: 0, Green: 1, Blue: 2 }; +Assert.equal(JSON.stringify(colorI), '{"Red":0,"Green":1,"Blue":2}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_variable_declarations/ambient_variable_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_variable_declarations/ambient_variable_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..d76695a41e304d0780a6c474ac3017a47090304a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_declarations/ambient_variable_declarations/ambient_variable_declarations.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An ambient variable declaration introduces a variable in the containing declaration space. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +declare type variable1 = number; +declare type variable2 = string; +declare type variable3 = (vab1:number,vab2:string) => string;; + +var vab1:variable1 = 3; +let vab2:variable2 = "www"; +const vab3:variable3 = (vab1,vab2) =>{return vab1+vab2;} + +Assert.equal(typeof vab1,"number"); +Assert.equal(typeof vab2,"string"); +Assert.equal(vab3(vab1,vab2),"3www"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6052602a32ee670e749280e5fd99a4f1b729b700 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + ambient modules are "open-ended" and ambient module declarations with the same string literal name contribute to a single module. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../../suite/assert.js" + +declare module AMD1 { + export var a1: any; + export interface AMD1IF { + a1_1: any; + a1_2: number; + } +} +var am2: AMD1.AMD1IF = { a1_1: "am2", a1_2: 123 }; +Assert.equal(JSON.stringify(am2), '{"a1_1":"am2","a1_2":123}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..22d8b497553ab5df9f8203bf40f778d387d44cd0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if an ambient module declaration includes an export assignment, it is an error for any of the declarations within the module to specify an export modifier. + if an ambient module declaration contains no export assignment, entities declared in the module are exported regardless of whether their declarations include the optional export modifier. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +export declare module m1 { + interface I1 { + a3_1: boolean; + a3_2: number; + } + var AMD2Var1: string; +} +declare module m2 { + export interface I2 { + a3_1: boolean; + a3_2: string; + } + export var AMD2Var1: string; +} +var am3_1: m1.I1 = { a3_1: false, a3_2: 0 }; +var am3_2: m2.I2 = { a3_1: true, a3_2: "T" }; +Assert.equal(JSON.stringify(am3_1), '{"a3_1":false,"a3_2":0}'); +Assert.equal(JSON.stringify(am3_2), '{"a3_1":true,"a3_2":"T"}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_body/class_body.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_body/class_body.ts new file mode 100644 index 0000000000000000000000000000000000000000..08b2bd7059eaf0105de9f63a664ecaf97b6c50f4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_body/class_body.ts @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The class body consists of zero or more constructor or member declarations. + Statements are not allowed in the body of a class—they must be placed in the constructor or in members. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Compute { + num1: number; + num2: number; + constructor(num1: number, num2: number) { + this.num1 = num1; + this.num2 = num2; + } + public hypot() { + return Math.sqrt(this.num1 * this.num1 + this.num2 * this.num2); + } + static initial = new Compute(0, 0); +} +let p: Compute = new Compute(10, 20); +Assert.equal(10, p.num1); +Assert.equal(20, p.num2); +Assert.equal(0, Compute.initial.num1); +Assert.equal(0, Compute.initial.num2); +let p2: Compute = new Compute(4, 3) +Assert.equal(5, p2.hypot()) + +class Circle { + radius: number = 1; +} +let c = new Circle(); +Assert.equal(c.radius, 1); +type TypeSummation = { + width?: number; + height?: number; +}; +class summation { + public width: number; + public height: number; + constructor(width: number, height: number); + constructor(ParamObje_: TypeSummation); + constructor(ParamObje_Obj_: any, height_ = 0) { + if (typeof ParamObje_Obj_ === "object") { + const { width, height } = ParamObje_Obj_; + this.width = width; + this.height = height; + } else { + this.width = ParamObje_Obj_; + this.height = height_; + } + } + sunArea(): number { + return this.width * this.height; + } +} +let sun = new summation(4, 5); +Assert.equal(sun.sunArea(), 20); +let obj: TypeSummation = { width: 10, height: 2 }; +let sun2 = new summation(obj); +Assert.equal(sun2.sunArea(), 20); + +class IndexNum { + [key: string]: number | string[]; + constructor(keyList: string[] = [], valueList: string[][] | number[] = []) { + let len = 0; + if ((keyList !== undefined) && (valueList !== undefined)) { + if (keyList.length <= valueList.length) { + len = keyList.length; + } else { + len = valueList.length; + } + for (let i: number = 0; i < len; i++) { + this[keyList[i]] = valueList[i]; + } + } + } +}; +let index = new IndexNum(['A', 'B', 'C'], [0, 1, 2]); +Assert.equal(index['A'], 0); +index['B'] = 5; +Assert.equal(index['B'], 5); +Assert.equal(index.C, 2); +index['D'] = ['D', 'E']; +index.E = 9; +Assert.equal(JSON.stringify(index.D), '["D","E"]'); +Assert.equal(index['E'], 9); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f381f91de86a44abb4a3b93343858dde8ec4f10 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_1.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A class declaration declares a class type and a constructor function: class BindingIdentifieropt TypeParametersopt ClassHeritage { ClassBody } + module: ESNext + isCurrent: true + ---*/ +import { Assert } from '../../../../suite/assert.js' + +class Rectangle { + constructor(public height: number, public width: number) { }; + get area() { + return this.calcArea() + } + calcArea() { + return this.height * this.width; + } +} +let square = new Rectangle(10, 10); +Assert.equal(100, square.area); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..eaa0ec471b9ea234ee4b211e9866a1baf6d8ff5f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_2.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A class declaration declares a class type and a constructor function: class BindingIdentifieropt TypeParametersopt ClassHeritage { ClassBody } + module: ESNext + isCurrent: true + ---*/ +import { Assert } from '../../../../suite/assert.js' + +class MyClass { + constructor(public x: number, public y: number) { + this.x = x; + this.y = y; + } + static initial = new MyClass(0, 0); + static distance(a: MyClass, b: MyClass) { + let dx = a.x - b.x; + let dy = a.y - b.y; + return Math.hypot(dx, dy); + } +} +Assert.equal(0, MyClass.initial.x); +Assert.equal(0, MyClass.initial.y); +let p1 = new MyClass(0, 4); +let p2 = new MyClass(3, 0); +Assert.equal(0, p1.x); +Assert.equal(4, p1.y); +Assert.equal(3, p2.x); +Assert.equal(0, p2.y); +Assert.equal(5, MyClass.distance(p1, p2)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..ea990b064c3f3bce0cf17fc7e17aeea62de0c115 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_declarations_3.ts @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A class with type parameters is called a generic class. + module: ESNext + isCurrent: true + ---*/ +import { Assert } from '../../../../suite/assert.js' + +class MyClass { + field: T; + constructor(field: T) { + this.field = field; + } + public getFieldName(): T { + return this.field; + } +} +class Compute { + constructor(public num1: number, public num2: number) { } + public hypot() { + return Math.sqrt(this.num1 * this.num1 + this.num2 * this.num2); + } +} +let mc1: MyClass = new MyClass("a"); +Assert.equal("a", mc1.field); +Assert.equal("a",mc1.getFieldName()); +let mc2: MyClass = new MyClass(1); +Assert.equal(1, mc2.field); +Assert.equal(1,mc2.getFieldName()); +let mc3: MyClass = new MyClass(false); +Assert.equal(false, mc3.field); +Assert.equal(false,mc3.getFieldName()); +let p: Compute = new Compute(8, 6); +let mc4: MyClass = new MyClass(p); +Assert.equal(8, mc4.field.num1); +Assert.equal(6, mc4.field.num2); +Assert.equal(10,mc4.field.hypot()); + +let obj: object = { + x: 1, + y: 2 +} +let mc5: MyClass = new MyClass(obj); +Assert.equal(obj, mc5.field); +let list: Array = [1, 2, 3]; +let mc6: MyClass> = new MyClass>(list); +Assert.equal(list, mc6.field); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..eb1837f98cdfef86a118980140ce7c4545dccafe --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_1.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A class that includes an extends clause is called a derived class, and the class specified in the extends clause is called the base class of the derived class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Compute { + constructor(public num1: number, public num2: number) { } + public hypot() { + return Math.sqrt(this.num1 * this.num1 + this.num2 * this.num2); + } + static initial = new Compute(0, 0); +} +class ChildP extends Compute { + constructor(public x: number, public y: number) { + super(x, y); + } + public move() { + this.x += 1; + this.y += 1; + } +} +let pChild: ChildP = new ChildP(10, 20); +Assert.equal(10, pChild.x); +Assert.equal(20, pChild.y); +pChild.move(); +Assert.equal(11, pChild.x); +Assert.equal(21, pChild.y); +Assert.equal(0, ChildP.initial.num1); + +let count:ChildP = new ChildP(4,3); +Assert.equal(4, count.x); +Assert.equal(3, count.y); +Assert.equal(5,count.hypot()); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb1f816f5e858a27f8dfd8852c6647b3c689979f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_2.ts @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the implements clause specifies a set of interfaces for which to validate the class provides an implementation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Compute { + constructor(public num1: number, public num2: number) { } + public hypot() { + return Math.sqrt(this.num1 * this.num1 + this.num2 * this.num2); + } + static initial = new Compute(0, 0); +} +let p: Compute = new Compute(4, 3); +Assert.equal(4, p.num1); +Assert.equal(3, p.num2); +Assert.equal(5,p.hypot()) + +interface InterP1 { + x: number; + y: number; +} +class P2 implements InterP1 { + x = 1; + y = 1; + setarea(x: number, y: number) { + return x * y; + } +} +let Ip = new P2(); +Assert.equal(1, Ip.x); +Assert.equal(1, Ip.y); +Assert.equal(2,Ip.setarea(1,2)) + +interface InterP2 { + Area(x: number, y: number): number; +} +class Rectangle implements InterP2 { + Area(x: number, y: number): number { + return x * y; + } +} +let rec = new Rectangle(); +Assert.equal(200, rec.Area(10, 20)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..57caf3cfdb5db45bb6349ea3a3dba176d33a06cc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_3.ts @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The this-type (section 3.6.3) of the declared class must be assignable (section 3.11.4) to the base type reference + and each of the type references listed in the implements clause. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Addx { + constructor(public x: number) { } + public add(): this { + this.x++; + return this; + } +} +class ChildAddx extends Addx { + constructor(public x: number) { + super(x); + } + public move(): this { + this.x++; + return this; + } +} +let childadd: ChildAddx = new ChildAddx(10); +Assert.equal(11, childadd.move().x); +let a: Addx = childadd.move(); +Assert.equal(13, a.add().x); + +interface InterAddx { + x: number; +} +class Addx2 implements InterAddx { + x = 1; + setadd(): this { + this.x++; + return this; + } +} +let b1 = new Addx2(); +Assert.equal(2, b1.setadd().x); +let interb1: InterAddx = b1.setadd(); +Assert.equal(3, interb1.x); + +interface InterP { + Area(x: number, y: number): number; +} +class Rectangle implements InterP { + Area(x: number, y: number): number { + return x * y; + } + setRec(): this { + return this; + } +} +let b2 = new Rectangle(); +Assert.equal(200, b2.Area(10, 20)); +let interb2: InterP = b2.setRec(); +Assert.equal(200, interb2.Area(10, 20)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..4075363ecc2da10e64e4bf2ea07f3799060d7149 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_4.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + as is the case with every object type, type references (section 3.3.1) to the class + will appear to have the members of the global interface type named 'Object' + unless those members are hidden by members with the same name in the class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Compute { + constructor(public num1: number, public num2: number) { } + public hypot() {return "what are you doing?" } + static initial = new Compute(0, 0); +} +let p = new Compute(1, 2); +let obj = new Object(); +Assert.equal(p.toString(), obj.toString()); +Assert.isString(p.hypot()); +Assert.equal(0,Compute.initial.num1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..7126a82c870b8b01a3bff4d9742e24a39eaf8a46 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_5.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type parameters of a generic class declaration are in scope in the entire declaration and may be referenced in the ClassHeritage and ClassBody. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MyClass{ + myArry: T[] = []; + add(num: T): void { + this.myArry.push(num); + } + max(): T { + let mNum = this.myArry[0]; + for (let i = 0; i < this.myArry.length; i++) { + if (mNum < this.myArry[i]) { + mNum = this.myArry[i]; + } + } + return mNum; + } +} +let h_m: MyClass = new MyClass(); +h_m.add(5); +h_m.add(4); +h_m.add(9); +h_m.add(15); +Assert.equal(h_m.max(), 15); +let h_m2: MyClass = new MyClass(); +h_m2.add('b'); +h_m2.add('a'); +h_m2.add('w'); +h_m2.add('f'); +Assert.equal(h_m2.max(), 'w'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..227a5aaefe0897c2277324f441a209d63727dfc5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_6.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The constructor function type created by the class declaration must be assignable to the base class constructor function type + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Person { + name: string; + age: number; + constructor(name: string, age: number) { + this.name = name; + this.age = age; + } +} +class Student extends Person { + constructor(name: string, age: number, public id: string) { + super(name, age); + this.id = id; + } +} +let stu1: Student = new Student("xiao", 18, "001"); +let per1: Person = stu1; +Assert.equal("xiao", per1.name); +Assert.equal(18, per1.age); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..3e4006441032b56064a7b1ee426f7946f1b48a93 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If a class omits a constructor declaration, an automatic constructor is provided. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A { + x: number = 1; + y: string = "go"; +} +let a = new A(); +Assert.equal(a.x, 1); +Assert.equal(a.y, "go"); +class B { + x: number; + y: string; + constructor(x: number = 1, y: string = "go") { + this.x = x; + this.y = y; + } +} +let b = new B(); +Assert.equal(a.x, 1); +Assert.equal(a.y, "go"); +Assert.equal(b.x, 1); +Assert.equal(b.y, "go"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1faf83d9b390b20d1822ce55e73276556e84c9d8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_2.ts @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a derived class, the automatic constructor has the same parameter list (and possibly overloads) as the base class constructor. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Point1 { + x: string; + y: string; + constructor(x: string, y: string) { + this.x = x; + this.y = y; + } + toString() { + return this.x + " " + this.y; + } +} +class ColorPoint extends Point1 { + color: string; + constructor(x: string, y: string, color: string) { + super(x, y); + this.color = color; + } + toString() { + return this.color + " " + super.toString(); + } +} +let co = new ColorPoint("A", "B", "blue"); +Assert.equal(co.x, "A"); +Assert.equal(co.y, "B"); +Assert.equal(co.color, "blue"); +Assert.equal(co.toString(), "blue A B"); +class ColorPoint2 extends Point1 { + constructor(x: string, y: string) { + super(x, y); + } +} +let co2 = new ColorPoint2("a", "b"); +Assert.equal(co2.x, "a"); +Assert.equal(co2.y, "b"); +Assert.equal(co2.toString(), "a b"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..a7a294bd1108880a01487b748ebb085822ce345e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_3.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a class with no extends clause, the automatic constructor has no parameters + and performs no action other than executing the instance member variable initializers (section 8.4.1), if any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Foo { + constructor(public x: number = 1, public y: string = "go") { + this.x = x; + this.y = y; + } +} +let foo = new Foo(); +Assert.equal(foo.x, 1); +Assert.equal(foo.y, "go"); +class Foo2 { + x: number; + y: string; + constructor(x: number, y: string) { + this.x = x; + this.y = y; + } +} +let foo2 = new Foo2(2, "go"); +Assert.equal(foo2.x, 2); +Assert.equal(foo2.y, "go"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..79d6a94147e102a0b1363e141bcf625bf5ef9323 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_1.ts @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Constructor declarations that specify a body are called constructor implementations and + constructor declarations without a body are called constructor overloads. + It is possible to specify multiple constructor overloads in a class, + but a class can have at most one constructor implementation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Animal1 { + name: string | undefined; + age: number | undefined; + constructor(); + constructor(name: string); + constructor(age: number); + constructor(name: string, age: number); + constructor(nameorage?: string | number, age?: number) { + if (typeof nameorage == "number") { + this.age = nameorage; + } + if (typeof nameorage == "string") { + this.name = nameorage; + } + if (age) { + this.age = age; + } + } +} +let tt1 = new Animal1("caihua1",NaN); +Assert.equal(tt1.name,"caihua1"); +Assert.equal(tt1.age, undefined); +let tt2 = new Animal1("caihua2", 12); +Assert.equal(tt2.name, "caihua2"); +Assert.equal(tt2.age, 12); +let tt3 = new Animal1("caihua3"); +Assert.equal(tt3.name, "caihua3"); +let tt4 = new Animal1(1230); +Assert.equal(tt4.age, 1230); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..2af41b9e05250934db5a2da11a0263bf7833cbf3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_2.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A parameter of a ConstructorImplementation may be prefixed with a public, private, or protected modifier. + This is called a parameter property declaration and is shorthand for declaring a property with the same name as the parameter + and initializing it with the value of the parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MyClass1 { + constructor(public num1: number, private num2: number = 1) { } + get foo(): number { + return this.num2; + } +} +class MyClass2 { + public num1: number; + protected num2: number; + constructor(num1: number, num2: number) { + this.num1 = num1; + this.num2 = num2; + } + get foo(): number { + return this.num2; + } +} +let p1 = new MyClass1(1, 2); +Assert.equal(p1.num1, 1); +Assert.equal(p1.foo, 2); +let p2 = new MyClass2(3, 4); +Assert.equal(p2.foo, 4); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..8138bbfb04344a0489a5acbbd6a26c2926e57e01 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_3.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The function body of a constructor is permitted to contain return statements. If return statements specify expressions, + those expressions must be of types that are assignable to the this-type of the class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + + +class myClass1 { + num: number; + constructor(num: number) { + this.num = num + return; + } +} +let myTest1 = new myClass1(5); +Assert.equal(myTest1.num, 5); +class myClass2 { + num: number; + constructor(num: number) { + this.num = num + return { num: 10 }; + } +} +let myTest2 = new myClass2(5); +Assert.equal(myTest2.num, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..ce311d0f45fd12ea73952563c8203bccffd34931 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_4.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type parameters of a generic class are in scope and accessible in a constructor declaration. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class myClass{ + num: T; + constructor(num: T) { + this.num = num; + } +} +let myTest = new myClass(10); +Assert.equal(myTest.num, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..4ee4119aa917c9b644e92bca9419e37ce4127d21 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_5.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A parameter property declaration may declare an optional parameter, + but the property introduced by such a declaration is always considered a required property. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class myClass { + constructor(public num1: number, public num2?: number, public num3: number = 10) { + this.num1 = num1; + this.num2 = num2; + this.num3 = num3; + } +} +let myTest = new myClass(3); +Assert.equal(myTest.num1, 3); +Assert.equal(myTest.num3, 10); +Assert.equal(myTest.num2, undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..3c0093b044dc414fe7f9887a28e9a56f0ea0d2f4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_6.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a class with no constructor declaration, an automatic constructor is provided, as described in section 8.3.3. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class myClass { + public a: string = "test"; + public b: number = 1; +} +class myClass2 { + public c: number = 2; +} +let myTest: myClass = new myClass(); +Assert.equal(myTest.a, "test"); +Assert.equal(myTest.b, 1); +let myTest2: myClass2 = new myClass2(); +Assert.equal(myTest2.c, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/super_calls/super_calls.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/super_calls/super_calls.ts new file mode 100644 index 0000000000000000000000000000000000000000..c938838f7102f3b4a134f517d8f10c4eb8b622dc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/constructor_declarations/super_calls/super_calls.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Super calls (section 4.9.1) are used to call the constructor of the base class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class P { + public num1: number; + public num2: number; + constructor(num1: number, num2: number) { + this.num1 = num1; + this.num2 = num2; + } +} +class HueP extends P { + constructor(num1: number, num2: number, public hue: string) { + super(num1, num2); + } +} +let p1 = new HueP(1, 2, "red"); +Assert.equal(p1.num1, 1); +Assert.equal(p1.num2, 2); +Assert.equal(p1.hue, "red"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/decorators/decorators.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/decorators/decorators.ts new file mode 100644 index 0000000000000000000000000000000000000000..cf935f606304774c1c2c732e8039266c0199d033 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/decorators/decorators.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Class decorator. + module: ESNext + isCurrent: true + experimentalDecorators: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function create(name:string,age:number){ + return function(constructor:T){ + return class extends constructor { + name = name + age = age + } + } +} + +@create("小张",22) +class Man { + name:string + height = 180 + constructor(name:string){ + this.name = name + } +} +let man = new Man("jack"); +Assert.equal(man.height,"180"); +Assert.notEqual(man.name,"jack"); +Assert.equal(man.name,"小张") diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/index_member_declarations/index_member_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/index_member_declarations/index_member_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..d0584deafc92a038e7ab26bf7e2f4022818634d7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/index_member_declarations/index_member_declarations.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + All instance property members of a class must satisfy + the constraints implied by the index members of the class as specified in section 3.9.4. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class ind { + name: string; + [index: string]: number | string; + constructor(name: string) { + this.name = name; + } +} +let a = new ind("aa"); +a.name = "pig"; +a.age = 12; +Assert.equal(a.age, 12); +a.add = "qindao"; +Assert.equal(a.add, "qindao"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..90ac66d160ee7f93b96a729dd649df672e89692c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_1.ts @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Public property members can be accessed everywhere without restrictions. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Base { + public a: number; + constructor(a: number) { + this.a = a; + } + public add() { + this.a++; + } +} +class Derived extends Base { + public b: number; + constructor(a: number, b: number) { + super(a); + this.b = b; + } + public geta() { + return this.a; + } +} +let derived: Derived = new Derived(1, 2); +let x = derived.a; +Assert.equal(x, 1); +let y = derived.geta(); +Assert.equal(y, 1); +let z: Base = new Base(3); +Assert.equal(z.a, 3); +z.add(); +Assert.equal(z.a, 4); +class MyClass{ + public myArry: T[] = []; + public add(num: T): void { + this.myArry.push(num); + } + public max(): T { + let mNum = this.myArry[0]; + for (let i = 0; i < this.myArry.length; i++) { + if (mNum < this.myArry[i]) { + mNum = this.myArry[i]; + } + } + return mNum; + } +} +let h_m: MyClass = new MyClass(); +h_m.add(5); +h_m.add(4); +h_m.add(9); +h_m.add(15); +Assert.equal(h_m.max(), 15); +let h_m2: MyClass = new MyClass(); +h_m2.add('b'); +h_m2.add('a'); +h_m2.add('w'); +h_m2.add('f'); +Assert.equal(h_m2.max(), 'w'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0a8a658283b6a815c821283b30bfe28bea4c9f38 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_2.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain m copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Private property members can be accessed only within their declaring class. Specifically, + m private member M declared in m class C can be accessed only within the class body of C. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Base { + private x: number = 1; + private y: number = 2; + addx() { + this.x++; + } + get foo() { + return this.x; + } + public addxy(): number { + return this.x + this.y; + } + static fun(m: Base, n: Derived) { + m.x = 1; + n.x = 1; + m.y = 1; + n.y = 1; + } +} +class Derived extends Base { } +let m: Base = new Base(); +m.addx(); +Assert.equal(m.foo, 2); +Assert.equal(m.addxy(), 4); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..b91989153a64e842972eb54665c624242d25fab3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/accessibility/accessibility_3.ts @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Protected property members can be accessed only within their declaring class and classes derived from their declaring class, + and a protected instance property member must be accessed through an instance of the enclosing class or a subclass thereof. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Base { + protected x: number = 1; + protected y: number = 4; + protected addxy() { + return this.x + this.y; + } + public addx() { + this.x++; + } + public get foo() { + return this.x; + } + static fun(m: Base, b: Derived) { + m.x = 1; + b.x = 1; + m.y = 1; + b.y = 1; + } +} +class Derived extends Base { + public get add(): number { + return this.addxy() + } + public get foo() { + return this.x; + } + static fun(b: Derived) { + b.x = 1; + b.y = 1; + } +} +let m: Base = new Base(); +m.addx(); +Assert.equal(m.foo, 2); +let b: Derived = new Derived(); +b.addx(); +Assert.equal(b.foo, 2); +Assert.equal(b.add, 6); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..25ab9d6c7fe00a650da1d6312d2cd755acd0c56f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_1.ts @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Within the constructor and instance member functions of a class, the type of this is the this-type of that class type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Ra { + public x: number; + public y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + public addx(): this { + this.x++; + return this + } + public addy(): this { + this.y++; + return this; + } + public add() { + return this.x + this.y; + } +} +class Rb extends Ra { + public z: number; + constructor(x: number, y: number, z: number) { + super(x, y); + this.z = z; + } + public addz(): this { + this.z++; + return this; + } +} +let a: Ra = new Ra(1, 1); +let b: Rb = new Rb(1, 1, 1); +Assert.equal(a.addx().x, 2); +Assert.equal(a.addx().y, 1); +Assert.equal(a.addx().add(), 5); +Assert.equal(a.addy().x, 4); +Assert.equal(a.addy().y, 3); +Assert.equal(a.add(), 7); +Assert.equal(b.addx().x, 2); +Assert.equal(b.addx().y, 1); +Assert.equal(b.addy().x, 3); +Assert.equal(b.addy().y, 3); +Assert.equal(b.addz().z, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7686b8c6f2185e1cd4385cb41d8b84a650d80a42 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_2.ts @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + All instance property members of a class must satisfy the constraints implied by the index members of the class . + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Ra { + public h_x: number; + constructor(h_x: number) { + this.h_x = h_x; + } + public h_f() { + this.h_x++; + } + public h_g(): any { + return this.h_x; + } + static h_s: string = "aa"; +} +let aa: Ra = new Ra(1); +Assert.equal(aa.h_x, 1); +aa.h_f(); +Assert.equal(aa.h_x, 2); +class Rb extends Ra { + public h_y: number; + constructor(h_x: number, h_y: number) { + super(h_x); + this.h_y = h_y; + } + public h_g(): boolean { return false; } +} +let bb: Rb = new Rb(1, 2); +Assert.equal(bb.h_x, 1); +Assert.equal(bb.h_y, 2); +bb.h_f(); +Assert.equal(bb.h_x, 2); +Assert.equal(bb.h_g(), false); +interface Rc { + h_x: number; + h_f: () => void; + h_g: () => any; +} +let c: Rc = aa; +Assert.equal(c.h_g(), 2); +interface Rd { + h_x: number; + h_y: number; + h_f: () => void; + h_g: () => boolean; +} +let d: Rd = bb; +Assert.equal(d.h_g(), false); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..870c54edb0c1eeef5a8bf3a666e234d181cfa416 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/class_types/class_types_3.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The class type has a property for each constructor parameter declared with a public, private, or protected modifier. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Ra { + public x: number = 10; + private y: number = 10; + protected z: number = 10; + constructor() { }; + public f() { + this.x++; + return this; + } + public gety(): number { + return this.y; + } + public getz(): number { + return this.z; + } + protected fun(): boolean { + return false; + } + public getfun() { + return this.fun(); + } +} +class Rb extends Ra { + protected z: number = 20; + protected fun(): boolean { + return true; + } +} +let a: Ra = new Ra(); +Assert.equal(a.x, 10); +Assert.equal(a.gety(), 10); +Assert.equal(a.getz(), 10); +Assert.equal(a.getfun(), false); +Assert.equal(a.f().x, 11); +let b: Rb = new Rb(); +Assert.equal(b.x, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0eeb7cee806f8b1ffe03acb94efa07d434502a99 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_1.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the class contains no constructor declaration and has no base class, + a single construct signature with no parameters, + having the same type parameters as the class (if any) + and returning an instantiation of the class type with those type parameters passed as type arguments. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A { + a: number = 0; + b: number = 0; + constructor() { } +} +let a = new A(); +Assert.equal(a.a, 0); +Assert.equal(a.b, 0); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..83f781677ceec17667009b75ec2108ef5befef10 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_2.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the class contains no constructor declaration and has a base class, + a set of construct signatures with the same parameters as those of the base class constructor function type + following substitution of type parameters with the type arguments specified in the base class type reference, + all having the same type parameters as the class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A1 { + public numa: number; + constructor(a: number) { + this.numa = a; + } +} +class B1 extends A1 { + public numb: number; + constructor(a: number, b: number) { + super(a); + this.numb = b; + } +} +let a: A1 = new A1(1); +Assert.equal(a.numa, 1); +let b: B1 = new B1(2, 2); +Assert.equal(b.numa, 2); +Assert.equal(b.numb, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..4d8e22be4b3c1176ce828461dba0cf556ad401c9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_3.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the class contains a constructor declaration with no overloads, + a construct signature with the parameter list of the constructor implementation, + having the same type parameters as the class (if any) + and returning an instantiation of the class type with those type parameters passed as type arguments. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A { + public x: number; + public y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } +} +let a = new A(1, 2); +Assert.equal(a.x, 1); +Assert.equal(a.y, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..3ccc9e317da9150f5c3b482dcaae6c0e664d6e51 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_4.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the class contains a constructor declaration with overloads, a set of construct signatures + with the parameter lists of the overloads, all having the same type parameters as the class (if any) + and returning an instantiation of the class type with those type parameters passed as type arguments. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type TypeSummation = { + width?: number; + height?: number; +}; +class summation { + public width; + public height; + constructor(width: number, height: number); + constructor(ParamObje_: TypeSummation); + constructor(ParamObje_Obj_: any, height_ = 0) { + if (typeof ParamObje_Obj_ === "object") { + const { width, height } = ParamObje_Obj_; + this.width = width; + this.height = height; + } else { + this.width = ParamObje_Obj_; + this.height = height_; + } + } + sunArea(): number { + return this.width * this.height; + } +} +const sun = new summation(4, 5); +Assert.equal(sun.sunArea(), 20); +const obj: TypeSummation = { width: 10, height: 20 }; +Assert.equal(new summation(obj).sunArea(), 200); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..98ad0473e630ac75f32f9b9dffd67100c89f506c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_5.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A property for each static member variable declaration in the class body. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A1 { + public numa: number; + constructor(a: number) { + this.numa = a; + } + static c: number = 20; +} +class B1 extends A1 { + public numb: number; + constructor(a: number, b: number) { + super(a); + this.numb = b; + } +} +let a: A1 = new A1(10); +Assert.equal(a.numa, 10); +Assert.equal(A1.c, 20); +let b: B1 = new B1(10, 20); +Assert.equal(b.numa, 10); +Assert.equal(b.numb, 20); +Assert.equal(B1.c, 20); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..d97b249c6eadd7007f8e78b97a1e8474b141f7f6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_6.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A property of a function type for each static member function declaration in the class body. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A1 { + public numa: number; + constructor(a: number) { + this.numa = a; + } + static c = 10; + static f(): void { + this.c++; + } +} +class B1 extends A1 { + public numb: number; + constructor(a: number, b: number) { + super(a); + this.numb = b; + } + static f() { + this.c++; + return this.c + } +} +Assert.equal(A1.c, 10); +A1.f(); +Assert.equal(A1.c, 11); +Assert.equal(B1.c, 11); +Assert.equal(B1.f(), 12); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..8435c66f7424455fdd959e7bddfbb9be2ff10bb6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/constructorc_function_types/constructor_function_types_7.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A property named 'prototype', the type of which is an instantiation of the class type with type Any supplied + as a type argument for each type parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Animal { + public name: string; + constructor(name: string) { + this.name = name; + } +} +Assert.equal(typeof (Animal.prototype), "object"); +let cat = new Animal("Cat") +Assert.equal(Object.getPrototypeOf(cat) === Animal.prototype, true); +Assert.equal(Object.getPrototypeOf(Animal.prototype) === Object.prototype, true); +Assert.equal(Object.getPrototypeOf(Object.prototype) === null, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c813e63ef592d777307e5f4fa07fd10a018cae8f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_1.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A derived class inherits all members from its base class it doesn't override. + Inheritance means that a derived class implicitly contains all non-overridden members of the base class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Shape { + public color: string = "black"; + protected side: number = 10; + constructor() { }; + public switchColor() { + this.color = this.color === "black" ? "white" : "black"; + } +} +class Circle extends Shape { + get gside() { + return this.side; + } +} +let circle: Circle = new Circle(); +let a: string = circle.color; +Assert.equal(a, "black"); +circle.switchColor(); +let b: string = circle.color; +Assert.equal(b, "white"); +Assert.equal(10, circle.gside); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..093a81f893022e91d4443bd9b5a543607ddc843a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_2.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Only public and protected property members can be overridden. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Shape { + public color: string = "black"; + protected side: number = 10; + constructor() { }; + public switchColor() { + this.color = this.color === "black" ? "white" : "black"; + } +} +class Circle extends Shape { + public color: string = "red"; + protected side: number = 11; + get gside() { + return this.side; + } + public switchColor() { + this.color = this.color === "red" ? "white" : "black"; + } +} +let shape: Shape = new Shape(); +Assert.equal(shape.color, "black"); +shape.switchColor(); +Assert.equal(shape.color, "white"); +let circle: Circle = new Circle(); +Assert.equal(circle.color, "red"); +circle.switchColor(); +Assert.equal(circle.color, "white"); +Assert.equal(11, circle.gside); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..42c1eecee2348297e2a310b9c5cb522a50182002 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_3.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Base class static property members can be overridden by derived class static property members + of any kind as long as the types are compatible. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Shape { + static color: string = "black"; + static switchColor() { + this.color = this.color === "black" ? "white" : "black"; + } +} +class Circle extends Shape { + static color: string = "red"; + static switchColor() { + this.color = this.color === "red" ? "green" : "red"; + } +} +let a = Circle.color; +Assert.equal(a, "red"); +Circle.switchColor(); +let b = Circle.color; +Assert.equal(b, "green"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..1b55b81f593d6bfe5f30b1ee26ed50d7369a8b69 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_4.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Base class instance member variables and accessors can be overridden by derived class instance member variables and accessors. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MyClass { + num: number = 20; + constructor(num: number) { + this.num = num; + } + get() { + return this.num + } + set(num: number) { + this.num = num; + } +} +class myClass extends MyClass { + constructor(num: number) { + super(num); + this.num = num; + } + get() { + return 10 + } + set(num: number) { + this.num = num; + } +} +let myTest = new myClass(5); +Assert.equal(myTest.num, 5); +Assert.equal(myTest.get(), 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..aa473982b5cf61d69d30ee358981e015034f3dab --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_5.ts @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A property member in a derived class is said to override a property member in a base class + when the derived class property member has the same name and kind (instance or static) as the base class property member. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Shape { + public color: string = "black"; + protected name: string = "shape"; + public switchColor() { + this.color = this.color === "black" ? "white" : "black"; + } + protected f(a: Shape, b: Circle) { + a.color = "blue"; + b.color = "blue"; + } + static size: number = 10; +} +class Circle extends Shape { + public color: string = "red"; + protected name: string = "circle"; + public switchColor() { + this.color = this.color === "red" ? "green" : "red"; + } + static size: number = 12; + protected f(a: Shape, b: Circle) { + a.color = "pink"; + b.color = "pink"; + } +} +let shape: Shape = new Shape(); +Assert.equal(shape.color, "black"); +shape.switchColor(); +Assert.equal(shape.color, "white"); +Assert.equal(Shape.size, 10); +let circle = new Circle(); +Assert.equal(circle.color, "red"); +circle.switchColor(); +Assert.equal(circle.color, "green"); +Assert.equal(Circle.size, 12); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5683f2888a2bfce8a2f25b9fba30c9fe5a28ee2e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_1.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Members are either instance members or static members. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Person { + private _name: string; + public age: number; + constructor(name: string, age: number) { + this._name = name; + this.age = age; + } + public set name(name: string) { + this._name = name; + } + public get name() { + return this._name; + } + static language: string = "english"; + static ageAdd() { + return this.language + "aaa"; + } +} +let per: Person = new Person("rain", 22); +Assert.equal(per.name, "rain"); +Assert.equal(per.age, 22); +Assert.equal(Person.language, "english"); +Assert.equal(Person.ageAdd(), "englishaaa"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..478ebc4313aad77d785b545fd07d08e3d8bd9b92 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_2.ts @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Instance members are members of the class type and its associated this-type. Within constructors, + instance member functions, and instance member accessors, + the type of this is the this-type of the class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Counter { + private count: number = 0; + constructor(count_: number) { + this.count = count_; + } + public add(): this { + this.count++; + return this; + } + public subtract(): this { + this.count--; + return this; + } + public get Result(): number { + return this.count; + } +} +let counter = new Counter(1); +counter.add(); +Assert.equal(counter.add(), counter); +Assert.equal(counter.Result, 3); +counter.subtract(); +Assert.equal(counter.subtract(), counter); +Assert.equal(counter.Result, 1); +class Compute { + constructor(public num1: number, public num2: number) { } + public hypot() { + return Math.sqrt(this.num1 * this.num1 + this.num2 * this.num2); + } + static initial = new Compute(0, 0); +} +class ChildP extends Compute { + constructor(public x: number, public y: number, public z: number) { + super(x, y); + this.z = z; + } + public move(): this { + this.x += 1; + this.y += 1; + this.z += 1; + return this + } +} +let childp: ChildP = new ChildP(1, 2, 3); +childp.move(); +Assert.equal(childp.x, 2); +Assert.equal(childp.y, 3); +Assert.equal(childp.z, 4); +Assert.equal(childp.move(), childp); +let count: ChildP = new ChildP(4,3,1); +Assert.equal(count.hypot(),5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..234ee19637e562f3e9cec71dd22e80364d1dcdbd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/members/instance_and_static_members/instance_and_static_members_3.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Static members are declared using the static modifier and are members of the constructor function type. + Within static member functions and static member accessors, + the type of this is the constructor function type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Person { + static n2: number = 20; + tool(): number { + return 30; + } + static tool1(): number { + return this.n2; + } + static tool2() { + this.n2++; + return this; + } + static get foo(): number { + return this.n2; + } +} +let a: number = Person.n2; +Assert.equal(a, 20); +let b: number = Person.tool1(); +Assert.equal(b, 20); +let c: number = Person.foo; +Assert.equal(c, 20); +Assert.equal(Person, Person.tool2()); +let d: Person = new Person(); +Assert.equal(d.tool(),30); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/dynamic_property_declarations/dynamic_property_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/dynamic_property_declarations/dynamic_property_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..c11af2fa48c4bdaa9d6ab232bb2e008f7f520b48 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/dynamic_property_declarations/dynamic_property_declarations.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A dynamic property declaration does not introduce a property in the class type or constructor function type. + The property name expression of a dynamic property assignment must be of type Any or the String, Number, or Symbol primitive type. + The name associated with a dynamic property declarations is considered to be a numeric property name if the property name expression is of type Any or the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class computer { + [compute:string]:any + constructor( + public name:string, + public age:number + ){} + sayOne(){return "one"} +} +let uce = new computer("aa",22); +uce.pid = "223"; +uce.id=1; +Assert.equal(uce.name,"aa"); +Assert.equal(typeof uce,"object"); +Assert.equal(uce.id+uce.pid,"1223"); +Assert.equal(uce.sayOne(),"one"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_accessor_declarations/member_accessor_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_accessor_declarations/member_accessor_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..d4789e76dca2100f2486390cd99d16ec27de5332 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_accessor_declarations/member_accessor_declarations.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A member accessor declaration declares an instance member accessor or a static member accessor. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A { + private _foo: number = 0; + get foo(): number { + return this._foo; + } + set foo(value: number) { + this._foo = value; + } + private static a: number = 0; + static get aa(): number { + return this.a; + } + static set aa(value: number) { + this.a = value; + } +} +let x = new A(); +Assert.equal(x.foo, 0); +x.foo = 10; +Assert.equal(x.foo, 10); +Assert.equal(A.aa, 0); +A.aa = 20; +Assert.equal(A.aa, 20); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..81ff4b1d7b7166b9ae56d386e393a7a1de310b88 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A member function declaration declares an instance member function or a static member function. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MyClass { + constructor(public x: number, public y: number) { } + public distance(p: MyClass) { + let dx = this.x - p.x; + let dy = this.y - p.y; + return Math.sqrt(dx * dx + dy * dy); + } + static add(p1: MyClass) { + p1.x++; + return p1.x + } +} +let p1: MyClass = new MyClass(2, 2); +let p2: MyClass = new MyClass(1, 1); +Assert.equal(p1.distance(p2), Math.sqrt(2)); +Assert.equal(MyClass.add(p1), 3); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..913abdd47cc0f764ecbd0ce5ec2631f3bc9524a6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the body of an instance member function declaration, this is of the this-type (section 3.6.3) of the class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Circle { + radius: number = 1; + area(): number { + return Math.PI * this.radius * this.radius; + } +} +let c = new Circle(); +Assert.equal(c.radius, 1); +Assert.equal(c.area(), Math.PI); +c.radius = 2; +Assert.equal(c.area(), Math.PI * 4); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..b5b27f92c4614767abc3eb89a99a9e6c00432b20 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_3.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the body of a static member function declaration, the type of this is the constructor function type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MyClass { + constructor(public x: number = 3, public y: number = 3) { } + static z: number; + static pro() { + return this.prototype; + } + static returnz() { + this.z; + } +} +Assert.equal(MyClass.pro(), "[object Object]"); +Assert.equal(MyClass.returnz(), undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..00b736c2f0c29972904f826c82b7949cc650ba68 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_4.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A member function can access overridden base class members using a super property access (section 4.9.2). + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MyClass { + constructor(public x: number, public y: number) { } + public add() { + return this.x + this.y; + } +} +class MyClass2 extends MyClass { + constructor(x: number, y: number, public z: number) { + super(x, y); + } + public add() { + return super.add() + this.z; + } +} +let p = new MyClass(1, 2); +Assert.equal(p.x, 1); +Assert.equal(p.y, 2); +Assert.equal(p.add(), 3); +let cp = new MyClass2(1, 2, 3); +Assert.equal(cp.z, 3); +Assert.equal(cp.add(), 6); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..9d52a76633178726fc54012f2428c066c51a7027 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_5.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a static member function,a call to 'new this()' may actually invoke a derived class constructor + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class TP { + constructor(public a: number = 1, public b: number = 1) { } + static createthis() { + return new this(); + } +} +class CP extends TP { + constructor(public a: number = 2, public b = 2) { + super(); + } +} +let x = TP.createthis(); +let y = CP.createthis(); +Assert.equal(x.a, 1); +Assert.equal(y.a, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0858958081e9ea26a6388a98794709b023868d72 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_1.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Initializers in static member variable declarations are executed once when the containing script or module is loaded. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MyClass { + constructor(public x: number, public y: number) { } + public addc() { + MyClass.c++; + } + static po = new MyClass(0, 0); + static c: number = 10; +} +let a: MyClass = new MyClass(1, 1); +Assert.equal(MyClass.c, 10); +a.addc(); +Assert.equal(MyClass.c, 11); +let b: MyClass = new MyClass(1, 1); +b.addc(); +Assert.equal(MyClass.c, 12); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f6709f6ede30e44b2c7e45fb801f19e6093bca6a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_2.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Instance member variable initializers are equivalent to assignments to properties of this in the constructor + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Staff1 { + public name: string; + public place: string; + public work: boolean; + constructor(name: string, place: string, work: boolean) { + this.name = name; + this.place = place; + this.work = work; + } + initializer(name: string, place: string, work: boolean) { + this.name = name; + this.place = place; + this.work = work; + } +} +class Staff2 { + public name: string; + public place: string; + public work: boolean; + constructor(name: string, place: string, work: boolean) { + this.name = name; + this.place = place; + this.work = work; + } +} +let em1 = new Staff1("zhangsan", "shanghai", true); +em1.initializer("wangwu", "hainan", true); +let em2 = new Staff2("lisi", "qingdao", true); +Assert.equal(em1.name, "wangwu"); +Assert.equal(em1.place, "hainan"); +Assert.equal(em1.work, true); +Assert.equal(em2.name, "lisi"); +Assert.equal(em2.place, "qingdao"); +Assert.equal(em2.work, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..562151a0f7048b341c5a6e07cb96adc5fc1d9eda --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_3.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A member variable declaration declares an instance member variable or a static member variable. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class MyClass { + public x: number; + public y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + static z: number = 3; +} +class MyClass2 extends MyClass { + constructor(x: number, y: number, public color: string) { + super(x, y); + } + static r: number = 10; +} +let p = new MyClass(1, 2); +Assert.equal(p.x, 1); +Assert.equal(p.y, 2); +Assert.equal(MyClass.z, 3); +let cp = new MyClass2(4, 5, "red"); +Assert.equal(cp.x, 4); +Assert.equal(cp.y, 5); +Assert.equal(MyClass2.r, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..04668be36da59195d98e3722c8db629f8543abcc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_4.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Instance member variable initializers are equivalent to assignments to properties of this in the constructor + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class A { + public x: number = 1; + public y: string = "name"; + public z: boolean = false; + constructor() { } +} +class B { + public x: number = 1; + public y: string = "name"; + public z: boolean + constructor(z: boolean = false) { + this.z = z; + } +} +let a = new A(); +Assert.equal(a.z, false); +let b = new B(); +Assert.equal(b.z, false); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f6eacfb220d22d3c876f7be572ed634ab2bcf6e9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_1.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Member declarations with a static modifier are called static member declarations. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Compute { + constructor(public x: number, public y: number) { } + public range(p1: Compute, p2: Compute) { + let dx = p1.x - p2.x; + let dy = p1.y - p2.y; + return Math.sqrt(dx * dx + dy * dy); + } + static getx() { + return this.x; + } + static po = new Compute(0, 0); + static x: number = 10; +} +let p1: Compute = new Compute(2, 2); +let p2: Compute = new Compute(1, 1); +Assert.equal(p1.range(p1, p2), Math.sqrt(2)); +Assert.equal(Compute.getx(), 10); +Assert.equal(p1.range(p1, Compute.po), Math.sqrt(8)); +Assert.equal(Compute.x, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..12ffdf3f7d2e5089bbd97c16b92bbee63a028660 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_2.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + it is possible to have instance and static property members with the same name. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../../suite/assert.js" + +class myClass { + public x: number; + public y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + public add(): number { + return this.x + this.y; + } + static x: number = 3; + static add(): number { + this.x++; + return this.x; + } +} +let myTest = new myClass(1, 2); +Assert.equal(myTest.x, 1); +Assert.equal(myTest.y, 2); +Assert.equal(myTest.add(), 3); +Assert.equal(myClass.x, 3); +Assert.equal(myClass.add(), 4); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..e8432500f8b6d6937d47806aa24408fcab3633cd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/classes/property_member_declarations/property_member_declarations_3.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Every class automatically contains a static property member named 'prototype' + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Compute { + public x: number; + public y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + static p = new Compute(0, 0); +} +class ColoredPoint extends Compute { + constructor(x: number, y: number, public color: string) { + super(x, y); + } +} +Assert.equal(ColoredPoint.prototype.color, undefined); +Assert.equal(ColoredPoint.prototype.x, undefined); +Assert.equal(ColoredPoint.prototype.y, undefined); +Assert.equal(Compute.prototype.x, undefined); +Assert.equal(Compute.prototype.y, undefined); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/constant_enum_declarations/constant_enum_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/constant_enum_declarations/constant_enum_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c56963def63335c314e12a63bad7dff94826d01 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/constant_enum_declarations/constant_enum_declarations.ts @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + in a constant enum declaration, all members must have constant values and it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + it is an error to reference a constant enum object in any other context than a property access that selects one of the enum's members. + the only permitted references to the enum object are those that are replaced with an enum member value. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const enum CED { + None = -1, + False, + True = 1, + DEF = 1024, + Val = DEF / 8, +} +Assert.equal(CED.None, -1); +Assert.equal(CED.False, 0); +Assert.equal(CED.True, 1); +Assert.equal(CED.DEF, 1024); +Assert.equal(CED.Val, 128); +Assert.equal(CED["None"], -1); +Assert.equal(CED["False"], 0); +Assert.equal(CED["True"], 1); +Assert.equal(CED["DEF"], 1024); +Assert.equal(CED["Val"], 128); + +enum CED_COPY { + None = -1, + False, + True = 1, + DEF = 1024, + Val = DEF / 8, +} +Assert.equal(CED_COPY[-1], "None"); +Assert.equal(CED_COPY[0], "False"); +Assert.equal(CED_COPY[1], "True"); +Assert.equal(CED_COPY[1024], "DEF"); +Assert.equal(CED_COPY[128], "Val"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/declaration_merging/declaration_merging_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/declaration_merging/declaration_merging_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..3cad444c101b8692e3ad8c224728b0e36f1efb54 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/declaration_merging/declaration_merging_1.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + enums are "open-ended" and enum declarations with the same qualified name relative to a common root define a single enum type and contribute to a single enum object. + it isn't possible for one enum declaration to continue the automatic numbering sequence of another, and when an enum type has multiple declarations, only one declaration is permitted to omit a value for the first member. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +enum A { + A1, + A2, +} +enum A { + A3 = 3, + A4, +} +Assert.equal(A.A1, 0); +Assert.equal(A.A2, 1); +Assert.equal(A.A3, 3); +Assert.equal(A.A4, 4); +namespace NSP { + export enum A { + A1, + A2, + } + export enum A { + A3 = 2, + A4, + } +} +Assert.equal(NSP.A.A1, 0); +Assert.equal(NSP.A.A2, 1); +Assert.equal(NSP.A.A3, 2); +Assert.equal(NSP.A.A4, 3); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/declaration_merging/declaration_merging_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/declaration_merging/declaration_merging_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f7efe29be57c509a1826e15e9f8d8a6e4df79273 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/declaration_merging/declaration_merging_2.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when enum declarations are merged, they must either all specify a const modifier or all specify no const modifier. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const enum T1 { + Tex0, + Tex1, +} +const enum T1 { + Tex2 = 2, + Tex3, +} +Assert.equal(T1.Tex0, 0); +Assert.equal(T1.Tex1, 1); +Assert.equal(T1.Tex2, 2); +Assert.equal(T1.Tex3, 3); +enum T2 { + Tex0, + Tex1, +} +enum T2 { + Tex2 = 2, + Tex3, +} +Assert.equal(T2.Tex0, 0); +Assert.equal(T2.Tex1, 1); +Assert.equal(T2.Tex2, 2); +Assert.equal(T2.Tex3, 3); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..cca34206d6a34924b73ad674717b2e46b47bbaec --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_1.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an EnumDeclaration introduces a named type and a named value in the containing declaration space. + The enum type is a distinct subtype of the Number primitive type. + the enum object is a value of an anonymous object type containing a set of properties, + all of the enum type, corresponding to the values declared for the enum type in the body of the declaration. + The enum object's type furthermore includes a numeric index signature with the signature '[x: number]: string'. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +enum Animal { + cat = 0xff0000, + dog = 0x00ff00, + pig = 0x0000ff, +} +Assert.isString(Animal[0xff0000]); +Assert.equal(Animal[0x00ff00], "dog"); +Assert.isNumber(Animal.pig); +enum TypeABC { + A, + B, + C, +} +var index = TypeABC.A; +Assert.equal(TypeABC.A, 0); +Assert.equal(TypeABC.B, index + 1); +Assert.equal(TypeABC.C, index + 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..78e6995c4dfbd61ee05174814d5f5bc51c798cb7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_2.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the numeric index signature reflects a "reverse mapping" that is automatically generated in every enum object. + the reverse mapping provides a convenient way to obtain the string representation of an enum value + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +enum Animal { + Cat = 0xff0000, + Dog = 0x00ff00, + Pig = 0x0000ff, +} +var dog = Animal.Dog; +Assert.isNumber(dog); +Assert.isString(Animal[dog]); +Assert.equal("Dog", Animal[dog]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..481799d7ca231fdc383e792ac962c0c056b9637b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_declarations/enum_declarations_3.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When an enum declaration includes a const modifier it is said to be a constant enum declaration. + The members of a constant enum declaration must all have constant values that can be computed at compile time. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const enum H_Date { + Monday = 'Mon', + Tuesday = 'Tues', + Wednesday = 'Wed', + Thursday = 'Thur', + Friday = 'Fri', + Saturday = 'Satur', + Sunday = 'Sun' +} +let h_date1 = H_Date.Monday; +Assert.equal(h_date1, 'Mon'); +let h_date2 = H_Date.Tuesday; +Assert.equal(h_date2, 'Tues'); +let h_date3 = H_Date.Wednesday; +Assert.equal(h_date3, 'Wed'); +let h_date4 = H_Date.Thursday; +Assert.equal(h_date4, 'Thur'); +let h_date5 = H_Date.Friday; +Assert.equal(h_date5, 'Fri'); +let h_date6 = H_Date.Saturday; +Assert.equal(h_date6, 'Satur'); +let h_date7 = H_Date.Sunday; +Assert.equal(h_date7, 'Sun'); +const enum H_Odd { + a = 1, + b = a + 2, + c = a + 4, + d = c + 2, + e = b * b, +} +Assert.equal(H_Odd.a, 1); +Assert.equal(H_Odd.b, 3); +Assert.equal(H_Odd.c, 5); +Assert.equal(H_Odd.d, 7); +Assert.equal(H_Odd.e, 9); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_members/enum_members_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_members/enum_members_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..3a272a6a11357bb588d9c5710b44988884190d94 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/enums/enum_members/enum_members_1.ts @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if the member declaration specifies no value, the member is considered a constant enum member. + If the member is the first member in the enum declaration, it is assigned the value zero. + Otherwise, it is assigned the value of the immediately preceding member plus one, and an error occurs if the immediately preceding member is not a constant enum member. + if the member declaration specifies a value that can be classified as a constant enum expression, + the member is considered a constant enum member.otherwise, the member is considered a computed enum member. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function toLength(str: string): number { + return str.length; +} +let k = 999; +enum ABCList { + A, + B, + C = "string".length, + D = 10, + E, + F = ~17, + G = 0x0f << 0x02, + H = 0xff & 0xaa, + I = E | F, + J = toLength(ABCList[11]), + K = k++, + L = k--, + M = 1 + 2, + N = 1 - 2, + O = 1 / 2, + P = 1 % 2, + Q = 1 >> 2, + R = 1 >>> 2, + S = 1 ^ 2, +} +Assert.equal(ABCList.A, 0); +Assert.equal(ABCList.B, 1); +Assert.equal(ABCList.C, 6); +Assert.equal(ABCList.D, 10); +Assert.equal(ABCList.E, 11); +Assert.equal(ABCList.F, -18); +Assert.equal(ABCList.G, 60); +Assert.equal(ABCList.H, 170); +Assert.equal(ABCList.I, -17); +Assert.equal(ABCList.J, 1); +Assert.equal(ABCList.K, 999); +Assert.equal(ABCList.L, 1000); +Assert.equal(ABCList.M, 3); +Assert.equal(ABCList.N, -1); +Assert.equal(ABCList.O, 0.5); +Assert.equal(ABCList.P, 1); +Assert.equal(ABCList.Q, 0); +Assert.equal(ABCList.R, 0); +Assert.equal(ABCList.S, 3); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..e5fa6f675cbc2e09bb11bfdddee10cb3e9f7499d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_1.ts @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An Async Function is a JavaScript Function, Parameterized Arrow Function, Method that has been prefixed with the async modifier. + An Async Function must provide a return type annotation that points to a compatible Promise type. + Return type inference can only be used if there is a globally defined, compatible Promise type. + options: + lib:es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let pp: Promise = Promise.resolve(1); +async function fetchTest1(): Promise { + return await pp; +} +fetchTest1().then((params) => { + Assert.equal(params, 1); +}); +async function fetchTest1NoReturnType() { + return await pp; +} +fetchTest1NoReturnType().then((params) => { + Assert.equal(params, 1); +}); +const fetchTest2 = async (): Promise => { + return await pp; +}; +fetchTest2().then((params) => { + Assert.equal(params, 1); +}); +const fetchTest2NoReturnType = async () => { + return await pp; +}; +fetchTest2NoReturnType().then((params) => { + Assert.equal(params, 1); +}); +class Person { + async fetchTest3(): Promise { + return await pp; + } + async fetchTest3NoReturnType() { + return await pp; + } +} +new Person().fetchTest3().then((params) => { + Assert.equal(params, 1); +}); +new Person().fetchTest3NoReturnType().then((params) => { + Assert.equal(params, 1); +}); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1ddc1ac3477f05d103a66b4911e71057180a0565 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_2.ts @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Async Generator Functions + options: + lib:es2018 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +async function* ag() { + let i = 0; + let c = 0; + yield* [0, 0, 0, 0]; + while (true) { + yield c++; + i++; + if (i > 20) { + break; + } + } +} +function newArray() { + let arr: number[] = [0, 0, 0, 0]; + let i = 0; + let c = 0; + while (true) { + arr[i + 4] = c; + c++; + i++; + if (i > 20) { + break; + } + } + return arr; +} +let arr = ag(); +async function showAsyncGenerator(arr: AsyncGenerator) { + let i = 0; + for await (let x of arr) { + Assert.equal(x as number, arr2[i++]); + } +} +let arr2 = newArray(); +showAsyncGenerator(arr); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..2d547a96f0926f5c40bd28aecdfe8bf394281cef --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_3.ts @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Async Iterators Functions + options: + lib:es2018 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function createAsyncInterator(arr: any[]): AsyncIterator { + let index = 0; + let len = arr.length; + return { + async next() { + return new Promise((resolve, reject) => { + if (index < len) { + resolve({ value: arr[index++], done: false }); + } else { + resolve({ value: undefined, done: true }); + } + }); + }, + }; +} +export function newArray(len: number, step: number = 1) { + if (len <= 0) { return [] }; + let arr: any[] = []; + let x: number = 0; + for (let i: number = 0; i < len; i++) { + arr[i] = x + step; + } + return arr; +} +let arr = newArray(15); +async function exp(arr: any[]) { + let asy = createAsyncInterator(arr); + let i = 0; + let fg; + while (true) { + if (fg == true) { + break; + } + await asy.next().then((v) => { + if (v.done == true) { + fg = true; + } + Assert.equal(v.value, arr[i++]); + return; + }); + } +} +exp(arr); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..d6a9232379b86e40084d2008f0b428318196d8f5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/asynchronous_functions/asynchronous_functions_4.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Async Iterators Functions + options: + lib:es2018 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import { newArray } from './asynchronous_functions_3.js'; + +async function asc(arr: any[]) { + let i = 0; + for await (let x of arr) { + Assert.equal(x, arr[i]); + i++ + } +} +let arr = newArray(15, 5); +asc(arr); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d3a47bc49de6548ff3e2c97de2d423863cb97b18 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_1.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a destructuring parameter declaration introduces zero or more named locals + and initializes them with values extracted from properties or elements of the object or array passed as an argument for the parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Person { + m_name: string; + m_age: number; + constructor(name: string, age: number) { + this.m_name = name; + this.m_age = age; + } +} +let tt: Person = new Person("caihua", 12); +function showInfo({ m_name, m_age }: Person) { + Assert.isString(m_name); + Assert.equal(m_name, tt.m_name); + Assert.isNumber(m_age); + Assert.equal(m_age, tt.m_age); +} +showInfo(tt); +let tt1: Person = new Person("caihua1", 121); +let tt2: Person = new Person("caihua2", 122); +let person_array: Person[] = [tt1, tt2]; +function showArrayInfo(v_array: Array) { + let [tt1, tt2] = v_array; + Assert.equal(tt1.m_name, v_array[0].m_name); + Assert.equal(tt1.m_age, v_array[0].m_age); + Assert.equal(tt2.m_name, v_array[1].m_name); + Assert.equal(tt2.m_age, v_array[1].m_age); +} +showArrayInfo(person_array); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..ab5bf3ddd49e409e15099f61cf79c5b41d43269a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_2.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the type T associated with a destructuring parameter declaration is determined as follows + If the declaration includes a type annotation, T is that type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Person { + m_name: string; + m_age: number; + constructor(name: string, age: number) { + this.m_name = name; + this.m_age = age; + } +} +let tt: Person = new Person("caihua", 12); +function showInfo(v: Person) { + let { m_name, m_age }: { m_name: string; m_age: number } = v; + Assert.isString(m_name); + Assert.equal(m_name, "caihua"); + Assert.isNumber(m_age); + Assert.equal(m_age, 12); +} +showInfo(tt); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..3c3293c70bf0768f533a6bd9caf299e693d0c61a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_3.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the type T associated with a destructuring parameter declaration is determined as follows + If the declaration occurs in a function expression for which a contextual signature is available, + T is the type obtained from the contextual signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Person { + m_name: string; + m_age: number; + constructor(name: string, age: number) { + this.m_name = name; + this.m_age = age; + } +} +let tt: Person = new Person("caihua", 12); +const showInfo: (v: Person) => void = function (v) { + let { m_name, m_age } = v; + Assert.isString(m_name); + Assert.equal(m_name, "caihua"); + Assert.isNumber(m_age); + Assert.equal(m_age, 12); +}; +showInfo(tt); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..2f9cefa6e835515ca32c9f18e691fbfb3fabcca6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_4.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the type T associated with a destructuring parameter declaration is determined as follows + if the declaration includes an initializer expression, T is the widened form of the type of the initializer expression. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type testStr = "caihua"; +type testStrNum = 12; +class Person { + m_name: testStr | undefined; + m_age: testStrNum | undefined; + constructor(); + constructor(name?: testStr, age?: testStrNum) { + if (name && typeof name === "string") { + this.m_name = name; + } else { + this.m_name = undefined; + } + if (age && typeof age === "number") { + this.m_age = age; + } else { + this.m_age = undefined; + } + } +} +let tt: Person = new Person(); +function showInfo(v: Person) { + let { + m_name = "caihua", + m_age = 12, + }: { m_name: testStr | undefined; m_age: testStrNum | undefined } = v; + Assert.equal(typeof m_name === "string", true); + Assert.equal(typeof m_age === "number", true); +} +showInfo(tt); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..cc30604e7e69aedc15651b9ff9ec33bd9c646a53 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_5.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the type T associated with a destructuring parameter declaration is determined as follows + if the declaration specifies a binding pattern, T is the implied type of that binding pattern + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type cc = { + name: string; + age: number; + isJob: boolean; +}; +function showInfo({ age, name, isJob }: cc) { + Assert.isNumber(age); + Assert.isBoolean(isJob); + Assert.isString(name); +} +showInfo({ + name: "caihua", + age: 90, + isJob: true, +}); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e5149f23e34989120a6f9009e6bdc3f20a86c9a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_6.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Destructuring parameter declarations do not permit type annotations on the individual binding patterns, + as such annotations would conflict with the already established meaning of colons in object literals. + Type annotations must instead be written on the top-level parameter declaration. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface information { + title?: string; + place?: [number, number]; + brave?: boolean; +} +function infor({ title, place, brave }: information) { + if (title) { + Assert.equal(typeof title == "string", true); + } + if (place) { + Assert.equal(Array.isArray(place), true); + } + if (brave) { + Assert.equal(typeof brave === "boolean", true); + } +} +infor({ title: "title" }); +infor({ brave: false }); +infor({ place: [0, 1] }); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..779bec1a95203590003aadc8eca9b9e4ee96ebce --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_7.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the type T associated with a destructuring parameter declaration is determined as follows + if the parameter is a rest parameter, T is any[]. + Otherwise,T is any + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function test(...v: any[]) { + Assert.isString(v[0]); + Assert.isNumber(v[1]); + Assert.isBoolean(v[2]); +} +test("caihua", 90, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_declarations/function_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_declarations/function_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4ea5a16f1992241891dc433e32bf7b46e7cc8d6e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_declarations/function_declarations_1.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Function declarations are extended to permit the function body to be omitted in overload declarations. + a function can have at most one implementation. + When a function has both overloads and an implementation, the overloads must precede the implementation + and all of the declarations must be consecutive with no intervening grammatical elements. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function add(a: number, b: number): number; +function add(a: string, b: number): string; +function add(a: string, b: string): string; +function add(arg1: string | number, arg2: string | number) { + if (typeof arg1 === "number" && typeof arg2 === "number") { + return arg1 + arg2; + } + + if (typeof arg1 === "string" || typeof arg2 === "string") { + return `${arg1}${arg2}`; + } +} +Assert.equal(add(0, 1), 1); +Assert.equal(add("0", 1), "01"); +Assert.equal(add("0", "1"), "01"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_declarations/function_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_declarations/function_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..686fa6ef767b2a5724b307e365c2f396aa2ff6ad --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_declarations/function_declarations_2.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + All declarations for the same function must specify the same set of modifiers (the same combination of declare, export, and default) + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +export function add1(a: string, b: string): string; +export function add1(a: number, b: number): number; +export function add1(a: string, b: number): string; +export function add1(arg1: string | number, arg2: string | number) { + if (typeof arg1 === "number" && typeof arg2 === "number") { + return arg1 + arg2; + } + if (typeof arg1 === "string" || typeof arg2 === "string") { + return `${arg1}${arg2}`; + } +} +Assert.equal(add1(0, 1), 1); +Assert.equal(add1("0", 1), "01"); +Assert.equal(add1("0", "1"), "01"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..cc139d21f1fa7a38f763766abb74f9f659033457 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_1.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If there are no return statements with expressions in f's function body, the inferred return type is Void. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function func(h_x: number) { + Assert.isNumber(h_x); +} +type voidTest = ReturnType; +let tt1: void = undefined; +let tt2: voidTest = undefined; +Assert.equal(tt1, tt2); +Assert.equal(tt1 === tt2, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3180469b8ee9eccdd6ffb36f0e8e2c015cffcead --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_2.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if f is a contextually typed function expression + the inferred return type is the union type of the types of the return statement expressions in the function body, + ignoring return statements with no expressions. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function f(x: number) { + switch (x) { + case 0: + return "hello"; + case 1: + return 1; + default: + return true; + } +} +type testType = ReturnType; +type ttStr = testType & string; +let tt1: ttStr = "hello"; +Assert.isString(tt1); +type ttBoo = testType & boolean; +let tt2: ttBoo = true; +Assert.isBoolean(tt2); +type ttNum = testType & number; +let tt3: ttNum = 1; +Assert.isNumber(tt3); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..bbe98cd076e612d0e9002944e076dbb2e777b6aa --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_3.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the inferred return type is the first of the types of the return statement expressions in the function body + that is a supertype of each of the others, ignoring return statements with no expressions. + A compile-time error occurs if no return statement expression has a type that is a supertype of each of the others. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function foo(x: string | number, y: string | boolean) { + if (typeof x === "string") { + return x.toUpperCase(); + } else if (typeof x === "number") { + return x.toString(); + } else if (typeof y === "string") { + return y.toUpperCase(); + } else { + return y; + } +} +type cc = ReturnType; +let dd: cc = "string"; +Assert.isString(dd); +dd = true; +Assert.isBoolean(dd); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..15c7e92fdaae3f846d1e9f0f52763a7a9aedfdd6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_4.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the signature of a function implementation, a parameter can be marked optional by following it with an initializer. + When a parameter declaration includes both a type annotation and an initializer, the initializer expression is contextually typed by the stated type + and must be assignable to the stated type, or otherwise a compile-time error occurs. + When a parameter declaration has no type annotation but includes an initializer, + the type of the parameter is the widened form (section 3.12) of the type of the initializer expression + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Test { + a: number; + constructor(a: number) { this.a = a; } +} +function f(x: number, y: Test = { a: 1 }, z = "hello") { + Assert.isString(z); + return { + x, + yy: y, + z: z, + }; +} +Assert.equal(f(0).yy.a, 1); +Assert.equal(f(0).z, "hello"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..1427e462acffeb3179f1eb9fdd2ac20cc5e99845 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_implementations/function_implementations_5.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Initializer expressions are evaluated in the scope of the function body but are not permitted to reference local variables + and are only permitted to access parameters that are declared to the left of the parameter they initialize, + unless the parameter reference occurs in a nested function expression. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let a = 3; +function f(n1: number = a, n2 = n1 * 2, n3 = n1 + 2) { + let b = 12; + function g(xx = b) { + return xx; + } + let c = g(); + return { n1, n2, n3, g: c }; +} +Assert.equal(f().n1, 3); +Assert.equal(f().n2, 6); +Assert.equal(f().n3, 5); +Assert.equal(f().g, 12); +Assert.equal(f(1).n1, 1); +Assert.equal(f(1).n2, 2); +Assert.equal(f(1).n3, 3); +Assert.equal(f(1).g, 12); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_overloads/function_overloads_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_overloads/function_overloads_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..734b89651c57f5c3cfeb2a3e8d8d3d0db157cfc6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/function_overloads/function_overloads_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The parameter list of a function overload cannot specify default values for parameters. + In other words, an overload may use only the ? form when specifying optional parameters. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function add(): number; +function add(x: number): number; +function add(x: number, y: number): number; +function add(x?: number, y?: number): number { + let sum: number = 0; + if (x) { + sum += x; + } + if (y) { + sum += y; + } + return sum; +} +Assert.equal(add(), 0); +Assert.equal(add(1), 1); +Assert.equal(add(1, 1), 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generator_functions/generator_functions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generator_functions/generator_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4601785d81f56c3fadd5a284c98a309ea9a5c7b9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generator_functions/generator_functions_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Generator Functions + options: + lib: es2015 + target: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function* generator(): IterableIterator { + yield 0; + yield 1; + yield 2; +} +const iterator = generator(); +let cc = iterator.next(); +Assert.equal(cc.value, 0); +Assert.equal(cc.done, false); +cc = iterator.next(); +Assert.equal(cc.value, 1); +Assert.equal(cc.done, false); +cc = iterator.next(); +Assert.equal(cc.value, 2); +Assert.equal(cc.done, false); +cc = iterator.next(); +Assert.equal(cc.value, undefined); +Assert.equal(cc.done, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generator_functions/generator_functions_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generator_functions/generator_functions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..237d92a46df00113afa1e0cad991efd017a080bd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generator_functions/generator_functions_2.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Generator Functions + options: + lib: es2015 + target: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function* g1(n: number, step: number = 1): Generator { + let i: number = 0, c: number = 0; + while (true) { + yield i += step; + c++; + if (c >= n) { + break; + } + } +} +function getGeneratorArray(gen: Generator, step: number = 1): any[] { + let c: number = 0; + let arr: any[] = []; + let i: number = 0; + while (true) { + let next = gen.next(); + if (next.done == true) { + break; + } + arr[i] = next.value; + i++; + Assert.equal(next.value, c += step); + } + return arr; +} +let genS1 = g1(10); +let arr: number[] = getGeneratorArray(genS1); +Assert.equal(JSON.stringify(arr), '[1,2,3,4,5,6,7,8,9,10]'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generic_functions/generic_functions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generic_functions/generic_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5a4a3a7ae19866e1883341ae2fb89f6795ca22d9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generic_functions/generic_functions_1.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type parameters declared in the signature of a function implementation are + in scope in the signature and body of that function implementation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type XY = { x: number; y: number }; +function f1(arg: T): T { + return arg; +} +Assert.equal(f1(0), 0); +Assert.equal(f1("hello"), "hello"); +Assert.equal(f1(true), true); +function f2(arg: T): T { + return arg; +} +Assert.equal(JSON.stringify(f2<{ x: number, y: number, z: number }>({ x: 0, y: 0, z: 0 })), '{"x":0,"y":0,"z":0}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generic_functions/generic_functions_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generic_functions/generic_functions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4fb699c4f70cbe36cf56768ea3ca2899845edcbd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/generic_functions/generic_functions_2.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type arguments of a call to a generic function may be explicitly specified in a call operation + or may, when possible, be inferred from the types of the regular arguments in the call. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function identity(arg: T): T { + return arg; +} +Assert.equal(identity(0), 0); +Assert.equal(identity(0), 0); +Assert.equal(identity("hello"), "hello"); +Assert.equal(identity("hello"), "hello"); +Assert.equal(identity(true), true); +Assert.equal(identity(true), true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/type_guard_functions/type_guard_functions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/type_guard_functions/type_guard_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..538450c5fb71c1357fc0993264c3f1c19556dea6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/functions/type_guard_functions/type_guard_functions_1.ts @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: TypeScript compiler narrows the type of a variable within a type guard. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Student { + name: string; + age: number; + constructor(name: string, age: number) { + this.name = name; + this.age = age; + } +} +class Teacher { + name: string; + age: number; + job: string; + constructor(name: string, age: number, job: string) { + this.name = name; + this.age = age; + this.job = job; + } +} +function isTeacher(obj: Student | Teacher): obj is Teacher { + return "job" in obj; +} +function printInfo(obj: Student | Teacher) { + if ( + isTeacher(obj) && + typeof obj.age === "number" && + obj instanceof Teacher + ) { + Assert.equal(obj.name, "caihuaTeacher"); + Assert.equal(obj.age, 20); + Assert.equal(obj.job, "teacher"); + return "teacher"; + } else { + Assert.equal(obj.name, "caihuaStudent"); + Assert.equal(obj.age, 20); + return "student"; + } +} +let tt: Teacher = new Teacher("caihuaTeacher", 20, "teacher"); +let ss: Student = new Student("caihuaStudent", 20); +Assert.equal(printInfo(tt), "teacher"); +Assert.notEqual(printInfo(tt), "student"); +Assert.equal(printInfo(ss), "student"); +Assert.notEqual(printInfo(ss), "teacher"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/classes/classes.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/classes/classes.ts new file mode 100644 index 0000000000000000000000000000000000000000..f72709bc4836f86b0e1c6e6caa599c5ef2bd5b8a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/classes/classes.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface PointInterface { + x: number; + y: number; + z: number; + getPointArray(): number[]; +} +class Point implements PointInterface { + x: number = 0; + y: number = 0; + z: number = 0; + getPointArray() { + return [this.x, this.y, this.z]; + } + constructor(x: number, y: number, z: number) { + this.x = x; + this.y = y; + this.z = z; + } +} +class ColorC extends Point { + static Red: number = 0; + static Green: number = 0; + static Blue: number = 0; + static getColorArray(): number[] { + return [this.Red, this.Green, this.Blue] + } + toJSON() { + return JSON.stringify(ColorC.getColorArray()); + } + constructor(Red: number, Green: number, Blue: number, x: number = 0, y: number = 0, z: number = 0) { + super(x, y, z); + ColorC.Red = Red; + ColorC.Green = Green; + ColorC.Blue = Blue; + } +} +let colorPoint = new ColorC(255, 0, 0, 1, 1, 1); +Assert.equal(JSON.stringify(colorPoint.getPointArray()), "[1,1,1]"); +Assert.equal(colorPoint.toJSON(), "[255,0,0]"); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/enums/enums.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/enums/enums.ts new file mode 100644 index 0000000000000000000000000000000000000000..18f047adf998e4912034b12d0561d3d23e49cdd3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/enums/enums.ts @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const enum CED { + None = -1, + False, + True = 1, + DEF = 1024, + Ver = "1.0.1", +} +Assert.equal(CED.None, -1); +Assert.equal(CED.False, 0); +Assert.equal(CED.True, 1); +Assert.equal(CED.DEF, 1024); +Assert.equal(CED["None"], -1); +Assert.equal(CED["False"], 0); +Assert.equal(CED["True"], 1); +Assert.equal(CED["DEF"], 1024); +Assert.equal(CED.Ver, "1.0.1"); + +function toLength(str: string): number { + return str.length; +} + +enum ABCList { + A, + B, + C = "string".length, + D = 10, + E, + F = ~17, + G = 0x0f << 0x02, + H = 0xff & 0xaa, + I = E | F, + J = toLength(ABCList[11]), +} +Assert.equal(ABCList.A, 0); +Assert.equal(ABCList.B, 1); +Assert.equal(ABCList.C, 6); +Assert.equal(ABCList.D, 10); +Assert.equal(ABCList.E, 11); +Assert.equal(ABCList.F, -18); +Assert.equal(ABCList.G, 60); +Assert.equal(ABCList.H, 170); +Assert.equal(ABCList.I, -17); +Assert.equal(ABCList.J, 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/expressions/expressions.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/expressions/expressions.ts new file mode 100644 index 0000000000000000000000000000000000000000..09327519a0c49b67ae7707ce21087e1a49bb5e44 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/expressions/expressions.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class EXP { + cname: string; + private _ver: number = -1; + addName(ad: string) { + this.cname = this.cname + " " + ad; + return this.cname; + } + get ver() { + return this._ver; + } + set ver(v: number) { + this._ver = v; + } + constructor(cname: string) { + this.cname = cname; + } +} +let exp = new EXP("EXP"); +exp.addName("Class"); +Assert.equal(exp.cname, "EXP Class"); + +exp.ver = 1; +Assert.equal(exp.ver, 1); + +function Ax2(a: number) { + return a * 2; +} +Assert.equal(Ax2(2), 4); + +let fun1: (a: number, b: number) => number = (a: number, b: number) => { + return a + b; +} +Assert.equal(fun1(1, 2), 3); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/functions/functions.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/functions/functions.ts new file mode 100644 index 0000000000000000000000000000000000000000..031c95ec63e0ba956ae89db22ae439ab61ba4f10 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/functions/functions.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function addXYZ(x: number, y: number, z: number): number { + return x + y + z; +} +Assert.equal(addXYZ(1, 2, 3), 6); + +function addXY(x: number, y: number): number; +function addXY(x: number, y: number): number { + return x + y; +} +Assert.equal(addXY(1, 2), 3); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/interfaces/interfaces.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/interfaces/interfaces.ts new file mode 100644 index 0000000000000000000000000000000000000000..34eebfd5378ea7353a01854acb60c37ea5cb8111 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/interfaces/interfaces.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface X { + x: number; +} +interface Y { + y: number; +} +interface Z { + z: number; +} +interface PointXYZ extends X, Y, Z { + toJSON(): string; +} +let pa: PointXYZ = { + x: 3, + y: 4, + z: 5, + toJSON() { + let pArr = [this.x, this.y, this.z]; + return JSON.stringify(pArr); + }, +} +Assert.equal(pa.toJSON(), "[3,4,5]"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/namespaces/namespaces.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/namespaces/namespaces.ts new file mode 100644 index 0000000000000000000000000000000000000000..f272a45cb4888ee0e390960c7022760d2062c475 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/namespaces/namespaces.ts @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace ExampleG { + var namespace_name = "Example"; + export function exampleName(str: string): string { + return str + " " + namespace_name; + } + class Point { + x: number = 0; + y: number = 0; + } + interface PointXYZ { + x: number; + y: number; + z: number; + } + type StrNumBool = string | number | boolean; + export enum Color { + RED = 0xFF0000, + GREEN = 0x00FF00, + BLUE = 0x0000FF, + }; + namespace SubNamespace { } + declare var __TEST__: boolean; + export import EE = ExportExampleG; +} +namespace ExportExampleG { + export var namespace_name = "ExportExampleG"; + export function exampleEName(str: string): string { + return str + " " + namespace_name; + } + export class Point { + x: number = 0; + y: number = 0; + } + export interface PointXYZ { + x: number; + y: number; + z: number; + } + export type StrNumBool = string | number | boolean; + export enum Color { + RED = 0xFF0000, + GREEN = 0x00FF00, + BLUE = 0x0000FF, + } + export namespace SubNamespace { } + export declare var __TEST__: boolean; + export import E = ExampleG; +} +Assert.equal(ExampleG.exampleName("G"), "G Example"); +Assert.equal(ExampleG.Color.RED, 0xFF0000); +Assert.equal(ExportExampleG.exampleEName("G"), "G ExportExampleG"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/scripts_and_modules.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/scripts_and_modules.ts new file mode 100644 index 0000000000000000000000000000000000000000..f719c24c2ea9e9a7c29ee341678ea243ce4a01ba --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/scripts_and_modules.ts @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import * as sf1 from "./source_file_1.js"; +import checkGenerator from "./source_file_2.js"; +import arr1 from "./source_file_3.js"; +import PointXY from "./source_file_4.js"; +import { Assert } from "../../../../suite/assert.js"; + +Assert.equal(sf1.num, 1408); + +Assert.equal(sf1.show(), "show"); + +let a3: Generator = sf1.arr3(9); +checkGenerator(a3, 3); + +let green = new sf1.Color(0, 255, 0); +Assert.equal(JSON.stringify(green.ColorData), '[0,255,0]'); + +let p: sf1.Point = { x: 0, y: 0 }; +Assert.equal(p.x, 0); +Assert.equal(p.y, 0); + +let arrstrnum: sf1.ArrStrNum; +arrstrnum = ["a", "b", "c", "d", "e"]; +Assert.equal(JSON.stringify(arrstrnum), '["a","b","c","d","e"]'); +arrstrnum = [1, 2, 3, 4, 5]; +Assert.equal(JSON.stringify(arrstrnum), '[1,2,3,4,5]'); +arrstrnum = ["a", 2, "c"]; +Assert.equal(JSON.stringify(arrstrnum), '["a",2,"c"]'); +arrstrnum = "arrstrnum"; +Assert.isString(arrstrnum); +arrstrnum = 0; +Assert.isNumber(arrstrnum); + +Assert.equal(sf1.XXXX.x, 1024); +Assert.equal(sf1.XXXX.showXXXX(), "showXXXX"); + +let a1 = arr1(9); +checkGenerator(a1, 1); + +let p1 = new PointXY(10, 10); +Assert.equal(JSON.stringify(p1.point), '[10,10]'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a19235627cd807eda2fd706c3736ac37d68a3fe3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_1.ts @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export let num: number = 1408; +export function show() { return "show"; }; +export function* arr3(n: number): Generator { + let i = 0; + let c = 0; + if (n > 0) { + while (true) { + yield i += 3; + if (c >= n) { + break; + } else { + c++; + } + } + } +} +export class Color { + Red: number; + Green: number; + Blue: number; + ColorData: [number, number, number] = [0, 0, 0]; + getColor(): [number, number, number] { + this.ColorData = [this.Red, this.Green, this.Blue]; + return this.ColorData; + } + constructor(red: number, green: number, blue: number) { + this.Red = red; + this.Green = green; + this.Blue = blue; + this.getColor(); + } +} +export interface Point { + x: number; + y: number; +} +export type ArrStrNum = string[] | number[] | (string | number)[] | string | number; +export enum ABC { + A, B, C, D, E, +} +export namespace XXXX { + export let x: number = 1024; + export function showXXXX() { return "showXXXX"; }; +} +export declare let dnum: number; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..624f3d3f5ae801099b387052a81c7312ad1943bc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_2.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import { Assert } from "../../../../suite/assert.js"; +export default function checkGenerator(gen: Generator, step: number) { + let c = 0; + while (true) { + let next = gen.next(); + if (next.done == true) { + break; + } + Assert.equal(next.value, c += step); + } +} \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..bc333e40b1fabc91400302f925c9fc88fa1aae76 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export default function* arr1(n: number): Generator { + let i = 0; + let c = 0; + if (n > 0) { + while (true) { + yield i += 1; + if (c >= n) { + break; + } else { + c++; + } + } + } +} \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..67fbe4f5888c51f4c6c568ba0229ff556d885a5a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/scripts_and_modules/source_file_4.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export default class PointXY { + x: number; y: number; + point: [number, number] = [0, 0]; + getPoint(): [number, number] { + this.point = [this.x, this.y]; + return this.point; + } + constructor(x: number, y: number) { + this.x = x; + this.y = y; + this.getPoint(); + } +} \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/statements/statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/statements/statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..239165c97418614cf1a5c1f11687092cdfc502ea --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/statements/statements.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Skill { + SkillName: string; + Damage: number; + Data: string; +} +let aoe: Skill = { SkillName: "AoE", Damage: 495, Data: "AreaFire" }; +Assert.equal(aoe.SkillName, "AoE"); +Assert.equal(aoe.Damage, 495); +Assert.equal(aoe.Data, "AreaFire"); + +type NumStr = number | string; +let ns: NumStr; +ns = 1024; +Assert.isNumber(ns); +ns = "A"; +Assert.isString(ns); + +enum Color { + Red = 0xFF0000, + Green = 0x00FF00, + Bule = 0x0000FF, +} +Assert.equal(Color.Green, 0x00FF00); + +let aaa; +aaa = true; +Assert.isBoolean(aaa); +aaa = 1408; +Assert.isNumber(aaa); +let abc: string; +abc = "ABC"; +Assert.isString(abc); + +type TF = -1 | 0 | 1; +let tf0: TF = 0; +let tf1: TF = 1; +let tf2: TF = -1; +Assert.equal(tf0, 0); +Assert.equal(tf1, 1); +Assert.equal(tf2, -1); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/types/types.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/types/types.ts new file mode 100644 index 0000000000000000000000000000000000000000..a7f8823b07c54f3d8b27a636659bdc720d441bf9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/grammar/types/types.ts @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) + and this appendix lists only productions that are new or modified from the ECMAScript grammar. + options: + target: es2015 + lib: es2015 + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let num: number = 1024; +let str: string = "string"; +let bool: boolean = false; +let big: bigint = 1n; +let sym: symbol = Symbol(); +let obj: object = {}; +let fun: Function = () => { return "Function"; } +let udf: undefined = undefined; +Assert.isNumber(num); +Assert.isString(str); +Assert.isBoolean(bool); +Assert.equal(typeof big, "bigint"); +Assert.isSymbol(sym); +Assert.equal(typeof obj, "object"); +Assert.isFunction(fun); +Assert.isUndefined(udf); + +type Ver = { ver: string }; +interface Data extends Ver { + data: number; +} +let v: Data = { data: 1024, ver: "1.0.1" }; +Assert.equal(v.data, 1024); +Assert.equal(v.ver, "1.0.1"); + +function addNumbers(...num: number[]): number { + let n: number = 0; + for (let i: number = 0; i < num.length; i++) { + n += num[i]; + } + return n; +} +Assert.equal(addNumbers(5, 5, 5, 5, 5), 25); + +interface StrIndex { + [key: string]: number; +} +let si: StrIndex = { C: 3, D: 4 }; +si['A'] = 1; +si.B = 2; +Assert.equal(si.A, 1); +Assert.equal(si['B'], 2); +Assert.equal(si.C, 3); +Assert.equal(si['D'], 4); +Assert.equal(JSON.stringify(si), '{"C":3,"D":4,"A":1,"B":2}'); + +interface NumIndex { + [key: number]: string; +} +let ni: NumIndex = { 1: 'A' }; +ni[3] = 'C'; +Assert.equal(ni[1], 'A'); +Assert.equal(ni[3], 'C'); +Assert.equal(JSON.stringify(ni), '{"1":"A","3":"C"}'); + +class AB{ + a: T | undefined; + b: U | undefined; + public c: string = ""; + private d: string = ""; + protected e: string = ""; + setD(val: string) { + this.d = val; + } + setE(val: string) { + this.e = val; + } + show() { + return JSON.stringify({ d: this.d, e: this.e }); + } + constructor(a?: T, b?: U) { + this.a = a; + this.b = b; + } +} +let ab = new AB(123, "ABC"); +Assert.equal(ab.a, 123); +Assert.equal(ab.b, "ABC"); + +let ab2 = new AB(1024); +Assert.equal(ab2.a, 1024); +Assert.isUndefined(ab2.b); + +ab.c = "1024"; +Assert.equal(ab.c, "1024"); + +ab.setD("D"); +ab.setE("E"); +Assert.equal(ab.show(), "{\"d\":\"D\",\"e\":\"E\"}"); + +type ONS = { n1?: number | string, n2?: number | string }; +let ons: ONS = { n1: 0, n2: "ABC" }; +Assert.equal(ons.n1, 0); +Assert.equal(ons.n2, "ABC"); + + +type NumStrBool = number | string | boolean; +type NumObjBool = number | object | boolean; +type NumBool = NumObjBool & NumStrBool; +let nb: NumBool = 1; +Assert.isNumber(nb); +nb = true; +Assert.isBoolean(nb); + +type StrFun = (a: string, b: string) => string; +let strFun: StrFun = (a: string, b: string) => { return a + b; } +Assert.equal(strFun("AB", "CD"), "ABCD"); + +let a: any; +let vo: void; + +let arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +Assert.equal(arr[0], 0); + +type type0 = "A" | "B" | "C" | "D"; +let t0: type0 = "A"; +Assert.equal(t0, "A"); + +type NumStrArray = number[] | string[] | (number | string)[]; +let nsa1: NumStrArray = [1, 3, 5]; +let ar1 = [1, 3, 5] +let nsa2: NumStrArray = ["A", "B", "C"]; +let ar2 = ["A", "B", "C"]; +let nsa3: NumStrArray = ["A", 1, "B", 2, "C", 3]; +let ar3 = ["A", 1, "B", 2, "C", 3]; +Assert.equal(JSON.stringify(nsa1), JSON.stringify(ar1)); +Assert.equal(JSON.stringify(nsa2), JSON.stringify(ar2)); +Assert.equal(JSON.stringify(nsa3), JSON.stringify(ar3)); + +type ABC = [number, string, boolean]; +let abc: ABC = [1, "A", true]; +let abc2 = [1, "A", true]; +Assert.equal(JSON.stringify(abc), JSON.stringify(abc2)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..fa6621e1edb593eeb296b833fba248bdcbf223e3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_1.ts @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An interface declaration declares an interface type. + An InterfaceDeclaration introduces a named type (section 3.7) in the containing declaration space. + The BindingIdentifier of an interface declaration may not be one of the predefined type names (section 3.8.1). + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +{ + interface Worker { + x: number; + + work(): string; + + getS(): { rate: number,velocity: number; }; + } + + interface Tool { + y: number; + + tool(): string; + + getS(): { rate: number, velocity: number;}; + } + + interface Runer extends Tool,Worker{ + z:string, + + getS(): { rate: number, velocity: number,play:string}; + + run():string; + + tool(): string; + } + + let result1: { rate: number,velocity: number; } = { + rate: 1, + velocity:123, + }; + let result2: { velocity: number,rate: number,} = { + velocity: 1, + rate:999, + }; + let ToRun: {velocity: number, rate: number, play: string} = { + velocity: 1314, + rate:999, + play:"一万年", + } + + let point: Worker = { + x: 1, + getS(): {rate: number, velocity: number} { + return result2 + }, + work(): string { + return "work"; + } + }; + let pointer: Tool = { + y: 1, + getS(): {rate: number, velocity: number} { + return result1 + }, + tool(): string { + return "tool"; + }, + }; + let run:Runer = { + x: ++point.x, + y: --pointer.y, + z:"with z", + getS(): { rate: number; velocity: number; play: string } { + return ToRun; + }, + work(): string { + return "let it work"; + }, + run():string { + return "let it run"; + }, + tool(): string { + return "He is the tool of work"; + } + } + + Assert.equal(run.x,2); + Assert.equal(run.y,0); + Assert.equal(run.z,"with z"); + Assert.equal(run.getS(),ToRun); + Assert.equal(run.run()+","+run.work()+","+run.tool(),"let it run,let it work,He is the tool of work"); + Assert.equal(point.x, 2); + Assert.equal(pointer.y, 0); + Assert.equal(point.getS(), result2); + Assert.equal(pointer.getS(), result1); + Assert.equal(pointer.tool(), "tool"); + Assert.equal(point.work(), "work"); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4040342f7f9c0ed59c6daa6f6bdd081cd9685113 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_2.ts @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An interface can inherit from zero or more base types which are specified in the InterfaceExtendsClause. + The base types must be type references to class or interface types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +{ + interface Worker { + x: number; + + work(): void; + + getS(): { velocity: number; }; + } + + interface Tool { + y: number; + + tool(): void; + + getS(): { rate: number; }; + } + + interface WorkerTool extends Worker, Tool { + z: string; + + getS(): { velocity: number; rate: number; }; + } + + let result: { velocity: number; rate: number; } = { + velocity: 1, + rate: 1 + }; + + let point: WorkerTool = { + x: 1 , + y: 1, + z: "zzz", + getS(): { velocity: number; rate: number; } { + return result + }, + tool(): string { + return "tool"; + }, + work(): string { + return "work"; + } + }; + + Assert.equal(point.x, 1); + Assert.equal(point.y, 1); + Assert.equal(point.z, "zzz"); + Assert.equal(point.getS(), result); + Assert.equal(point.tool(), "tool"); + Assert.equal(point.work(), "work"); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..def71989e3b297e160fdb46be7376e18a192c341 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/1_interface_declarations/interface_declarations_3.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Since function and constructor types are just object types containing call and construct signatures, interfaces can + be used to declare named function and constructor types + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +{ + interface Example1 { + (h_a: string, h_b: string): number; + } + + let sc: Example1 = (h_a: string, h_b: string): number => { return h_a.charCodeAt(0) + h_b.charCodeAt(0); }; + Assert.equal(sc('a', 'b'), 195); +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/2_declaration_merging/declaration_merging_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/2_declaration_merging/declaration_merging_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d1a838681957f9b80792c7afce863c13eeffb4e5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/2_declaration_merging/declaration_merging_1.ts @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When a generic interface has multiple declarations, all declarations must have identical type parameter lists, + i.e. identical type parameter names with identical constraints in identical order. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +{ + interface Ds { + createE1(a: T): number; + } + + interface Ds { + createE2(a: T): number; + + createE3(a: T): number; + } + + let name: Ds = { + createE1(a: string): number { + return 0 + }, + createE2(a: boolean): number { + return 0 + }, + createE3(a: number): number { + return 0 + } + }; + let src :Ds = { + createE1(a: number): number { + return 1 + }, + createE2(a: number | string): number { + return 2; + }, + createE3(a: number | string): number { + return 3; + }, + } + + class Get { + static getName(name: Ds): Ds { + return name; + }; + static getSrc(src :Ds){ + return src; + } + } + + Assert.equal(Get.getSrc(src).createE1(1),1); + Assert.equal(Get.getSrc(src).createE2(""),2); + Assert.equal(Get.getName(src).createE3("233"), 3); + Assert.equal(Get.getName(name).createE1(''), 0); + Assert.equal(Get.getName(name).createE3(0), 0); + Assert.equal(Get.getName(name).createE2(true), 0); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/2_declaration_merging/declaration_merging_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/2_declaration_merging/declaration_merging_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..90d5543f22963ed5aea4c36360dc8017bd5b4314 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/2_declaration_merging/declaration_merging_2.ts @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + multiple declarations is equivalent to the following single declaration. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +{ + interface D1 { + createE1(a: string): number; + } + + interface D1 { + createE2(a: boolean): number; + + createE3(a: number): number; + } + + interface DAssert { + createE4(a: string): number; + + createE5(a: boolean): number; + + createE6(a: number): number; + } + + let name: D1 & DAssert = { + createE1(a: string): number { + return 0 + }, + createE2(a: boolean): number { + return 0 + }, + createE3(a: number): number { + return 0 + }, + createE4(a: string): number { + return 0 + }, + createE5(a: boolean): number { + return 0 + }, + createE6(a: number): number { + return 0 + } + }; + + class Class { + static getName(name: D1 & DAssert): D1 & DAssert { + return name; + } + } + + Assert.equal(Class.getName(name).createE1(''), 0); + Assert.equal(Class.getName(name).createE3(0), 0); + Assert.equal(Class.getName(name).createE2(true), 0); + Assert.equal(Class.getName(name).createE4(''), 0); + Assert.equal(Class.getName(name).createE5(true), 0); + Assert.equal(Class.getName(name).createE6(0), 0); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..12887890b392fd880e12fc5845982e23eec4deb3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_1.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When an interface type extends a class type it inherits the members of the class but not their implementations. + It is as if the interface had declared all of the members of the class without providing an implementation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +{ + class h_C { + private state: any = 0; + point: number | unknown = 0; + } + + interface h_SC extends h_C { + choose(): number; + } + + class h_B extends h_C implements h_SC { + choose(): number { + return 0 + } + } + + Assert.equal(new h_B().point,0); + Assert.equal(typeof new h_B().point,"number"); + Assert.isTrue(new h_B().hasOwnProperty('point')); + Assert.isTrue(new h_B().hasOwnProperty('state')); + +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..584bb73ebeafa0e500c9309d075f140aa2710195 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_2.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Interfaces inherit even the private and protected members of a base class. + When a class containing private or protected members is the base type of an interface type, + that interface type can only be implemented by that class or a descendant class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class h_C { + private h_pri: string; + constructor(h_pri: string) { + this.h_pri = h_pri; + } + get() { + return this.h_pri; + } + set() { + this.h_pri = this.h_pri; + } +} +interface h_I extends h_C { + choose(): string; +} +class h_c extends h_C implements h_I { + choose(): string { + return 'choose'; + } +} +let h_CC = new h_C('private'); +Assert.equal(h_CC.get(), 'private'); +let h_cc = new h_c('PRIVATE'); +Assert.equal(h_cc.get(), 'PRIVATE'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/4_dynamic_type_checks/dynamic_type_checks_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/4_dynamic_type_checks/dynamic_type_checks_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..58eccbd620f71f6cf445d1ece620cafb987d41a7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/interfaces/4_dynamic_type_checks/dynamic_type_checks_1.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript does not provide a direct mechanism for dynamically testing whether an object implements a particular + interface. Instead, TypeScript code can use the JavaScript technique of checking whether an appropriate set of members + are present on the object + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + + +interface WorkerTool { + shift(): void; + + rock(): void; + + getS(): void; +} + +function asWorkerTool(o: any): WorkerTool { + return o && o.shift && o.rock && o.getS ? o : null; +} + +let point: WorkerTool = { + getS(): void { + }, + rock(): void { + }, + shift(): void { + } +}; + +Assert.equal(asWorkerTool(point), point); +Assert.equal(typeof asWorkerTool(point),"object") \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1b184061c813247ae784c7d15e7899f6b9d633ef --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_1.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +namespace outside { + var native = 1; + export var h_a = native; + export namespace interior { + export var h_x = 10; + } + export interface OA { + OA: string; + } +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..249f43f4be89ef9297726f7bf4bfde1f02890086 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_2.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +namespace outside { + var native = 2; + export var h_b = native; + export namespace interior { + export var h_y = 20; + } + export interface OB { + OB: number; + } +}; diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..40f5e047463aba4df6ad76bbd2769416205833b8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_1_3.ts @@ -0,0 +1,30 @@ +/// +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + namespaces are "open-ended" and namespace declarations with the same qualified name relative to a common root (as defined in section 2.3) contribute to a single namespace. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var o1: outside.OA = { OA: "OA" }; +var o2: outside.OB = { OB: 1024 }; +Assert.isString(o1.OA); +Assert.isNumber(o2.OB); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d75e35c37e25960b1c6118e4267ff3bc51758811 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/declaration_merging/declaration_merging_2.ts @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + declaration merging also extends to namespace declarations with the same qualified name relative to a common root as a function, class, or enum declaration. + when merging a non-ambient function or class declaration and a non-ambient namespace declaration, the function or class declaration must be located prior to the namespace declaration in the same source file. + this ensures that the shared object instance is created as a function object. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Point { + x: number; + y: number; +} +function Point(x: number, y: number): Point { + return { x: x, y: y }; +} +namespace Point { + export var origin = Point(0, 0); + export function equals(p1: Point, p2: Point) { + return p1.x == p2.x && p1.y == p2.y; + } +} +var p1 = Point(0, 0); +var p2 = Point.origin; +var b = Point.equals(p1, p2); +Assert.equal(p1.x, 0); +Assert.equal(p1.y, 0); +Assert.equal(p2.x, 0); +Assert.equal(p2.y, 0); +Assert.equal(b, true); + +enum Color { + Red = 0xFF0000, + Green = 0x00FF00, + Blue = 0x0000FF, +} +namespace Color { + export interface Color { + Red: number; + Green: number; + Blue: number; + } +} +Assert.equal(Color.Red, 0xFF0000); +var color: Color.Color = { Red: 255, Green: 255, Blue: 255 }; +Assert.equal(color.Green, 255); + +class PointXYZ { + public x: number; + public y: number; + public z: number; + constructor(x: number = 0, y: number = 0, z: number = 0) { + this.x = x; + this.y = y; + this.z = z; + } + toString() { + return "( " + this.x + " , " + this.y + " , " + this.z + " )"; + } +} +namespace PointXYZ { + export var xyz = new PointXYZ(1, 1, 1); + export interface PointXYZ { + x: number; + y: number; + z: number; + } +} +var xyz1 = new PointXYZ(1, 3, 5); +var xyz2: PointXYZ.PointXYZ = { x: 2, y: 3, z: 4 }; +var xyz3 = PointXYZ.xyz; +Assert.equal(xyz1.toString(), "( 1 , 3 , 5 )"); +Assert.equal(xyz2.x, 2); +Assert.equal(xyz3.toString(), "( 1 , 1 , 1 )"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f240e609dedb78e1484673d5a793620117a9410 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_1.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an export declaration declares an externally accessible namespace member. An export declaration is simply a regular declaration prefixed with the keyword export. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace ED1 { + export interface Weapon { WName: string, Damage: number } + export var def = 1024; +} +var weapon1: ED1.Weapon = { WName: "40mmAT", Damage: 40 }; +var def1 = ED1.def; +Assert.equal(weapon1.WName, "40mmAT"); +Assert.equal(weapon1.Damage, 40); +Assert.equal(def1, 1024); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1ad82d5e7da6b96a0491d786689adfb8d218b17c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_2.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the members of a namespace's export declaration space constitute the namespace's export member set. + a namespace's instance type is an object type with a property for each member in the namespace's export member set that denotes a value. + module: ESNext + isCurrent: true + ---*/ + +import { Assert } from '../../../../suite/assert.js' + + +namespace ED2 { + export var def = 1024; + interface Weapon { WName: string, Damage: number } + export var weapon1: Weapon = { WName: "Lightting", Damage: 200 } + export var tf = "False"; +} +var ed2 = ED2; +var ed2_1 = ed2.def; +var ed2_2 = ed2.weapon1; +var ed2_3 = ed2.tf; +Assert.equal(typeof ed2, "object"); +Assert.isNumber(ed2_1); +Assert.equal(typeof ed2_2, "object"); +Assert.isString(ed2_3); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..bea69bcc1b7667bcb3264631da321de38124c6d6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/export_declarations/export_declarations_3.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an exported member depends on a (possibly empty) set of named types. Those named types must be at least as accessible as the exported member, or otherwise an error occurs. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface WeaponDamage { DamageType: string, Damage: number } +namespace ED3 { + export interface WeaponData { WName: string, ROT: number, Range: number } + export interface Weapon { WeaponDamage: WeaponDamage, WeaponData: WeaponData } + export function getWeapon(Weapon: Weapon) { + return "WeaponName = " + Weapon.WeaponData.WName + "\n" + "DamageType = " + Weapon.WeaponDamage.DamageType + "\n" + "Damage = " + Weapon.WeaponDamage.Damage + "\n" + "ROT = " + Weapon.WeaponData.ROT + "\n" + "Range = " + Weapon.WeaponData.Range; + } +} +var weapon_damage: WeaponDamage = { DamageType: "EXPLODE", Damage: 1024 }; +var weapon_data: ED3.WeaponData = { WName: "AntiTankMissiles", ROT: 75, Range: 16 }; +var weapon: ED3.Weapon = { WeaponData: weapon_data, WeaponDamage: weapon_damage }; +var weaponStr = ED3.getWeapon(weapon); +Assert.isString(weaponStr); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/import_alias_declarations/import_alias_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/import_alias_declarations/import_alias_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..41e5d6172ec9591c314b567249d2de596bd191d8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/import_alias_declarations/import_alias_declarations_1.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + import alias declarations are used to create local aliases for entities in other namespaces. + an EntityName consisting of a single identifier is resolved as a NamespaceName and is thus required to reference a namespace. The resulting local alias references the given namespace and is itself classified as a namespace. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace hB { + interface hA { hn: number } + export import hY = hA; + import hZ = hA.hX; + export var vb: hZ = { hs: "v" }; +} +namespace hA { + export interface hX { hs: string } + export import va = hB.vb; +} +var v1: hB.hY.hX = hB.vb; +var v2 = hA.va; +let iadFlag: boolean = false; +if (v1 == v2) { + iadFlag = true; +} +Assert.isTrue(iadFlag); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/import_alias_declarations/import_alias_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/import_alias_declarations/import_alias_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..dc177e8eaa42518187bed1558612664f431807dc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/import_alias_declarations/import_alias_declarations_3.ts @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when an import statement includes an export modifier, all meanings of the local alias are exported. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../../suite/assert.js"; + +namespace IAD1 { + export var lv: number = 5; + export interface GGI { + UIName: string; + Cost: number; + } +} +namespace IAD2 { + import I1 = IAD1; + import I2 = IAD1.GGI; + export var i2 = I1.lv * 2; + export interface GI extends I2 { + Strength: number; + } +} +var i1 = IAD2.i2; +var gi: IAD2.GI = { UIName: "GI", Cost: 200, Strength: 100 }; +Assert.equal(i1, 10); +Assert.equal(gi.UIName, "GI"); +Assert.equal(gi.Cost, 200); +Assert.equal(gi.Strength, 100); + +namespace IAD3 { + export var i = 0; + interface U { + times: number; + } + var u1: U = { times: 5 }; + export function ux5() { + return u1.times * 5; + } +} +export namespace IAD4 { + export import I3 = IAD3; +} +Assert.equal(IAD4.I3.i, 0); +Assert.equal(IAD4.I3.ux5(), 25); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_body/namespace_body.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_body/namespace_body.ts new file mode 100644 index 0000000000000000000000000000000000000000..34ad1291b87f9e17ecf48448e8acbe03cce0a9d4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_body/namespace_body.ts @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the body of a namespace corresponds to a function that is executed once to initialize the namespace instance. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace Example { + var namespace_name = "Example"; + export function exampleName(str: string): string { + return str + " " + namespace_name; + } + class Point { + x: number = 0; + y: number = 0; + } + interface PointXYZ { + x: number; + y: number; + z: number; + } + type StrNumBool = string | number | boolean; + export enum Color { + RED = 0xFF0000, + GREEN = 0x00FF00, + BLUE = 0x0000FF, + } + namespace SubNamespace { } + declare var __TEST__: boolean; + import EE = ExportExample; +} +namespace ExportExample { + export var namespace_name = "ExportExample"; + export function exampleEName(str: string): string { + return str + " " + namespace_name; + } + export class Point { + x: number = 0; + y: number = 0; + } + export interface PointXYZ { + x: number; + y: number; + z: number; + } + export type StrNumBool = string | number | boolean; + export enum Color { + RED = 0xFF0000, + GREEN = 0x00FF00, + BLUE = 0x0000FF, + } + export namespace SubNamespace { } + export declare var __TEST__: boolean; + export import E = Example; +} +Assert.equal(Example.exampleName("G"), "G Example"); +Assert.equal(Example.Color.RED, 0xFF0000); +Assert.equal(ExportExample.exampleEName("G"), "G ExportExample"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..47588be5e2a29f5620e43a8e0de68f01471316ad --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + namespaces are declared using the namespace keyword, + but for backward compatibility of earlier versions of TypeScript a module keyword can also be used. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace A { + export interface TA { + TName: string; + Ver: number; + } +} +var varA: A.TA = { TName: "TA", Ver: 1.5 } +Assert.equal(varA.TName, "TA"); +Assert.equal(varA.Ver, 1.5); + +module B { + export interface TB { + SkillName: string; + Damage: number; + } +} +var varB: B.TB = { SkillName: "OverKill", Damage: 1024 }; +Assert.equal(varB.SkillName, "OverKill"); +Assert.equal(varB.Damage, 1024); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..eef814ca134aa6c0eac6481aea09b008872b1af7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_2.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + namespaces are either instantiated or non-instantiated. A non-instantiated namespace is a namespace containing only interface types, type aliases, and other non-instantiated namespace. + an instantiated namespace is a namespace that doesn't meet this definition. In intuitive terms, an instantiated namespace is one for which a namespace instance is created, whereas a non-instantiated namespace is one for which no code is generated. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace NonInstantiated { + export interface A { + TName: string; + Ver: number; + } + export type TF = boolean | number; + export namespace NI { + export interface B { + SkillName: string; + Damage: number; + } + export type StrNum = string | number; + } +} +var ni1: NonInstantiated.A = { TName: "Non", Ver: 1.0 }; +var ni2: NonInstantiated.NI.StrNum = "ni2"; +Assert.equal(ni1.Ver, 1.0); +Assert.equal(ni2, "ni2"); + +namespace Instantiated { + export function returnName() { + return "Instantiated"; + } + export var nsname: string = "Instantiated"; +} +Assert.equal(Instantiated.returnName(), "Instantiated"); +Assert.equal(Instantiated.nsname, "Instantiated"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..ba7781ad29f12e9e4b676ef5c24981388ec33b41 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_3.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when a namespace identifier is referenced as a NamespaceName it denotes a container of namespace and type names, and when a namespace identifier is referenced as a PrimaryExpression it denotes the singleton namespace instance. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace A { + export interface TypeA { + AName: string; + Lv: number; + UID: number; + } + export var player: TypeA = { AName: "Player", Lv: 5, UID: 0x0000A0F0 }; + export var playerRandom: TypeA = { AName: "playerRandom", Lv: getRandomNumber(99), UID: getRandomNumber(0xFFFFFFFF) }; + function getRandomNumber(x: number): number { + return Math.floor(Math.random() * x); + } +} +var playerA = A.player; +var playerB = A.playerRandom; +var an = A; +var playerC: A.TypeA = { AName: "PlayerC", Lv: 95, UID: 6250 }; +var playerD = an.player; +Assert.equal(playerA.AName, "Player"); +Assert.equal(playerA.Lv, 5); +Assert.equal(playerA.UID, 41200); +Assert.equal(playerB.AName, "playerRandom"); +Assert.isNumber(playerB.Lv); +Assert.isNumber(playerB.UID); +Assert.equal(playerC.AName, "PlayerC"); +Assert.equal(playerC.Lv, 95); +Assert.equal(playerC.UID, 6250); +Assert.equal(playerD.AName, "Player"); +Assert.equal(playerD.Lv, 5); +Assert.equal(playerD.UID, 41200); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..c2c09e42be15534a3ef0f13ac2e4fad0d37cdad5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/namespaces/namespace_declarations/namespace_declarations_4.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a namespace declaration that specifies an IdentifierPath with more than one identifier is equivalent to a series of nested single-identifier namespace declarations where all but the outermost are automatically exported. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace h_A.h_B.h_C { + export var h_x = "This is equivalent to the code below."; +} +Assert.equal(h_A.h_B.h_C.h_x, "This is equivalent to the code below."); +namespace h_A { + export namespace h_B { + export namespace h_C { + export var h_x = "This is equivalent to the code above."; + } + } +} +Assert.equal(h_A.h_B.h_C.h_x, "This is equivalent to the code above."); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/1_source_files_dependencies/source_00.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/1_source_files_dependencies/source_00.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c22da7bc1f12799c20af6b171e6cc4e7f231646 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/1_source_files_dependencies/source_00.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +type Color = [number, number, number]; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/1_source_files_dependencies/source_files_dependencies.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/1_source_files_dependencies/source_files_dependencies.ts new file mode 100644 index 0000000000000000000000000000000000000000..257831a87f05ecc86c092e6485422232ed9e8bfb --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/1_source_files_dependencies/source_files_dependencies.ts @@ -0,0 +1,28 @@ +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The TypeScript compiler automatically determines a source file's dependencies and includes those dependencies in the program being compiled. + Any files included as dependencies in turn have their references analyzed in a transitive manner until all dependencies have been determined. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../../../suite/assert.js"; + +let red: Color = [255, 0, 0]; +Assert.equal(JSON.stringify(red), '[255,0,0]'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9f6fb69caded4bb4bfe6642c57fce4502326c544 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_1.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export function message():string { + return 'string'; +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_10.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..fafe386049c4de8e56ba69ae80641f67176091ea --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_10.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export interface Point { x: number; y: number }; + +export function point(x: number, y: number): Point { + return { x, y }; +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..512c7b88a07eb842a119ff582f2e15fbfc2ebca6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_2.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export let h_x: number = 0; +export let h_y: number = 1; +export let h_z: number = 2; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1f005b1ecb34a5095fa4b687c3b5bcae031e359b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_3.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +let x:number = 0; +export default x; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..15a51b3ab1eb0edeb64656a90bbf47c28b203bc5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_4.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export default function message():string { + return 'string'; +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..7bb278e263064cf4bed8b70a21f43bcb74d52510 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_5.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function message():string { + return 'string'; +} + +export default message; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..0ad23bd163e206df8b953e3488df6d852db4a6f1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_6.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function message():string { + return 'string'; +} + +export { message as default }; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..fab5d937f616b564705120e7e519f5dcbde1b4a2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_7.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function message():string { + return 'string'; +} + +interface message { + x: number; + y: number; +} + + +export default message; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_8.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f1327d564e48e559c9b94a129f6777d1db1fde1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_8.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export default 'hello'; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_9.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..899278f98f694dfaa6f921611f183cb5aee2275b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/1_programs_and_source_files/source_9.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +class Point { + public x: number; + public y: number; + constructor(a: number, b: number) { + this.x = a; + this.y = b; + } +} +export default Point; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/2_modules/modules_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/2_modules/modules_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..03406a4a93c69ffce5c9dd307c430d8ff93dd1b0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/2_modules/modules_1.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An interface declaration declares an interface type. + An InterfaceDeclaration introduces a named type (section 3.7) in the containing declaration space. + The BindingIdentifier of an interface declaration may not be one of the predefined type names (section 3.8.1). + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import { message } from '../1_programs_and_source_files/source_1.js'; + +Assert.equal(message(), 'string'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..482d3537e4a7f52c65b4351cb37670f5efa7c8f7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_1.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Import declarations are used to import entities from other modules and provide bindings for them in the current module. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import * as h_m from '../1_programs_and_source_files/source_2.js'; + +Assert.equal(h_m.h_x, 0); +Assert.equal(h_m.h_y, 1); +Assert.equal(h_m.h_z, 2); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..2fe3642b6bf6db8b518f5894c9b6835bd42cb889 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + imports the module with the given name and creates a local binding for the module itself. The local binding is + classified as a value (representing the module instance) and a namespace (representing a container of types and + namespaces). + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import { h_x, h_y, h_z } from '../1_programs_and_source_files/source_2.js'; + +Assert.equal(h_x, 0); +Assert.equal(h_y, 1); +Assert.equal(h_z, 2); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..b444779c3c9d13bb254a14c3cf8aec26739e6a2f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_3.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + imports a given module and creates local bindings for a specified list of exported members of the module. The specified + names must each reference an entity in the export member set (11.3.4.4) of the given module. The local bindings have t + he same names and classifications as the entities they represent unless as clauses are used to that specify different + local names. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import { h_x as a, h_y as b, h_z as c } from '../1_programs_and_source_files/source_2.js'; + +Assert.equal(a, 0); +Assert.equal(b, 1); +Assert.equal(c, 2); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..11c402ebe247d31681db1a12012c1335ac7910a5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/3_import_declarations/import_declarations_4.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + imports a given module and creates local bindings for a specified list of exported members of the module. The specified + names must each reference an entity in the export member set (11.3.4.4) of the given module. The local bindings have t + he same names and classifications as the entities they represent unless as clauses are used to that specify different + local names. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import x from '../1_programs_and_source_files/source_3.js'; +import { default as a } from '../1_programs_and_source_files/source_3.js'; + +Assert.equal(x, 0); +Assert.equal(a, 0); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/4_import_require_declarations/import_require_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/4_import_require_declarations/import_require_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..574384e19f9d128843615ae3a171d6661f3ae1ce --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/4_import_require_declarations/import_require_declarations_1.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Import require declarations exist for backward compatibility with earlier versions of TypeScript. + An import require declaration of the form + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import * as m from "../1_programs_and_source_files/source_2.js" + +Assert.equal(m.h_x, 0); +Assert.equal(m.h_y, 1); +Assert.equal(m.h_z, 2); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4cf7bf66f117d7facca18ab75ff94d3a075771a1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_1.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import message from '../1_programs_and_source_files/source_4.js'; + +Assert.equal(message(), 'string'); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..88e2c6d8c22e5ca07b8ab75f043c161a312a8d35 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_2.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import message from '../1_programs_and_source_files/source_5.js'; + +Assert.equal(message(), 'string'); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc93b8382d2474845b7a3f3d956ddda0efe7786d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_3.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import message from '../1_programs_and_source_files/source_6.js'; + +Assert.equal(message(), 'string'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..aaa19b54808255fa34b332013184e6606879888f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_4.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import message from '../1_programs_and_source_files/source_7.js'; + +Assert.equal(message(), 'string'); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..c214a7f21fdd4f7f4d55c5d1065f8eeca3e12c0c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/5_export_declarations/export_declarations_5.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import message from '../1_programs_and_source_files/source_8.js'; + +Assert.equal(message, 'hello'); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/6_export_assignments/export_assignments_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/6_export_assignments/export_assignments_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c7b3dee269afe526952bc33af89ecd1d5d1fab73 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/6_export_assignments/export_assignments_1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export assignments exist for backward compatibility with earlier versions of TypeScript. An export assignment + designates a module member as the entity to be exported in place of the module itself. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import Pt from '../1_programs_and_source_files/source_9.js'; + +let point = new Pt(10, 20); +Assert.equal(point.x, 10); +Assert.equal(point.y, 20); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c386db17badf1bff91215ca8d7f9a686a693cc90 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A variable declaration and 'require' call is emitted for a particular imported module only if the imported module, or + a local alias that references the imported module, is referenced as a PrimaryExpression somewhere in the body of the + importing module. If an imported module is referenced only as a NamespaceName or TypeQueryExpression,nothing is emitted. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import * as g from '../1_programs_and_source_files/source_10.js'; + +let p = g.point(10, 20); +Assert.equal(p.x, 10); +Assert.equal(p.y, 20); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6efd439a18ce2d15dd1bac7525b3004d823b07e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A variable declaration and 'require' call is emitted for a particular imported module only if the imported module, or + a local alias that references the imported module, is referenced as a PrimaryExpression somewhere in the body of the + importing module. If an imported module is referenced only as a NamespaceName or TypeQueryExpression,nothing is emitted. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +import * as g from '../1_programs_and_source_files/source_10.js'; + +let p: g.Point = { x: 10, y: 20 }; +Assert.equal(p.x, 10); +Assert.equal(p.y, 20); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9c29f7714983eca9b2be11420c1d57e58ce95121 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_1.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Expressions controlling 'if' statements can be of any type (and not just type Boolean). + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let a = true; + +if (a) { + a = false; +} + +Assert.isFalse(a); + +let b = "string b"; +if (b) { + b = "string b 2"; +} +Assert.equal("string b 2", b); + +let c = 1; +if (c) { + c = 2 +} +Assert.equal(2, c); + +var myObject = { + d: 4 +} +if (myObject) { + myObject.d = 5; +} +Assert.equal(5, myObject.d); + +let myObject2 = 1; +if (myObject2 == 2) { + myObject2 = 3; +} +Assert.equal(1, myObject2); + +let und: undefined; +let flag = false; +if (und) { + flag = true; +} else { + flag = false; +} +Assert.isFalse(flag); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b81710a25c2b6e5af38db2c791b10c31e368c6d8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_2.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Expressions controlling 'Do' statements can be of any type (and not just type Boolean). + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var num = 1; +do { + if (num % 5 == 0) { + + break; + } + num++; +} while (num); +Assert.equal(5, num); + +do { + num++; +} while (num < 100); + +Assert.equal(100, num); + + +var myObject = { + d: 1, +} +do { + if (myObject.d % 5 == 0) { + + break; + } + myObject.d++; +} while (myObject); +Assert.equal(5, myObject.d); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..aa04e3ee8110d0fe18fe4ab123e84297c5acd29d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_3.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Expressions controlling 'while' statements can be of any type (and not just type Boolean). + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let arr: number[] = []; +let i = 0; +while (true) { + arr[i] = i * 2; + i++; + if (i >= 10) { + break; + } +} +Assert.equal(JSON.stringify(arr), '[0,2,4,6,8,10,12,14,16,18]'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..2139e475dbe86c7cf37dcf13f5a07b0298dfdd38 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Blocks are extended to include, local interface + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface BlocksInterface { + name: string; +} +class MyBlocks implements BlocksInterface { + name: string; + + constructor(name: string) { + this.name = name; + } + + public getName() { + return this.name; + } + +} +var myBlocks = new MyBlocks("Open Harmony"); + +Assert.equal("Open Harmony", myBlocks.getName()); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f5e8ed75b91333a867c1d4e4ca64e892a453a68 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_2.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Blocks are extended to include, type alias + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type Parent = { + name: string; +}; + +type Child = Parent & { + age: number +}; + + +const child: Child = { name: "name", age: 10 }; + +Assert.equal("name", child.name) +Assert.equal(10, child.age); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..803c7cab14cd1436dc04afbfa5a05307bc2ff097 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_3.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Blocks are extended to include, enum + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +enum Direction { + Forward, + Back, + Left, + Right, +} + +class Car { + name!: string; + direction!: Direction; +} + +const car = new Car(); +car.name = "myCar"; +car.direction = Direction.Forward; + +Assert.equal("myCar", car.name) +Assert.equal(Direction.Forward, car.direction); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..a884aa1e6c46a7b064defba293d58ced2459728e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/blocks/blocks_4.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Blocks {} + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +{ + var block1: number | undefined = 1024; + let block2: string | undefined = "NARC"; + Assert.equal(block1, 1024); + Assert.equal(block2, "NARC"); +} +{ + var block1: number | undefined; + let block2: string | undefined; + Assert.equal(block1, 1024); + Assert.isUndefined(block2); +} \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/break_statements/break_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/break_statements/break_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..e9b559590c68b476abfc7010361b0ecb98790240 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/break_statements/break_statements.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: A 'break' statement is required to be nested, directly or indirectly (but not crossing function boundaries), within an iteration ('do', 'while', 'for', or 'for-in') or 'switch' statement. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let num: number = 0; +let count: number = 0; +do { + num++; + if (num >= 5) { + break; + } +} while (num < 10); +Assert.equal(5, num); + + +num = 0; +for (num = 0; num <= 20; num++) { + if (num >= 5) { + break; + } +} +Assert.equal(5, num); + +let arr = [0, 1, 2, 3, 4, 5]; +for (let index in arr) { + if (arr[index] >= 2) { + break; + } + count++; +} +Assert.equal(2, count); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/continue_statements/continue_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/continue_statements/continue_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..95dc3b4011fa4e82212641a1303dcc067a5012dc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/continue_statements/continue_statements.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: A 'continue' statement is required to be nested, directly or indirectly (but not crossing function boundaries), within an iteration ('do', 'while', 'for', or 'for-in') statement. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let num: number = 0; +let count: number = 0; +do { + num++; + if (num % 2 == 0) { + continue; + } + count++; +} while (num < 10); +Assert.equal(5, count); + + +num = 0; +count = 0; +for (num = 0; num <= 20; num++) { + if (num % 2 == 0) { + continue; + } + count++; +} +Assert.equal(10, count); + +count = 0; +let arr = [0, 1, 2, 3, 4, 5]; +for (let index in arr) { + if (arr[index] % 2 == 0) { + continue; + } + count++; +} +Assert.equal(3, count); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_in_statements/for_in_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_in_statements/for_in_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..45124ed81020c566e693fed731ff5b58356cb650 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_in_statements/for_in_statements.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: test for (v in expr) statement + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let arr = [4, 5, 6]; + +let i = 0; +for (let index in arr) { + i++; +} +Assert.equal(3, i); + +const person = { + name: "opharmony", + role: "tools", + age: 3, +}; + +i = 0; +for (const key in person) { + i++; +} +Assert.equal(3, i); + +interface ABC { + a: number + b: string +} + +const x: ABC = { + a: 1, + b: '2' +} + +i = 0; +for (let key in x) { + i++; +} +Assert.equal(2, i); + +i = 0; +for (var key in x) { + i++; +} +Assert.equal(2, i); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_of_statements/for_of_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_of_statements/for_of_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..774a0bd61a343daf8a7a440007413046ff73b61a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_of_statements/for_of_statements.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: test for (v of expr) statement + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let count = 0; +for (let word of ["one", "two", "three"]) { + count++; +} +Assert.equal(3, count); + +count = 0; +let s = [0, 1, 2, 3, 4]; +for (let value of s) { + count++; +} +Assert.equal(5, count); + +count = 0; +let blogName: string = "openHarmony"; +for (let character of blogName) { + count++; +} +Assert.equal(11, count); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_statements/for_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_statements/for_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..288bf5c529e7e25f6f82fb2b0b340151132c79ed --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/for_statements/for_statements.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Variable declarations in 'for' statements are extended in the same manner as variable declarations in variable statements + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let result = 0; +for (let i: number = 0; i < 10; i++) { + result++; +} +Assert.equal(10, result); + +let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; +for (let i: number = 0; i < arr.length; i++) arr[i] = 0; +Assert.equal(JSON.stringify(arr), '[0,0,0,0,0,0,0,0,0]'); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/let_and_const_declarations/let_and_const_declarations.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/let_and_const_declarations/let_and_const_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..82a9d8a39af08d1a2c1f807c135d3bb43ebaef51 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/let_and_const_declarations/let_and_const_declarations.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Let and Const Declarations + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let a = 1; +Assert.equal(1, a); + +a = 2; +Assert.equal(2, a); + +const num = 9; +Assert.equal(9, num); + +const myname = 'openharmony'; +Assert.equal('openharmony', myname); + +const myBoolean = true; +Assert.isTrue(myBoolean); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/return_statements/return_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/return_statements/return_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..015db368afb9c198d3b8bd3db2a1d529468cc0dc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/return_statements/return_statements.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Return Statements + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function h_f(): (h_x: string) => number { + return h_s => h_s.length; +} + +Assert.equal(11, h_f()("openharmony")); + +function testReruen() { + for (let i = 0; i < 10; i++) { + + if (i == 5) { + return i; + } + } +} +Assert.equal(5, testReruen()); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/switch_statements/switch_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/switch_statements/switch_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..5cd194a4b5800969aeb9bb8c54286f53c6d39b72 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/switch_statements/switch_statements.ts @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from (section 3.11.4) the type of the 'switch' expression + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function testSwitch(inputVar: number) { + let outputVar: string; + switch (inputVar) { + case 1: + outputVar = 'a'; + break; + case 2: + outputVar = 'b'; + break; + case 3: + outputVar = 'c'; + break; + case 4: + outputVar = 'd'; + break; + default: + outputVar = 'e'; + break; + } + return outputVar; +} + +Assert.equal("a", testSwitch(1)); +Assert.equal("b", testSwitch(2)); +Assert.equal("c", testSwitch(3)); +Assert.equal("d", testSwitch(4)); +Assert.equal("e", testSwitch(5)); + +function testSwitchExpression(x: number, y: number) { + let outputVar: string; + switch (x + y) { + case 0: + outputVar = 'a'; + break; + case 5: + outputVar = 'b'; + break; + case 10: + outputVar = 'c'; + break; + + default: + outputVar = 'd'; + } + return outputVar; +} + +Assert.equal("a", testSwitchExpression(-1, 1)); +Assert.equal("b", testSwitchExpression(1, 4)); +Assert.equal("c", testSwitchExpression(5, 5)); +Assert.equal("d", testSwitchExpression(8, 10)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/throw_statements/throw_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/throw_statements/throw_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..8af052297420f81736d6cd4d8e38f3631a76e468 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/throw_statements/throw_statements.ts @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The expression specified in a 'throw' statement can be of any type. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function testThrow(inputVar: number) { + let outputVar: string; + if (inputVar == 1) { + throw 100; + } + + if (inputVar == 2) { + throw 'a'; + } + + if (inputVar == 3) { + throw true; + } + + if (inputVar == 4) { + throw new Error('Something bad happened'); + } +} + +try { + testThrow(1); +} catch (error) { + Assert.equal(100, error); +} + +try { + testThrow(2); +} catch (error) { + Assert.equal('a', error); +} + +try { + testThrow(3); +} catch (error) { + Assert.isTrue(error); +} + +try { + testThrow(4); +} catch (error: any) { + Assert.equal('Something bad happened', error.message); +} + +try { + testThrow(4); +} catch (error) { + Assert.isTrue(error instanceof Error); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/try_statements/try_statements.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/try_statements/try_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b1e992e7ca60daa7b13ab10d63ab3d03461cbed --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/try_statements/try_statements.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The variable introduced by a 'catch' clause of a 'try' statement is always of type Any. It is not possible to include a type annotation in a 'catch' clause. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../suite/assert.js' + +try { + new Array(100000000000000000000); +} +catch (err) { + Assert.isTrue(err instanceof RangeError); +} + +try { + let a: any; + let b = a.name; +} +catch (err) { + Assert.isTrue(err instanceof TypeError); +} + +try { + decodeURI('%'); +} +catch (err) { + Assert.isTrue(err instanceof URIError); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8eae01c6f4f36db9a8d6f69cec5f10291c54229f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_1.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type T of a variable introduced by a simple variable declaration is determined as follows: + If the declaration includes a type annotation, T is that type. + Otherwise, if the declaration includes an initializer expression, T is the widened form (section 3.12) of the type of the initializer expression. + Otherwise, T is the Any type. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var h_a; + +var h_b: number; + +var h_c = 30; + +var h_d = { x: 40, y: "hello" }; + +var h_e: any = "test"; +h_a = 1; +Assert.equal(1, h_a); + +h_a = '111'; +Assert.equal('111', h_a); + +h_b = 20; +Assert.equal(20, h_b); + +Assert.equal(30, h_c); + +Assert.equal(40, h_d.x); +Assert.equal("hello", h_d.y); + +var h_x = 50; +Assert.equal(50, h_x); +var h_x: number; +Assert.equal(50, h_x); +if (h_x == 50) { + var h_x = 100; + Assert.equal(100, h_x); + + h_x = 200; + + Assert.equal(200, h_x); +} + +Assert.equal(200, h_x); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a9501014d59883b34d84f3e23a6f52df100e7056 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_2.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type T of a variable introduced by a simple variable declaration is determined as follows: + If the declaration includes a type annotation, T is that type. + Otherwise, if the declaration includes an initializer expression, T is the widened form (section 3.12) of the type of the initializer expression. + Otherwise, T is the Any type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface Point { h_x: number; h_y: number; } + +var h_a = { h_x: 0, h_y: 1 }; +var h_b: Point = { h_x: 10, h_y: 11 }; +var h_c = { h_x: 100, h_y: 111 }; +var h_d: { h_x: number; h_y: number; } = { h_x: 1000, h_y: 1001 }; +var h_e = <{ h_x: number; h_y: number; }>{ h_x: 10000, h_y: 10001 }; + +Assert.equal(0, h_a.h_x); +Assert.equal(1, h_a.h_y); + +Assert.equal(10, h_b.h_x); +Assert.equal(11, h_b.h_y); + +Assert.equal(100, h_c.h_x); +Assert.equal(111, h_c.h_y); + +Assert.equal(1000, h_d.h_x); +Assert.equal(1001, h_d.h_y); + +Assert.equal(10000, h_e.h_x); +Assert.equal(10001, h_e.h_y); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7ca8929aa80556b7474d7bb2b8d9a73350a7880b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_3.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Multiple declarations for the same variable name in the same declaration space are permitted, + provided that each declaration associates the same type with the variable. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var h_a: number = 10; +var h_a: number = 20; +Assert.equal(h_a, 20); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1d676f3b0879e47b5dd5419c55b84bbc71100900 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: A destructuring variable declaration introduces zero or more named variables and initializes them with values extracted from properties of an object + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var object_name = { + key1: "value1", + key2: "value2", + key3: ["content1", "content2"], + key4: true, + key5: undefined +} + +var { key1, key2: y, key3, key4: z = false, key5: k = true } = object_name; + +Assert.equal("value1", key1); +Assert.equal("value2", y); +Assert.equal("content1", key3[0]); +Assert.equal("content2", key3[1]); +Assert.isTrue(z); +Assert.isTrue(k); + + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..79e1b11698b5c96e38a6ec67eb1bed5fd141d774 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_2.ts @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type T associated with a destructuring variable declaration is determined as follows: + If the declaration includes a type annotation, T is that type. + Otherwise, if the declaration includes an initializer expression, T is the type of that initializer expression. + Otherwise, T is the Any type. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface IPerson { + myname: string, + myage: number, +} + +var customer: IPerson = { + myname: "Tom", + myage: 10, +} +let { myname, myage } = customer; + +Assert.isString(myname); +Assert.isNumber(myage) + + +let o = { + a: "foo", + b: 12, + c: "bar" +}; +let { a: newName1 } = o; +Assert.isString(newName1); +var ohArray: number[] = [10, 20, 30]; + +var [x, y, z = 10, k = 10] = ohArray; +Assert.equal(10, x); +Assert.equal(20, y); +Assert.equal(30, z); +Assert.equal(10, k); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..ec75bc8e66705b48406ecaccd7ada02ae2db58df --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_3.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type T associated with a binding property is determined as follows: + Let S be the type associated with the immediately containing destructuring variable declaration, binding property, or binding element. + If S is the Any type: + If the binding property specifies an initializer expression, T is the type of that initializer expression. + Otherwise, T is the Any type. + Let P be the property name specified in the binding property. + If S has an apparent property with the name P, T is the type of that property. + Otherwise, if S has a numeric index signature and P is a numerical name, T is the type of the numeric index signature. + Otherwise, if S has a string index signature, T is the type of the string index signature. + Otherwise, no type is associated with the binding property and an error occurs. + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var object_name = { + key1: "value1", + key2: "value2", + key3: ["content1", "content2"], + key4: true, + key5: undefined +} + + +var ohArray: number[] = [10, 20, 30]; + +var { key1, key3: [y, z = 10, k = 10] = ohArray, key5: [a, b] = ohArray } = object_name; + +Assert.equal("value1", key1); +Assert.equal("content1", y); +Assert.equal("content2", z); +Assert.equal(10, k); +Assert.equal(10, a); +Assert.equal(20, b); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/3_implied_type/implied_type.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/3_implied_type/implied_type.ts new file mode 100644 index 0000000000000000000000000000000000000000..13157d91996968c1a9903fb1f7089a5dce34649e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/statements/variable_statements/3_implied_type/implied_type.ts @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: A variable, parameter, binding property, or binding element declaration that specifies a binding pattern has an implied type which is determined + module: ESNext + isCurrent: true +---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var object_name = { + key1: "value1" +} + +function f({ aa = {}, b = "hello", c = 3, d = object_name }) { + Assert.equal("object", typeof (aa)); + Assert.equal("string", typeof (b)); + Assert.equal("number", typeof (c)); + Assert.equal("object", typeof (d)); +} + +var objectFun = { + a: [1, 2], + b: "2", + c: 3, + d: object_name +}; +f(objectFun); + +var [a1, b1, c1, d1] = [1, "hello", true, object_name]; + +Assert.equal("number", typeof (a1)); +Assert.equal("string", typeof (b1)); +Assert.equal("boolean", typeof (c1)); +Assert.equal("object", typeof (d1)); + +function testRest(...restElements: any[]): any { + Assert.isTrue(restElements.length > 0); + return restElements[0]; +} + +Assert.isString(testRest("str", "str2")); +Assert.isNumber(testRest(1, 2)); \ No newline at end of file