diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..32a416d660cb7a271f452fdc488a009f67617077 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_1.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: > + If the array literal contains no spread elements, and if the array literal is contextually typed (section 4.23) + by a type T and T has a property with the numeric name N, where N is the index of the element expression in the array literal, + the element expression is contextually typed by the type of that property. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface MyArray { + 0: number; + 1: string; + length: 2; +} +const myArray: MyArray = [42, "hello"]; +Assert.isNumber(myArray[0]); +Assert.isString(myArray[1]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..78b09fffd455636388c8a558cf5575052354479b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_2.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: > + If the array literal is contextually typed by a type T with a numeric index signature, + the element expression is contextually typed by the type of the numeric index signature + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let MyArray: { + [key: number]: string; +} = ["aaa", "bbb", "ccc"]; +Assert.isString(MyArray[0]); +Assert.isString(MyArray[1]); +Assert.isString(MyArray[2]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..270c64722a6422f6e2f4f42475b3d38ed4b47cbc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_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: > + If the array literal is empty, the resulting type is an array type with the element type Undefined. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const emptyArray: any[] = []; +Assert.isUndefined(emptyArray[0]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..81a90b45b6b578f129502608380a792e640fbda2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_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: > + If the array literal contains no spread elements and is contextually typed by a tuple-like type (section 3.3.3), + the resulting type is a tuple type constructed from the types of the element expressions + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let myTuple: [string, number] = ["hello", 42]; +Assert.isString(myTuple[0]); +Assert.isNumber(myTuple[1]); +function foo(pair: [string, number]) { + return pair +} +Assert.equal(JSON.stringify(foo(["hello", 42])), '["hello",42]'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..b620932604fd0bb8cfc05eed9d52c2e20469daba --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_5.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: > + If the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section 4.21.1), + the resulting type is a tuple type constructed from the types of the element expressions. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let [x, y]: [string, number] = ["hello", 42]; +Assert.isString(x); +Assert.isNumber(y); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e51f2a2441d4d8b5a801af3b3f0c4fd500bbc93 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/array_literals/array_literal_6.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: > + The resulting type is an array type with an element type that is the union of the types of + the non-spread element expressions and the numeric index signature types of the spread element expressions. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let arr = [1, 2, ...["three", "four"], 5]; +Assert.isNumber(arr[0]); +Assert.isNumber(arr[1]); +Assert.isString(arr[2]); +Assert.isString(arr[3]); +Assert.isNumber(arr[4]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d2d837d32c6b740c76c4ed29a2d8d4b4a7e17b48 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_1.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: > + Arrow functions are extended from JavaScript to optionally include parameter and return type annotations. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const addNumbers = (num1: number, num2: number): number => { + return num1 + num2; +}; +const result1 = addNumbers(5, 10); +Assert.equal(result1, 15); +const greet = (name: string) => { + return name; +}; +const result2 = greet("Alice"); +Assert.equal(result2, "Alice"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d483ad0bc0c7898aa4cc6a603457e6c9b192a375 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_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: > + 1.( ... ) => expr is exactly equivalent to ( ... ) => { return expr ; } + 2.id => { ... } id => expr are exactly equivalent to + ( id ) => { ... } ( id ) => expr + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const myFunctionA = (num: string): string => { + return num; +}; +const resultA = myFunctionA("hello,world"); +Assert.equal(resultA, "hello,world"); +const myFunctionB: () => string = () => "hello,world"; +const resultB = myFunctionB(); +Assert.equal(resultB, "hello,world"); +let getTempItem = (id: any) => ({ id: id, name: "Temp" }); +const TempAResult = getTempItem(123); +Assert.equal(TempAResult.id, 123); +let getTempItemB = function (id: any) { + return { + id: id, + name: "Temp", + }; +}; +const TempBResult = getTempItemB(123); +Assert.equal(TempBResult.id, 123); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..a8aa4b781951ad650923012ca693f2fef64cdc9f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_3.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: > + When an arrow function with an expression body and no return type annotation is contextually typed + by a function type T and a contextual signature S can be extracted from T, + the expression body is contextually typed by the return type of S. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let myFunction = (h_x: number, h_y: number) => { return h_x + h_y; } +function myFunc(myValue: (x: number, y: number) => number) { + return myValue; +} +Assert.isFunction(myFunc(myFunction)); +function getx(x: number): number { + return x; +} +const a: number = 10; +let x = () => { return getx(a); } +let y = () => getx(a); +Assert.equal(x(), 10); +Assert.equal(y(), 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..f7e54b5fac15e5138349a529ec855430fc0c3c44 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/arrow_function/arrow_function_4.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: > + A function expression introduces a new dynamically bound this, whereas an arrow function expression preserves the this of its enclosing context. + Arrow function expressions are particularly useful for writing callbacks, which otherwise often have an undefined or unexpected this. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class ToAlert { + information = "Have a good time!"; + F() { + let for_this = this; + for_this.information = "sad boy"; + Assert.equal(for_this.information, "sad boy") + } +}; +let a = new ToAlert(); +a.F(); +a.information = "boy!"; +Assert.isString(a.information); +Assert.equal(a.information, "boy!"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..2dba576210c90a646c5de65a01baf7af852a09e2 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_1.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: > + An assignment of the form 'v = expr' requires v to be classified as a reference or as an assignment pattern. + The expr expression is contextually typed by the type of v, and the type of expr must be assignable to the type of v, + or otherwise a compile-time error occurs. The result is a value with the type of expr. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var sum: Function = function (x: number, y: number) { + return x + y +} +let result = sum(5, 10); +Assert.equal(result, 15); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..6603bcda8e5bfd301eeab0d6c86927e4d680eacb --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_2.ts @@ -0,0 +1,80 @@ +/* + * 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 compound assignment of the form 'v ??= expr' where '??=' is one of the compound assignment operators, + is subject to the same requirements, and produces a value of the same type, as the corresponding non-compound operation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var x: number = 10 +var y: number = 20 +x *= y +Assert.isNumber(x); +Assert.equal(x, 200); +x = 10; +y = 20; +x /= y +Assert.isNumber(x); +Assert.equal(x, 0.5); +x = 10; +y = 20; +x %= y +Assert.isNumber(x); +Assert.equal(x, 10); +x = 10; +y = 20; +x += y +Assert.isNumber(x); +Assert.equal(x, 30); +x = 10; +y = 20; +x -= y +Assert.isNumber(x); +Assert.equal(x, -10); +x = 10; +y = 20; +x <<= y +Assert.isNumber(x); +Assert.equal(x, 10485760); +x = 10; +y = 20; +x >>= y +Assert.isNumber(x); +Assert.equal(x, 0); +x = 10; +y = 20; +x >>>= y +Assert.isNumber(x); +Assert.equal(x, 0); +x = 10; +y = 20; +x &= y +Assert.isNumber(x); +Assert.equal(x, 0); +x = 10; +y = 20; +x ^= y +Assert.isNumber(x); +Assert.equal(x, 30); +x = 10; +y = 20; +x |= y +Assert.isNumber(x); +Assert.equal(x, 30); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..025a430f02f18cf67e831f6540209edc42b3b76b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/assignment_operators/assignment_operator_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: > + A compound assignment furthermore requires v to be classified as a reference and the type of the non-compound operation to be assignable to the type of v. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let obj = { + name: 'xiao', + age: 18 +} +let v = obj +v.age += v.age +Assert.equal(v.age, 36); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..731c9d1407c47fdac3096f41c43ae2bec1f42ba4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_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: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V, if V is variable and S is assignable to the type of V. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr = [1, 2] +let [first, second] = arr +Assert.equal(first, 1) +Assert.equal(second, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..8555c425b6beb676ceed55556eee2d4940a3d163 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_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 a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an object assignment pattern and, + for each assignment property P in V, S is the type Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let obj: any = { + a: "foo", + b: 15, + c: "bar" +} +let { a, b, c } = obj; +Assert.equal(a, 'foo') +Assert.equal(b, 15) +Assert.equal(c, 'bar'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..722f536d89245de6d481e2ad7b4a36ad34999e40 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_3.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: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an object assignment pattern and, + for each assignment property P in V, S has an apparent property with the property name specified in P of a type that is assignable to the target given in P. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let obj = { + a: "foo", + b: 15, + c: "bar" +} +let { a, b } = obj; +Assert.equal(a, 'foo'); +Assert.equal(b, 15); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..0d128233718282c2665a93f24ac7274b070c0448 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_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 a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an object assignment pattern and, + for each assignment property P in V, P specifies a numeric property name and S has a numeric index signature of a type that is assignable to the target given in P. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface Foo { + [key: number]: string + 18: string + 180: string +} +let t: Foo = { + 18: 'age', + 180: 'height' +} +let v = { + 18: 'Age', + 180: 'Height' +} +v = t +Assert.equal(v[18], 'age'); +Assert.equal(v[180], 'height'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..bcbbee18d3afb36bb1906c9b2176436a9ce8bb98 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_5.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 a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an object assignment pattern and, + for each assignment property P in V,S has a string index signature of a type that is assignable to the target given in P. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface Foo { + [key: string]: string + name: string + house: string +} +let t: Foo = { + name: 'xiao', + house: 'nanjing' +} +let v = { + name: '', + house: '' +} +v = t +Assert.equal(v.house, 'nanjing'); +Assert.equal(v.name, 'xiao'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..10a776d67dbba671a8bfbbefcb4ff73654aee7db --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_6.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: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an array assignment pattern, + for each assignment element E in V, S is the type Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let arr: any = [1, 2] +let [first, second]: [number, number] = arr +Assert.equal(first, 1) +Assert.equal(second, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7bb0e33ea8dab910fe75cc5b02ecded7e5b18b3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_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: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an array assignment pattern, + for each assignment element E in V, S is a tuple-like type with a property named N of a type that is assignable to the target given in E, + where N is the numeric index of E in the array assignment pattern. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +let s: ['0', string, number] = ['0', 'str', 10] +let [x, y, z]: [string, string, number] = s +Assert.equal(x, '0') +Assert.equal(y, 'str') +Assert.equal(z, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_8.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..5fe003b1d5b9cd078d1b3faac927d8f9f6b4dc7f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_8.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 a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an array assignment pattern, + S is the type Any or an array-like type, and, for each assignment element E in V, + S is not a tuple-like type and the numeric index signature type of S is assignable to the target given in E. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface A { + [key: number]: string +} +let a: A = ['a', 'b', 'c'] +let [x, y, z] = [a[0], a[1], a[2]] +Assert.equal(x, 'a') +Assert.equal(y, 'b') +Assert.equal(z, 'c'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9f13b77ca800caf1c85e98e0ec221ecc667de6f8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_1.ts @@ -0,0 +1,55 @@ +/* + * 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 binary + operator requires both operands to be of the Number primitive type or an enum type, + or at least one of the operands to be of type Any or the String primitive type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: number = 10 +var b: number = 20 +var x = a + b +Assert.equal(x, 30) +enum e1 { + A, + B, + C +} +enum e2 { + D, + E, + F +} +var c = e1.A +Assert.equal(c, 0) +var d = e2.D +Assert.equal(d, 0) +var y = c + d +Assert.equal(y, 0) +var e: any = true +Assert.isTrue(e) +var f: string = 's' +Assert.equal(f, 's') +var w = a + e +Assert.equal(w, 11) +var v = c + f +Assert.equal(v, '0s'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d563a695eac615c2c361b857e4f64dcffb11c0da --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_2.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: > + The binary + operator, + If one or both operands are of the String primitive type, the result is of the String primitive type. + Otherwise, the result is of type Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = true +var b: any = 's' +var c: boolean = false +var d: string = 'str' +var e: number = 10 +var f: any = 20 +var w = a + b +Assert.isString(w); +Assert.equal(w, 'trues'); +var v = a + f +Assert.isNumber(v); +Assert.equal(v, 21); +var x = a + c +Assert.isNumber(x); +Assert.equal(x, 1); +var y = a + d +Assert.isString(y); +Assert.equal(y, 'truestr'); +var z = a + e +Assert.isNumber(z); +Assert.equal(z, 11); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1584ff3a9218c286cafeab9630a8454eeefddfac --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_add_operator/the_add_operator_3.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: > + The binary + operator, + A value of any type can converted to the String primitive type by adding an empty string. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function func() { } +let s = func() + "" +Assert.isString(s); +Assert.equal(s, 'undefined'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_and_operator/the_and_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_and_operator/the_and_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..773396e33d84015c8c6c8b1cfdfe5e11ef04e994 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_and_operator/the_and_operator.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: > + The && operator permits the operands to be of any type and produces a result of the same type as the second operand. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = 10 +var b: any = 's' +var c: boolean = false +var d: boolean = true +var e: number = 20 +var f: number = 15 +var g: string = 'a' +var h: string = 'b' +var i: undefined = undefined +var j: undefined = undefined +var k = a && b +Assert.isString(k); +Assert.equal(k, 's'); +var l = c && d +Assert.isFalse(l); +var m = e && f +Assert.isNumber(m); +Assert.equal(m, 15); +var n = g && h +Assert.isString(n); +Assert.equal(n, 'b'); +var o = i && j +Assert.isUndefined(o); +var p = a && c +Assert.isFalse(p); +var q = a && e +Assert.isNumber(q); +Assert.equal(q, 20); +var r = a && g +Assert.isString(r); +Assert.equal(r, 'a'); +var s = a && i +Assert.isUndefined(s); +var t = c && e +Assert.isFalse(t); +var u = c && g +Assert.isFalse(u); +var v = c && i +Assert.isFalse(v); +var w = e && g +Assert.isString(w); +Assert.equal(w, 'a'); +var x = e && i +Assert.isUndefined(x); +var y = g && i +Assert.isUndefined(y); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_congruence_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_congruence_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..735549802719afc5d95037987fa5d179c79e3a6a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_congruence_operator.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: > + The '===' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = undefined; +var b: boolean = true; +var c: number = 1; +var d: string = 'str'; +var e: number = 1; +Assert.isFalse(a === b); +Assert.isFalse(a === c); +Assert.isFalse(a === d); +Assert.isFalse(b as unknown === c); +Assert.isTrue(c === e); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_equal_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_equal_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..95e2a6c463a6ca872605fb443f735add5efe8c98 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_equal_operator.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: > + The '==' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = undefined; +var b: boolean = true; +var c: number = 1; +var d: string = 'str'; +var e: number = 1; +Assert.isFalse(a == b); +Assert.isFalse(a == c); +Assert.isFalse(a == d); +Assert.isTrue(b as unknown == c); +Assert.isTrue(c == e); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_greater_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_greater_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..af6b372c83aa51a5c6e9c03551d64d775252c90e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_greater_operator.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: > + The '>' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = undefined; +var b: boolean = true; +var c: number = 10; +var d: string = 'str'; +var e: number = 0; +Assert.isFalse(a > b); +Assert.isFalse(a > c); +Assert.isFalse(a > d); +Assert.isTrue(c > (b as unknown as number)); +Assert.isTrue(c > e); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_greater_or_equal_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_greater_or_equal_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..eb07feba43e0bd25cd448e82b5aefab80b1a18f9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_greater_or_equal_operator.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: > + The '>=' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = undefined; +var b: boolean = true; +var c: number = 1; +var d: string = 'str'; +var e: number = 0; +Assert.isFalse(a >= b); +Assert.isFalse(a >= c); +Assert.isFalse(a >= d); +Assert.isTrue(c >= (b as unknown as number)); +Assert.isTrue(c >= e); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_incongruence_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_incongruence_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..fa381e16a2976dcc3b612c462262678ba2dc83e0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_incongruence_operator.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: > + The '!==' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = undefined; +var b: boolean = true; +var c: number = 1; +var d: string = 'str'; +var e: number = 1; +Assert.isTrue(a !== b); +Assert.isTrue(a !== c); +Assert.isTrue(a !== d); +Assert.isTrue(b as unknown !== c); +Assert.isFalse(c !== e); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_less_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_less_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..83e35d4f0d58d8e272cb3283a0fd88fd69b39444 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_less_operator.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: > + The '<' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = undefined; +var b: boolean = true; +var c: number = 1; +var d: string = 'str'; +var e: number = 0; +Assert.isFalse(a < b) +Assert.isFalse(a < c) +Assert.isFalse(a < d); +Assert.isFalse(c < (b as unknown as number)); +Assert.isFalse(c < e); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_less_or_equal_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_less_or_equal_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..86271a7683a28724b91feea0839bf56d60264b0f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_less_or_equal_operator.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: > + The '<=' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = undefined; +var b: boolean = true; +var c: number = 1; +var d: string = 'str'; +var e: number = 0; +Assert.isFalse(a <= b) +Assert.isFalse(a <= c) +Assert.isFalse(a <= d); +Assert.isTrue(c <= (b as unknown as number)); +Assert.isFalse(c <= e); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_unequal_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_unequal_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..68ba03331ed175c2a8993e3990938e814b7717bc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_greater/the_unequal_operator.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: > + The '!=' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = undefined; +var b: boolean = true; +var c: number = 1; +var d: string = 'str'; +var e: number = 1; +Assert.isTrue(a != b) +Assert.isTrue(a != c) +Assert.isTrue(a != d); +Assert.isFalse(b as unknown != c); +Assert.isFalse(c != e); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_in_operator/the_in_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_in_operator/the_in_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b201f2b87b5fe136ac18d47b6fb3554b8accf0e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_in_operator/the_in_operator.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: > + The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type, + and the right operand to be of type Any, an object type, or a type parameter type. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface A { + num: number +} +interface B { + str: string +} +function isString(k: A | B) { + let flag: boolean = false; + if ('num' in k) { + Assert.isTrue('num' in k); + flag = false; + } else if ('str' in k) { + Assert.isTrue('str' in k); + flag = true; + } + return flag; +} +let result1 = isString({ num: 20 }); +Assert.isFalse(result1); +let result2 = isString({ str: 'A' }); +Assert.isTrue(result2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_instanceof_operator/the_instanceof_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_instanceof_operator/the_instanceof_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a9e1e543588e037490928ad60264ec37c0e8b81 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_instanceof_operator/the_instanceof_operator.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 instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, + and the right operand to be of type Any or a subtype of the 'Function' interface type. + The result is always of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +interface Animal { + species: string +} +class Felidae implements Animal { + kind: string + species: string + constructor(species: string, kind: string) { + this.species = species + this.kind = kind + } +} +class Canidae implements Animal { + species: string + name: string + constructor(species: string, name: string) { + this.species = species + this.name = name + } +} +const getRandomAnimal = () => { + return Math.random() < 0.5 ? + new Canidae('Canidae', 'Wolf') : + new Felidae('Felidae', 'Tiger') +} +let Animal = getRandomAnimal() +if (Animal instanceof Canidae) { + Assert.isTrue(Animal instanceof Canidae) +} +if (Animal instanceof Felidae) { + Assert.isTrue(Animal instanceof Felidae); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_and_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_and_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f3c671a94319ed985e3894ecbca39fc8a92090d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_and_operator.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 '&' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = true; +var c: number = 9; +var x = a & b; +var y = a & c; +var bb = b & b; +var cc = c & c; +Assert.isNumber(x); +Assert.equal(x, 0); +Assert.isNumber(y); +Assert.equal(y, 8); +Assert.isNumber(bb); +Assert.equal(bb, 1); +Assert.isNumber(cc); +Assert.equal(cc, 9); +enum e { + A, + B, + C +} +var d = e.C; +var z = a & d; +Assert.isNumber(z); +Assert.equal(z, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_division_method_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_division_method_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..1e8304437c0eb4afab585824738bb6887ae4041b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_division_method_operator.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: > + The '/' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = true; +var c: number = 20; +var x = b / a; +var y = a / c; +Assert.isNumber(x); +Assert.equal(x, 0.1); +Assert.isNumber(y); +Assert.equal(y, 0.5); +Assert.isNumber(c / 2); +Assert.equal(c / 2, 10); +enum e { + A, + B, + C +} +var d = e.C; +var z = a / d; +Assert.isNumber(z); +Assert.equal(z, 5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_left_shift_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_left_shift_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..93c3f48936496de72687cad799a18bb40de2eb7c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_left_shift_operator.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: > + The '<<' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = true; +var c: number = 20; +var x = a << b; +var y = c << a; +Assert.isNumber(x); +Assert.equal(x, 20); +Assert.isNumber(y); +Assert.equal(y, 20480); +Assert.isNumber(c << 2); +Assert.equal(c << 2, 80); +enum e { + A, + B, + C +} +var d = e.C; +var z = a << d; +Assert.isNumber(z); +Assert.equal(z, 40); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_multiplication_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_multiplication_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..592d9bc61ad0b80addf766240d2607aec51cbb98 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_multiplication_operator.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: > + The '*' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = false; +var c: number = 20; +var x = a * b; +var y = a * c; +Assert.isNumber(x); +Assert.equal(x, 0); +Assert.isNumber(y); +Assert.equal(y, 200); +Assert.isNumber(c * 2); +Assert.equal(c * 2, 40); +enum e { + A, + B, + C +} +var d = e.C; +var z = a * d; +Assert.isNumber(z); +Assert.equal(z, 20); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_or_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_or_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..7da1b71d721f14a8b11b496aa06b5286d4a50b12 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_or_operator.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 '|' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = true; +var c: number = 9; +var x = a | b; +var y = a | c; +var bb = b | b; +var cc = c | c; +Assert.isNumber(x); +Assert.equal(x, 11); +Assert.isNumber(y); +Assert.equal(y, 11); +Assert.isNumber(bb); +Assert.equal(bb, 1); +Assert.isNumber(cc); +Assert.equal(cc, 9); +enum e { + A, + B, + C +} +var d = e.C; +var z = a | d; +Assert.isNumber(z); +Assert.equal(z, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_remainder_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_remainder_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..64034d3d7e83c7242463debdb894042d423ad749 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_remainder_operator.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: > + The '%' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '11'; +var b: any = true; +var c: number = 5; +var x = a % b; +var y = a % c; +Assert.isNumber(x); +Assert.equal(x, 0); +Assert.isNumber(y); +Assert.equal(y, 1); +Assert.isNumber(28 % c); +Assert.equal(28 % c, 3); +enum e { + A, + B, + C +} +var d = e.C; +var z = a % d; +Assert.isNumber(z); +Assert.equal(z, 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_right_shift_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_right_shift_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..c5dba842a470b7f53df6ad93f623a3d6cc317942 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_right_shift_operator.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: > + The '>>' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = true; +var c: number = 20; +var x = a >> b; +var y = a >> c; +Assert.isNumber(x); +Assert.equal(x, 5); +Assert.isNumber(y); +Assert.equal(y, 0); +Assert.isNumber(c >> 4); +Assert.equal(c >> 4, 1); +enum e { + A, + B, + C +} +var d = e.C; +var z = a >> d; +Assert.isNumber(z); +Assert.equal(z, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_subtraction_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_subtraction_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..ac23d291e32077ba2095885e581a477ea04e9e02 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_subtraction_operator.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: > + The '-' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = true; +var c: number = 20; +var x = a - b; +var y = a - c; +Assert.isNumber(x); +Assert.equal(x, 9); +Assert.isNumber(y); +Assert.equal(y, -10); +Assert.isNumber(c - 9); +Assert.equal(c - 9, 11); +enum e { + A, + B, + C +} +var d = e.A; +var z = a - d; +Assert.isNumber(z); +Assert.equal(z, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_unsigned_shift_to_the_right.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_unsigned_shift_to_the_right.ts new file mode 100644 index 0000000000000000000000000000000000000000..1710c25a4720c8f396c31fa444c2dc0c5a7b6390 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_unsigned_shift_to_the_right.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: > + The '>>>' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = true; +var c: number = 20; +var x = a >>> b; +var y = a >>> c; +Assert.isNumber(x); +Assert.equal(x, 5); +Assert.isNumber(y); +Assert.equal(y, 0); +Assert.isNumber(c >>> 4); +Assert.equal(c >>> 4, 1); +enum e { + A, + B, + C +} +var d = e.C; +var z = a >>> d; +Assert.isNumber(z); +Assert.equal(z, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_xor_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_xor_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..64c0333b88a5af1fff88f2309c3f769e1ebd6eb1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_mul/the_xor_operator.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: > + The '^' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = '10'; +var b: any = true; +var c: number = 20; +var x = a ^ b; +var y = a ^ c; +Assert.isNumber(x); +Assert.equal(x, 11); +Assert.isNumber(y); +Assert.equal(y, 30); +Assert.isNumber(c ^ 4); +Assert.equal(c ^ 4, 16); +enum e { + A, + B, + C +} +var d = e.C; +var z = a ^ d; +Assert.isNumber(z); +Assert.equal(z, 8); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_or_operator/the_or_operator_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_or_operator/the_or_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..3bfc017ab7189dd92af14552495fb1f29e9ccff4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_or_operator/the_or_operator_1.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: > + The || operator permits the operands to be of any type. + The type of the result is the union type of the two operand types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var a: any = 10; +var b: any = 's'; +var c: boolean = false; +var d: boolean = true; +var e: number = 20; +var f: number = 15; +var g: string = 'a'; +var h: string = 'b'; +var i: undefined = undefined; +var j: undefined = undefined; +var k = a || b; +Assert.isNumber(k); +Assert.equal(k, 10); +var l = c || d; +Assert.isTrue(l); +var m = e || f; +Assert.isNumber(m); +Assert.equal(m, 20); +var n = g || h; +Assert.isString(n); +Assert.equal(n, 'a'); +var o = i || j; +Assert.isUndefined(o); +var p = a || c; +Assert.isNumber(p); +Assert.equal(p, 10); +var q = a || e; +Assert.isNumber(q); +Assert.equal(q, 10); +var r = a || g; +Assert.isNumber(r); +Assert.equal(r, 10); +var s = a || i; +Assert.isNumber(s); +Assert.equal(s, 10); +var t = c || e; +Assert.isNumber(t); +Assert.equal(t, 20); +var u = c || g; +Assert.isString(u); +Assert.equal(u, 'a'); +var v = c || i; +Assert.isUndefined(v); +var w = e || g; +Assert.isNumber(w); +Assert.equal(w, 20); +var x = e || i; +Assert.isNumber(x); +Assert.equal(x, 20); +var y = g || i; +Assert.isString(y); +Assert.equal(y, 'a'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_or_operator/the_or_operator_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_or_operator/the_or_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..ab473317f68895e71bcba2de90a6cab815b207b1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/binary_operators/the_or_operator/the_or_operator_2.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: > + If the || expression is contextually typed, the operands are contextually typed by the same type. + Otherwise, the left operand is not contextually typed and the right operand is contextually typed by the type of the left operand. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var sum = function (x: number, y: number) { + return x + y +} +var average = function (a: number, b: number) { + return (a + b) / 2 +} +var rela1 = sum || average; +var rela2 = average || sum; +let a = rela1(1, 2); +let b = rela2(1, 2); +Assert.isFunction(rela1); +Assert.isFunction(rela2); +Assert.equal(rela1, sum); +Assert.equal(rela2, average); +Assert.equal(a, 3); +Assert.equal(b, 1.5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/class_expressions/class_expressions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/class_expressions/class_expressions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..83c9081bee9ec6e7d383397cebe7349bdddfc69b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/class_expressions/class_expressions_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: > + Just as with class declarations, class expressions would create a constructor function that can be used to construct instances. + Like class declarations, you may also declare property members and index members, as well as use the constructor parameters. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var Square = class { + acreage: number; + + constructor(public h_length: number, public h_width: number) { + this.acreage = this.h_length * this.h_width; + } +}; +var square = new Square(5, 10); +Assert.equal(square.acreage, 50); +let Ref = class Reflmpl { + public readonly _v = true; + constructor(private _rawValue: number, public _shaw = false) { + this._rawValue = _rawValue; + this._shaw = this._shaw; + } + get value() { + return this._rawValue; + } + set value(newVal) { + this._rawValue = newVal; + } + get shaw() { + return this._shaw; + } + set shaw(newShaw) { + this._shaw = newShaw; + } +} +let ref = new Ref(10, true); +Assert.equal(ref.value, 10); +Assert.equal(ref._shaw, true); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0868288f8ff7d1aa90e57c96c92e72e09601cece --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_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: > + In a variable, parameter, binding property, binding element, or member declaration, + an initializer expression is contextually typed by the type given in the declaration's type annotation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let a: number +a = 10 +Assert.isNumber(a) +function fun(x: string) { + let y = x + 'ing' + return y +} +Assert.isString(fun('str')) +interface Obj { + name: string + age: number +} +let obj: Obj = { + name: 'xiao', + age: 18 +} +Assert.isString(obj.name) +Assert.isNumber(obj.age); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_10.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..c9649d460a860f3a777dfcd336acb8eaf9fcc691 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_10.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: > + In a contextually typed array literal expression containing no spread elements, an element expression at index N is contextually typed by + the type of the property with the numeric name N in the contextual type, if any, or otherwise + the numeric index type of the contextual type, if any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let arr: { + [key: number]: string +} = { + 1: 'a', + 2: 'b', + 3: 'c', + 4: 'd' +} +Assert.isString(arr[1]) +Assert.isString(arr[2]) +Assert.isString(arr[3]) +Assert.isString(arr[4]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_11.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_11.ts new file mode 100644 index 0000000000000000000000000000000000000000..46c0bdd684c5992fdd8b39b72a05e5ee925d8a17 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_11.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: > + In a contextually typed array literal expression containing one or more spread elements, + an element expression at index N is contextually typed by the numeric index type of the contextual type, if any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let first = [1, 2] +let second = [3, 4] +let bothPlus = [0, ...first, ...second, 5] +Assert.isNumber(bothPlus[2]) +Assert.isNumber(bothPlus[4]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_12.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_12.ts new file mode 100644 index 0000000000000000000000000000000000000000..5c60fa1c14dae13bf381e427eb865d09c801b3c7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_12.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: > + In a contextually typed parenthesized expression, the contained expression is contextually typed by the same type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var a: number = 10 +var b: number = 5 +var c = (a + b) * (a - b) +Assert.isNumber(c) +interface Foo { + num: number + str: string +} +const x: Foo[] = [{ num: 18, str: 'hello' }, { num: 20, str: 'world' }] +Assert.isNumber(x[0].num) +Assert.isNumber(x[1].num) +Assert.isString(x[0].str) +Assert.isString(x[1].str); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_13.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_13.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7a6cd652f3ec4251d68acab75f40793d090d72c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_13.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: > + In a type assertion, the expression is contextually typed by the indicated type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Person { + name: string + age: number +} +let student = {} as Person +student.name = 'xiao' +student.age = 18 +Assert.isNumber(student.age) +Assert.isString(student.name); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_14.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_14.ts new file mode 100644 index 0000000000000000000000000000000000000000..76c8be1985c0fc904687b8d25acb28937450a893 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_14.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 || operator expression, if the expression is contextually typed, the operands are contextually typed by the same type. + Otherwise, the right expression is contextually typed by the type of the left expression. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var sum = function (x: number, y: number) { + return x + y +} +var average1 = function (a: number, b: number) { + return (a + b) / 2 +} +var rela1 = sum || average1 +Assert.equal(typeof rela1, 'function') +var average2: number = 10 +var rela2 = average2 || sum +Assert.equal(typeof rela2, 'number'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_15.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_15.ts new file mode 100644 index 0000000000000000000000000000000000000000..d7a649f139b5965d7aaaff876eae611dd5e8b161 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_15.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: > + In a contextually typed conditional operator expression, the operands are contextually typed by the same type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function processValue(value: number | (() => number)) { + var x = typeof value !== "number" ? value() : value + Assert.isNumber(x) +} +processValue(5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_16.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_16.ts new file mode 100644 index 0000000000000000000000000000000000000000..940f31181e885cbcc4d901b2ec7904ce8607be4f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_16.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: > + In an assignment expression, the right hand expression is contextually typed by the type of the left hand expression. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface A { + (a: number): void +} +const fn: A = function (a) { + Assert.isNumber(a) +} +fn(5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d24a9bad0e271903a1b3cfdec60e8d38cdc3cff7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_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: > + In a variable, parameter, binding property, binding element, or member declaration, + an initializer expression is contextually typed by for a parameter, the type provided by a contextual signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var f: (s: string) => string = function (x) { + Assert.isString(x) + return x.toLowerCase() +} +f('str'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..b94a14a8f595ca46420b1d7b5e039f41ca94708b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_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: > + In a variable, parameter, binding property, binding element, or member declaration, + an initializer expression is contextually typed by the type implied by the binding pattern in the declaration. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function f({ a = true, b = "hello", c = 1 }) { + Assert.isBoolean(a) + Assert.isString(b) + Assert.isNumber(c) +} +f({ a: false }); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..d15947b430862fac8eb97f1cad513bca2cfafdca --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_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: > + In the body of a function declaration, function expression, arrow function, method declaration, + or get accessor declaration that has a return type annotation, + return expressions are contextually typed by the type given in the return type annotation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function add(x: number, y: number): number { + return x + y +} +let sum = add(10, 20) +Assert.isNumber(sum) +type Fun = (a: number, b: number) => number +function fun(fn: Fun, x: number, y: number) { + return fn(x, y) +} +function minus(a: number, b: number) { + return a - b +} +function mul(a: number, b: number) { + return a * b +} +let m = fun(minus, 15, 7) +let n = fun(mul, 3, 6) +Assert.isNumber(m) +Assert.isNumber(n); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..dadabf167647bca096b45aa2adc93cbd137c96a3 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_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 the body of a function expression or arrow function that has no return type annotation, + if the function expression or arrow function is contextually typed by a function type with exactly one call signature, + and if that call signature is non-generic, return expressions are contextually typed by the return type of that call signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type Fun = { + description: string + (someThing: number): boolean +} +function fun1(fn: Fun) { + return fn.description + fn(100) +} +function fun2(num: number) { + Assert.equal(num, 100) + return false +} +fun2.description = 'hello' +Assert.equal(fun1(fun2), 'hellofalse'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..3d70a3597ec1ed2211e6df5c5f1b0c89c940ffa6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_6.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 a constructor declaration, return expressions are contextually typed by the containing class 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 + } +} +const p = new Person("xiao", 18) +Assert.isString(p.name) +Assert.isNumber(p.age); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..f910a94072475468b5369a3cda83884765fc51cd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_7.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: > + In the body of a get accessor with no return type annotation, + if a matching set accessor exists and that set accessor has a parameter type annotation, + return expressions are contextually typed by the type given in the set accessor's parameter type annotation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class C { + private _str = '' + private _arr: string[] = [] + get func() { + return this._str + } + set func(value: string) { + this._str= value + } + get f() { + return this._arr + } + set f(value: string[]) { + this._arr = value + } +} +const c = new C() +c.func= 'TS' +Assert.isString(c.func) +c.f = ['develop', 'test'] +c.f.push('ship') +Assert.equal(c.f, 'develop,test,ship'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_8.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..2036c81219e9f71c81b4b3f9a6f75f94e7c8e1a4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_8.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: > + In a typed function call, argument expressions are contextually typed by their corresponding parameter types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const obj: Record = { + boo: false, + arr: ['a', 'b', 'c'] +} +const para = (str: string): [T, Function] => { + const result1: T = obj[str]; + const result2: Function = (value: T) => { + obj[str] = value; + return obj[str]; + } + return [result1, result2]; +} +const [boo, mem2] = para('boo'); +const [arr, mem4] = para('arr'); +Assert.isBoolean(boo); +Assert.equal(arr, 'a,b,c'); +let x = mem2(true); +let y = mem4(false); +Assert.isTrue(x); +Assert.isFalse(y); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_9.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..e6fc7d38b75c358583e085a1162ada197767f0a0 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_9.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 contextually typed object literal, each property value expression is contextually typed by + the type of the property with a matching name in the contextual type, if any, or otherwise + for a numerically named property, the numeric index type of the contextual type, if any, or otherwise + the string index type of the contextual type, if any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Person { + (a: number): void +} +interface B { + fn: Person +} +const obj: B = { + fn: function (a) { + Assert.isNumber(a) + } +} +obj.fn(10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/grammar_ambiguities/grammar_ambiguities.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/grammar_ambiguities/grammar_ambiguities.ts new file mode 100644 index 0000000000000000000000000000000000000000..f93221902a1effedb805eeca5403281256ee10d5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/grammar_ambiguities/grammar_ambiguities.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 inclusion of type arguments in the Arguments production gives rise to certain ambiguities in the grammar for expressions. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +var h_g: number = 5 +var h_A: number = 3 +var h_B: number = 6 +function f(a: any, b?: any) { + return a +} +Assert.isFalse(f(h_g < h_A, h_B > 7)) +Assert.isFalse(f(h_g < h_A, h_B > +(7))) +type A1 = number +type B1 = number +function g1(a: T) { + return a; +} +function f1(a: any, b?: any) { + return a +} +Assert.equal(f1(g1(7)), 7); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/overload_resolution/overload_resolution_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/overload_resolution/overload_resolution_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ae09cd482e69689585a1e8f2176e66bac5502adc --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/overload_resolution/overload_resolution_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 compile-time processing of a typed function call consists, + a list of candidate signatures is constructed from the call signatures in the function type in declaration order. + A generic signature is a candidate in a function call with type arguments when the signature has the same number of type parameters + as were supplied in the type argument list, the type arguments satisfy their constraints, + and once the type arguments are substituted for their associated type parameters, + the signature is applicable with respect to the argument list of the function call. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function fun(x: Array, y: Array): Array +function fun(x: Array, y: Array, z: Array): Array +function fun(x: Array, y: Array, z?: V): Array { + return x +} +let arr1: Array = [1, 2, 3]; +let arr2: Array = ['a', 'b']; +let arr3: Array = [true, false]; +let a = fun(arr1, arr2); +Assert.equal(a, arr1); +let b = fun(arr1, arr2, arr3); +Assert.equal(b, arr1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/overload_resolution/overload_resolution_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/overload_resolution/overload_resolution_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a5d9d1095454932d5cc99369db08af67ea6b7e3d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/overload_resolution/overload_resolution_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: > + A non-generic signature is a candidate when + the function call has no type arguments, and + the signature is applicable with respect to the argument list of the function call. + 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/expressions/function_calls/type_argument_inference/type_argument_inference_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a27a866d9e1c8e208eabf0af978815e937dd830f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_1.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: > + Given a type parameter T and set of candidate types, the actual inferred type argument is determined, + if the set of candidate argument types is empty, the inferred type argument for T is T's constraint. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type t = number | string; +function select(h_x: T, h_y: T): T { + return h_x < h_y ? h_x : h_y; +} +var a = select(10, 20); +Assert.isNumber(a); +type HELLO = "HELLO"; +function getx(x: HELLO): string { + return x; +} +const x = "HELLO"; +Assert.equal(getx(x), "HELLO"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb9d601be9a9bd5cc09ad825814e6f4d81949087 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_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: > + Given a type parameter T and set of candidate types, the actual inferred type argument is determined, + if at least one of the candidate types is a supertype of all of the other candidate types, + let C denote the widened form of the first such candidate type. + If C satisfies T's constraint, the inferred type argument for T is C. + Otherwise, the inferred type argument for T is T's constraint. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type t = number | null | undefined +function select(h_x: T, h_y: T): T { + return h_x ? h_x : h_y; +} +var y = select(10, undefined) +if (typeof y === 'number') { + Assert.isNumber(y); +} +else { + Assert.isUndefined(y); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..a133fc3c7c4b5737331a3802b096eb0f3d2d3884 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_3.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: > + When a function expression is inferentially typed and a type assigned to a parameter in that expression references type parameters for which inferences are being made, + the corresponding inferred type arguments to become fixed and no further candidate inferences are made for them. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +function func(arg1: T, arg2:T) { + return [arg1, arg2]; +} +let a = func(3, 5); +let b = func('a', 'b'); +Assert.equal(a, '3,5'); +Assert.equal(b, 'a,b'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..173b602bbb6ad9be29b5a81d7b2f01abb401ab88 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_calls/type_argument_inference/type_argument_inference_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: > + If e is an expression of a function type that contains exactly one generic call signature and no other members, + and T is a function type with exactly one non-generic call signature and no other members, + then any inferences made for type parameters referenced by the parameters of T's call signature are fixed, + and e's type is changed to a function type with e's call signature instantiated in the context of T's call signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +type T = (arg: number) => number; +let f = (arg: T): T => { return arg }; +let para: T = function func(arg: number) { + return arg; +} +let result = f(para(20)); +Assert.equal(result, 20); +Assert.isFunction(f); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..36ee6593bea5b952c300dbf15d39e71cee811b3f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_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: > + If T is a union type, let U be the set of element types in T that have call signatures. + If each type in U has exactly one call signature and that call signature is non-generic, and if all of the signatures are identical ignoring return types, + then S is a signature with the same parameters and a union of the return types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type MyUnionType = ((x: number) => string) | ((x: number) => number); +function myFunction(fn: MyUnionType, arg: number) { + return fn(arg); +} +const fn1 = (x: number): string => `Hello,${x}`; +const fn2 = (x: number): number => x * 2; +const result1 = myFunction(fn1, 123); +const result2 = myFunction(fn2, 456); +Assert.equal(result1, "Hello,123"); +Assert.equal(result2, 912); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7dd95ab05a54dc2d7b8c88d7d506d7ea4088c4f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_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: > + If T is a function type with exactly one call signature, + and if that call signature is non-generic, S is that signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type MyFunctionType = (x: number, y: number) => number; +function myFunction(fn: MyFunctionType) { + return fn(2, 3); +} +const add: MyFunctionType = function (x: number, y: number) { + return x + y; +}; +const result = myFunction(add); +Assert.equal(result, 5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..efdd1e81574f9ca238aaa59f4d0b17eb847ef3d1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_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: > + Otherwise, no contextual signature can be extracted from T. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type MyType = { + fn: (a: number, b: string) => boolean; +}; +function myFunction(obj: MyType) { + const { fn } = obj; + const result = fn(123, "hello"); + Assert.isTrue(result); +} +let h_x: MyType = { + fn: function func() { + return true; + } +} +myFunction(h_x); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..79dc9f26e724c0ac8a7fdc2d8b5295c9ea81c308 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/function_expression/function_expressions_4.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: > + The type of a function expression is an object type containing a single call signature with parameter + and return types inferred from the function expression's signature and body. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +type Greeting = 'Hello, world'; +let x: Greeting = 'Hello, world'; +function f(x: Greeting): string { + return x; +} +Assert.equal(f(x), "Hello, world"); +function f2(x: T): T { + return x; +} +let a = f2(10); +Assert.equal(a, 10); +let b = f2("hello"); +Assert.equal(b, "hello"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/class.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/class.ts new file mode 100644 index 0000000000000000000000000000000000000000..8341313a6ed16ad71c0e85c3e5dd805f941b5f7d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/class.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: For a class, the constructor type associated with the constructor function object. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Car { + engine: string; + agelimit: number; + constructor(engine: string, agelimit: number) { + this.engine = engine; + this.agelimit = agelimit; + } +} +let car = new Car("Benchi", 2); +Assert.equal(car.agelimit, 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/enum.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/enum.ts new file mode 100644 index 0000000000000000000000000000000000000000..49f197edf1e35b26afa6c1fee560ebe3cf9281ea --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/enum.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: For an enum, the object type associated with the enum object. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +enum Direction { + up = 1, + down, + Left, + right, +} +const direction: Direction = Direction.up; +Assert.isNumber(direction); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/function.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/function.ts new file mode 100644 index 0000000000000000000000000000000000000000..d1513c3bf3e02cdf9774e4d06c93e1418ed5f023 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/function.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: For a function, the function type associated with the function object. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function myFunction(x: number): number { + return x * 2; +} +Assert.isFunction(myFunction); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/name_space.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/name_space.ts new file mode 100644 index 0000000000000000000000000000000000000000..dedd915898aac5749dcb668a704bbf2672e31a0e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/name_space.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: > + When an expression is an IdentifierReference, the expression refers to the most nested namespace, class, enum, function, variable, + or parameter with that name whose scope (section 2.4) includes the location of the reference. + The type of such an expression is the type associated with the referenced entity: + For a namespace, the object type associated with the namespace instance + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +namespace MyNamespace { + export interface Person { + name: string; + age: number; + } + export function greet(person: Person) { + Assert.equal(person.name, "John"); + Assert.equal(person.age, 30); + } +} +const person: MyNamespace.Person = { + name: "John", + age: 30, +}; +MyNamespace.greet(person); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/parameter.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/parameter.ts new file mode 100644 index 0000000000000000000000000000000000000000..050218eabaf7472a27a71c46b000fe435fd306e8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/parameter.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: > + For a parameter, the type of the parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class paramMethod { + person: string; + age: number; + constructor(person: string, age: number) { + this.person = person; + this.age = age; + Assert.isString(person); + Assert.isNumber(age); + } +} +let newparam = new paramMethod("xiaoli", 18); +Assert.equal(newparam.age, 18); +Assert.equal(newparam.person, "xiaoli"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/variable.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/variable.ts new file mode 100644 index 0000000000000000000000000000000000000000..b95d6d191a083eb27b148ba652a16c17fc978686 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/identifiers/variable.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: > + For a variable, the type of the variable. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let uname: string = "Runoob"; +let score1: number = 50; +Assert.isString(uname); +Assert.isNumber(score1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/boolean.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/boolean.ts new file mode 100644 index 0000000000000000000000000000000000000000..cfd5252d24f1a02886f3b5e69fe574b0e9c96fe4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/boolean.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. + */ +/**--- + description: The type of the literals true and false is the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +Assert.equal(typeof false, "boolean"); +Assert.equal(typeof true, "boolean"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/null.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/null.ts new file mode 100644 index 0000000000000000000000000000000000000000..f014d23c13ba0f40a7cede09a5c62b740b22a1f8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/null.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. + */ +/**--- + description: > + The type of the null literal is the Null primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +Assert.isTrue(typeof null === "object"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/number.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/number.ts new file mode 100644 index 0000000000000000000000000000000000000000..070870d0f5146407165e2bce961da67ab2f2fffd --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/number.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. + */ +/**--- + description: > + The type of numeric literals is the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +Assert.equal(typeof 1, "number"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/regexp.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/regexp.ts new file mode 100644 index 0000000000000000000000000000000000000000..bb9d37851625149c44ad25225e87ac8312b735d5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/regexp.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. + */ +/**--- + description: > + The type of regular expression literals is the global interface type 'RegExp'. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +Assert.isTrue(/hello world/ instanceof RegExp); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/string.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/string.ts new file mode 100644 index 0000000000000000000000000000000000000000..3a97e72e587393ea0861112f87937b34023736d5 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/literals/string.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: > + The type of string literals is the String primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +Assert.isString("Hello world"); +Assert.isString("kitty"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..e23968a8c98df1c6d1b5fab16e508771a5049d7e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_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: > + If the object literal is contextually typed and the contextual type contains a property with a matching name, + the property assignment is contextually typed by the type of that property. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let obj1: { + str: string; + num: number; + bool: boolean; +} = { + str: 'string', + num: 5, + bool: true +} +Assert.isString(obj1.str); +Assert.isNumber(obj1.num); +Assert.isBoolean(obj1.bool); + +interface I{ + num: number; + str: string; +} +let obj2 = {} as I; +obj2.num = 10; +obj2.str = 'string'; +Assert.isNumber(obj2.num); +Assert.isString(obj2.str); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..94500c72a79cc37da382f397a8fc674199d71733 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_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: > + if the object literal is contextually typed, + if the contextual type contains a numeric index signature, + and if the property assignment specifies a numeric property name, + the property assignment is contextually typed by the type of the numeric index signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let obj1: { + 1: string; + [key: number]: string; +} = { + 1: 'string', + 2: 'number', + 3: 'boolean' +} +Assert.isString(obj1[2]); +Assert.isString(obj1[3]); + +interface I{ + 1: string; + [key: number]: string; +} +let obj2 = {} as I; +obj2 = { + 1: 'string', + 2: 'number', + 3: 'boolean' +} +Assert.isString(obj2[2]); +Assert.isString(obj2[3]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..0d7ec34ce5500186a0660d4cd41d554393a898d6 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_3.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: > + If the object literal is contextually typed and the contextual type contains a string index signature, + the property assignment is contextually typed by the type of the string index signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let obj1: { + 'str': string; + [key: string]: string; +} = { + 'str': 'string', + 'num': 'number', + 'bool': 'boolean' +} +Assert.isString(obj1['num']); +Assert.isString(obj1['bool']); + +interface I{ + 'str': string; + [key: string]: string; +} +let obj2 = {} as I; +obj2 = { + 'str': 'string', + 'num': 'number', + 'bool': 'boolean' +} +Assert.isString(obj2['num']); +Assert.isString(obj2['bool']); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..6cc575365a75b768f74c8779aff542404034c812 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_4.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 property assignment is processed without a contextual type + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Iperson { + name: String; + age: Number; + gender?: string; + sayName(): void; +} +const P: Iperson = { + name: "Tom", + age: 21, + gender: "male", + sayName() { + return this.name; + }, +}; +Assert.equal(P.gender, "male"); +P.gender = "female"; +Assert.equal(P.gender, "female"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..3613783b55482296d3c03c0ec875b5a991b6f46f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_5.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: > + A get accessor declaration is processed in the same manner as an ordinary function declaration (section 6.1) with no parameters. + A set accessor declaration is processed in the same manner as an ordinary function declaration with a single parameter and a Void return type. + When both a get and set accessor is declared for a property: + If both accessors include type annotations, the specified types must be identical. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Obj { + id: number; + name: string; + get gid(): number; + set sid(id: number); +} +let obj1: Obj = { + id: 1, + name: "obj1", + get gid() { + return this.id + }, + set sid(id: number) { + this.id = id; + } +} +Assert.equal(obj1.id, 1); +obj1.sid = 2; +Assert.equal(obj1.gid, 2); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..3fb79b27f7859b02c8b1718c01f7ad1b0c266c98 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_6.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: > + If a get accessor is declared for a property, + the return type of the get accessor becomes the type of the property. + If only a set accessor is declared for a property, + the parameter type of the set accessor becomes the type of the property. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class C{ + private mem; + constructor(mem: string) { + this.mem = mem; + } + get Mem(): string { + return this.mem; + } + set Mem(mem: string) { + this.mem = mem; + } +} +let c = new C('a'); +Assert.isString(c.Mem); diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..f364ccedab1ee7337716180f481faaf3a307dd9a --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_7.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: > + When an object literal is contextually typed by a type that includes a string/numeric index signature, + the resulting type of the object literal includes a string/numeric index signature with the union type + of the types of the properties declared in the object literal, or the Undefined type if the object literal is empty. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface I1{ + 1: string; + [key: string]: string; +} +let obj1: I1 = { + 1: 'number', + 'str': 'string' +} +Assert.isString(obj1[1]); +Assert.isString(obj1['str']); +let obj2: I1 = { + 1: 'number' +} +Assert.isString(obj2[1]); +Assert.isUndefined(obj2['str']); + +interface I2{ + 'string': string; + [key: number]: string; +} +let obj3: I2 = { + 'string': 'number', + 1: 'string' +} +Assert.isString(obj3['string']); +Assert.isString(obj3[1]); +let obj4: I2 = { + 'string': 'number' +} +Assert.isString(obj4['string']); +Assert.isUndefined(obj4[1]); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_8.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..663bb91de2d5f08f12d914b25c7fc63c0fe9debf --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_8.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: > + A shorthand property assignment. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface I1 { + 1: 1; + 2: number; + f1: () => number; + f2(): number; +} +let obj1: I1 = { + 1: 1, + 2: 1, + f1() { + return 2; + }, + f2() { + return 2; + }, +} +Assert.equal(obj1[1], 1); +Assert.equal(obj1[2], 1); +Assert.equal(obj1.f1(), 2); +Assert.equal(obj1.f2(), 2); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_9.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..ea26e43b68c02d8b109c67d6a35b35bc7f7d5056 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/object_literal/object_literal_9.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: > + If the PropertyName of a property assignment is a computed property name. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const x1: "x1" = "x1"; +const x2: "x2" = "x2"; +const x3 = Symbol("x3"); +interface Obj { + [x1](): string; + [x2]: string; + [x3]: string; +} +let obj1: Obj = { + x1() { + return "xx1"; + }, + x2: 'xx2', + [x3]: 'xx3' +}; +let obj2 = { + [x2]: 1 +}; +let obj3: { + [x2]: string +} = { + x2: 'xxx2' +}; +Assert.equal(obj1[x1](), "xx1"); +Assert.equal(obj1[x2], "xx2"); +Assert.equal(obj2[x2], 1); +Assert.equal(obj3[x2], "xxx2"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/parentheses/parentheses.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/parentheses/parentheses.ts new file mode 100644 index 0000000000000000000000000000000000000000..b6975c3fb73ffe8ae4dfa51f002e26f8dda5dc81 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/parentheses/parentheses.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: > + A parenthesized expression has the same type and classification as the contained expression itself. + Specifically, if the contained expression is classified as a reference, so is the parenthesized expression. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let addparent = function (n1: number, n2: number): number { + return n1 + n2; +}; +let addnoparent = (n1: number, n2: number): number => n1 + n2; +let isCorrect = addparent(11, 22) === addnoparent(11, 22); +Assert.isTrue(isCorrect); +let foo: string = "hello"; +let bar: string = foo; +Assert.isString(foo); +Assert.isString(bar); +const obj = { value: "world" }; +function printValue(value: string) { + Assert.equal(value, "world"); + Assert.isString(value); +} +printValue(obj.value); + +let str1 = (37 * 12) + ((37 * 9) + ''); +Assert.equal(str1, "444333"); +Assert.isString(str1); + +let t: boolean = true; +let f: boolean = false; +let bool: boolean = t || ((f || t) && (false && t)); +Assert.isTrue(bool); + diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_access_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_access_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc245d3fd9e79d876bec9af1baeb641dd4e82c7e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_access_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: > + If name denotes an accessible apparent property (section 3.11.1) in the widened type (section 3.12) of object, + the property access is of the type of that property. Public members are always accessible, + but private and protected members of a class have restricted accessibility, as described in 8.2.2. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var sites = { + site1: "Runoob", + site2: "Google", + sayHello: function () { }, +}; +sites.sayHello = function () { + return "hello " + sites.site1; +}; +Assert.isString(sites.sayHello()); +class Person { + name: string; + age: string; + constructor(name: any, age: any) { + this.name = name; + this.age = age; + } + greet() { } +} +const person1 = new Person("Alice", 30); +const person2 = { name: "Bob", age: 35 }; +person1["greet"](); +Assert.equal(person1.name, "Alice"); +Assert.equal(person2.name, "Bob"); +Assert.equal(person1.age, 30); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_any_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_any_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..167baf148f89d99415a5190db3843c8a4dbc7545 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_any_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 object is of type Any, any name is permitted and the property access is of type Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let myVariable: any = { name: "John", age: 30 }; +Assert.isString(myVariable.name); +myVariable.name = 42; +Assert.isNumber(myVariable.name); + +Assert.isNumber(myVariable.age); +myVariable.age = "Hello World"; +Assert.isString(myVariable.age); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_any_type_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_any_type_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..87e41c23ce535d865e5fc4cc88161d193c4c3555 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_any_type_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: > + If index is of type Any, the String or Number primitive type, + or an enum type, the property access is of type Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface MyObject { + [key: string]: number; +} +const obj: MyObject = { + prop1: 1, + prop2: 2, + prop3: 3, +}; +const value2 = obj[0]; +Assert.equal(value2, undefined); +enum MyEnum { + Prop1 = "prop1", + Prop2 = "prop2", + Prop3 = "prop3", +} +const value3 = obj[MyEnum.Prop3]; +Assert.equal(value3, 3); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_apprent_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_apprent_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..0cfac703a8b604c83159d7075d4775896ae5ef5f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_apprent_5.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 object has an apparent string index signature and index is of type Any, + the String or Number primitive type, or an enum type, the property access is of the type of that index signature. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface ExampleObject { + [key: string]: string; +} +const myObject: ExampleObject = { + foo: "bar", + baz: "qux", +}; +const myIndex: any = "foo"; +const myValue = myObject[myIndex]; +Assert.isString(myValue); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_index_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_index_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a09d000a159ca77ed80cabc9fd04a7070053ae7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_index_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: > + If object has an apparent numeric index signature and index is of type Any, + the Number primitive type, or an enum type, the property access is of the type of that index signature + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface MyObj { + [index: number]: string; +} +const obj: MyObj = { + 0: "foo", + 1: "bar", + 2: "baz", +}; +Assert.equal(obj[0], "foo"); +Assert.equal(obj[1], "bar"); +Assert.equal(obj[2], "baz"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_string_number_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_string_number_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e4293dba483a1bfa76950d0c860bd8fd5df9098 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/object_string_number_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: > + If index is a string literal or a numeric literal and object has an apparent property with the name given by that literal, + the property access is of the type of that property. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +const sites = { + siteName: "Runoob", + site2: 999, + sayHello: function () { }, +}; +Assert.isString(sites["siteName"]); +Assert.isNumber(sites["site2"]); +Assert.isFunction(sites["sayHello"]); +interface Person { + name: string; + age: number; + city: string; +} +const person: Person = { + name: "Alice", + age: 30, + city: "New York", +}; +Assert.equal(person["name"], "Alice"); +Assert.equal(person["city"], "New York"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/property_access_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/property_access_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5dcfd232d90e6d59c59221acaa9c4e9ea362bb58 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/property_access_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: > + Properties are strongly typed when accessed using bracket notation with the literal representation of their name. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var myType = { + h_str: 'string', + h_bool: true +}; +var h_s = myType['h_str']; +Assert.isString(h_s); +var h_b = myType['h_bool']; +Assert.isBoolean(h_b); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/property_access_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/property_access_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3b624b2cdc34831954ff1fec797c57bd368c7204 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/property_access/property_access_2.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: > + Tuple types assign numeric names to each of their elements and elements are therefore strongly typed + when accessed using bracket notation with a numeric literal. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var myArry: [number, string, boolean] = [10, 'string', true]; +var h_n = myArry[0]; +Assert.isNumber(h_n); +var h_s = myArry[1]; +Assert.isString(h_s); +var h_b = myArry[2]; +Assert.isBoolean(h_b); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/template_literals/template_literals.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/template_literals/template_literals.ts new file mode 100644 index 0000000000000000000000000000000000000000..beaa1deeaef96bd69a99913978162d3ca99f2028 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/template_literals/template_literals.ts @@ -0,0 +1,80 @@ +/* + * 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: > + Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates. + Template literals are sometimes informally called template strings, because they are used most commonly for string interpolation (to create strings by doing substitution of placeholders). + However, a tagged template literal may not result in a string; it can be used with a custom tag function to perform whatever operations you want on the different parts of the template literal. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from "../../../../suite/assert.js"; + +function hiStr(str: string | TemplateStringsArray) { + return 'hi ' + str; +} +let hi = hiStr`TypeScript`; +Assert.equal(hi, 'hi TypeScript'); + +let bool1 = `\`` == "\`"; +Assert.isTrue(bool1); + +let bool2 = `\${37*3}` == '${37*3}'; +Assert.isTrue(bool2); + +let bool3 = `${bool1}` == 'true'; +Assert.isTrue(bool3); + +let str1 = hiStr`Type +Script`; +Assert.equal(str1, `hi Type +Script`); +Assert.equal(str1, 'hi Type\nScript'); + +function funF(str: string | TemplateStringsArray) { + let str2 = `${str}\n${str}`; + function funFF(str1: string | TemplateStringsArray, ...strs: string[] | TemplateStringsArray[]) { + let s = `${str2}\n${str1}`; + for (let i = 0; i < strs.length; i++) { + s = s + `\n` + strs[i]; + } + return s; + } + return funFF; +} +let s1 = funF`A``B`; +Assert.equal(s1, `A +A +B`); +let s2 = funF`A``B` + `C`; +Assert.equal(s2, `A +A +BC`); +let s3 = funF`A`(`B`, `C`); +Assert.equal(s3, `A +A +B +C`); + +function sRaw(str: TemplateStringsArray) { + return str.raw[0]; +} +let s4 = sRaw`ABC\nDEF`; +Assert.equal(s4, 'ABC\\nDEF'); + +let s5 = String.raw`AB\n${s4}`; +Assert.equal(s5, 'AB\\nABC\\nDEF'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_comma_operator/the_comma_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_comma_operator/the_comma_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..80cc709cfd7c38e193c4a854048c89072f893120 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_comma_operator/the_comma_operator.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 comma operator permits the operands to be of any type and produces a result that is of the same type as the second operand. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let x: number = 20; +let y: boolean = true; +let z: string = 'a'; +function add(arg1: number, arg2: number) { + return arg1 + arg2; +} +let com1 = (x++, y); +let com2 = (x++, z = z + 'b'); +let com3 = (x++, add(3, 5)); +Assert.isBoolean(com1); +Assert.equal(com1, true); +Assert.isString(com2); +Assert.equal(com2, 'ab'); +Assert.isNumber(com3); +Assert.equal(com3, 8); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_conditional_operator/the_conditional_operator_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_conditional_operator/the_conditional_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..01e70a83643ca998d7c61bd006302856ab7edc57 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_conditional_operator/the_conditional_operator_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: > + In a conditional expression of the form 'test ? expr1 : expr2', the test expression may be of any type. + The type of the result is the union type of the types of expr1 and expr2. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var x: number = 10 +var y: string = '5' +var z = Math.random() < 0.5 ? x + y : y && x +if (typeof z === 'number') { + z++ + Assert.equal(z, 11); +} +else { + Assert.equal(z, '105'); +}; \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_conditional_operator/the_conditional_operator_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_conditional_operator/the_conditional_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d722c0719d20f3cd93460d07f7c9a16052c86d9f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_conditional_operator/the_conditional_operator_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: > + If the conditional expression is contextually typed, expr1 and expr2 are contextually typed by the same type. + Otherwise, expr1 and expr2 are not contextually typed. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let sum = function (x: number, y: number) { + return x + y; +} +let average = function (a: number, b: number) { + return (a + b) / 2; +} +let flag = 0; +let rela1: number = flag < 0.5 ? sum(5, 7) : average(6, 9); +Assert.isNumber(rela1); +Assert.equal(rela1, 12); +flag = 1; +let rela2: number = flag < 0.5 ? sum(5, 7) : average(6, 9); +Assert.isNumber(rela2); +Assert.equal(rela2, 7.5); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6579559ced877b85fb2c8060eaace12e1cc211a7 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_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: > + If C is of type Any, any argument list is permitted and the result of the operation is of type Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let C: any; +C = function (this: any, x: number, y: number) { + this.sum = x + y; +}; +let myObject = new C(2, 3); +Assert.equal(myObject.sum, 5); +myObject = 5; +Assert.isNumber(myObject); +myObject = 'a'; +Assert.isString(myObject); +myObject = true; +Assert.isBoolean(myObject); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..42c673b0a37b8867ce798e9fe1c6721d3476c4ff --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_2.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: > + If C has one or more apparent construct signatures (section 3.11.1), the expression is processed in the same manner as a function call, + but using the construct signatures as the initial set of candidate signatures for overload resolution. + The result type of the function call becomes the result type of the operation. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Person { + constructor(public name: string, public age: number) { } +} +interface Animal { + speak(): void; +} +class Dog implements Animal { + speak() { + return "Woof!"; + } +} +const p = new Person("Alice", 30); +const d = new Dog(); +Assert.isString(p.name); +Assert.equal(d.speak(), "Woof!"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7ee5e319d5ced5e28e6f8f5adff9d0e74d067795 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_3.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 C has no apparent construct signatures but one or more apparent call signatures, the expression is processed as a function call. + A compile-time error occurs if the result of the function call is not Void. The type of the result of the operation is Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Person { + sayHello() { + return "Hello,world"; + } +} +let person: Person = new Person(); +Assert.equal(person.sayHello(), "Hello,world"); +let person2: any = new Person(); +person2.sayHello(); +Assert.equal(person2.sayHello(), "Hello,world"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..966c697a6a4460affd28427ecb9aecdf891dc868 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_new_operator/new_operator_4.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: > + new C + new C ( ... ) + new C < ... > ( ... ) + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class C { + x: number = 1; +} +let c = new C; +Assert.equal(c.x, 1); +class Person { + name: string; + age: number; + constructor(name: string, age: number) { + this.name = name; + this.age = age; + } +} +const john = new Person("john", 30); +Assert.isString(john.name, "john"); +class Box { + contents: T; + constructor(value: T) { + this.contents = value; + } +} +const myBox = new Box("hello"); +Assert.equal(myBox.contents, "hello"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/arguments.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/arguments.ts new file mode 100644 index 0000000000000000000000000000000000000000..965826348024df877cf6564b52e652c725187f79 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/arguments.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: > + Type arguments cannot be explicitly specified in a super call. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class SuperClass { + value: T; + constructor(value: T) { + this.value = value; + } +} +class SubClass extends SuperClass { + constructor(value: T) { + super(value); + } +} +const sub = new SubClass("hello"); +Assert.equal(sub.value, "hello"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/gengric_extends.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/gengric_extends.ts new file mode 100644 index 0000000000000000000000000000000000000000..3243840b97a30751f2fb2accced7e8cff0e1ebde --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/gengric_extends.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: > + If the base class is a generic class, + the type arguments used to process a super call are always those specified in the extends clause that references the base class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class BaseClass { + constructor(public value: T) { } +} +class SubClass extends BaseClass { + constructor(value: T) { + super(value); + } +} +class StringSubClass extends SubClass { + constructor() { + super("hello"); + } +} +const sub = new StringSubClass(); +Assert.equal(sub.value, "hello"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/super_calls.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/super_calls.ts new file mode 100644 index 0000000000000000000000000000000000000000..b64f05916a0766e995872792b5e05cea2b39bd7f --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_calls/super_calls.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: > + The super keyword can be used in expressions to reference base class properties and the base class constructor. + Super calls consist of the keyword super followed by an argument list enclosed in parentheses. Super calls are only permitted in constructors of derived classes. + A super call invokes the constructor of the base class on the instance referenced by this. + A super call is processed as a function call (section 4.15) using the construct signatures of the base class constructor function type + as the initial set of candidate signatures for overload resolution. + The type of a super call expression is Void. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Animal { + private name: string; + constructor(name: any) { + this.name = name; + } + speak() { + return `${this.name} makes a noise`; + } +} +class Dog extends Animal { + private breed: string; + constructor(name: any, breed: any) { + super(name); + this.breed = breed; + } + extendsSpeak() { + super.speak(); + Assert.isString(super.speak(), "Fodo makes a noise"); + return `${this.breed}`; + } +} +const d = new Dog("Fido", "Golden Retriever"); +d.extendsSpeak(); +Assert.isString(d.extendsSpeak(), "Golden Retriever"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_property_access/super_property_access_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_property_access/super_property_access_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5dce4ac822bb4d82fb4b8f412b41e70849ced68b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_property_access/super_property_access_1.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 constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance, + a super property access is permitted and must specify a public instance member function of the base class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Animal { + public age: number = 10; + constructor() { } + public makeSound(): string { + return "the animal makes"; + } + static weight: number = 0; +} +class Dog extends Animal { + + constructor() { + super(); + this.age = 20; + } + public makeSound(): string { + super.makeSound(); + Assert.equal(super.makeSound(), "the animal makes"); + return "the dog barks"; + } + public Animalweight(): number { + return Animal.weight; + } + get _age(): number { + return this.age + } + set _age(age: number) { + this.age = age; + } +} +let myDog = new Dog(); +myDog.makeSound(); +Assert.equal(myDog.makeSound(), "the dog barks"); +Assert.equal(myDog.age, 20); +Assert.equal(myDog.Animalweight(), 0); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_property_access/super_property_access_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_property_access/super_property_access_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d7c0f1dc7af81081554d8c40f4e1d6e99812a6fa --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/the_super_keyword/super_property_access/super_property_access_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: > + In a static member function or static member accessor where this references the constructor function object of a derived class, + a super property access is permitted and must specify a public static member function of the base class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../../suite/assert.js' + +class Base { + static greet() { + return "hello world"; + } +} +class Derived extends Base { + static greet() { + super.greet(); + Assert.equal(super.greet(), "hello world"); + return "hola,mundo"; + } +} +Derived.greet(); +Assert.equal(Derived.greet(), "hola,mundo"); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c1c78b95c910c2fe90b46635577c410b7dacebd4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_1.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: > + In a constructor, instance member function, instance member accessor, + or instance member variable initializer, this is of the this-type (section 3.6.3) of the containing class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class C { + public mem: string; + private num: number; + constructor(mem: string, num: number) { + this.mem = mem; + this.num = num; + Assert.isObject(this); + } + func() { + return this; + } + get getNum() { + Assert.isObject(this); + return this.num; + } + set setNum(num: number) { + this.num = num; + } +} +let c: C = new C('a', 10); +c.getNum; +c.setNum = 11; +Assert.isObject(c.func()); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..af043bd290297286e312430b1366c5cfb4f50a63 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_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: > + In a static member function or static member accessor, the type of this is the constructor function type of the containing class. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class C{ + static mem: string = 'a'; + static get getMem() { + Assert.isFunction(this); + return this.mem; + } + static set setMem(mem: string) { + this.mem = mem; + } + static func() { + return this; + } +} +C.getMem; +Assert.isFunction(C.func()); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..f6038164887006387b3dd5d9195a233ebb9fa57b --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_3.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: > + In a function declaration or a function expression, this is of type Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Str { + id: number; +} +interface Obj { + name: string; + getName(this: Obj): string; +} +interface Obj2 { + name: string; + getName(this: Str): number; +} +let obj: Obj = { + name: "obj", + getName() { + return this.name; + }, +} +let obj2: Obj2 & Str = { + name: "obj2", + id: 1, + getName() { + return this.id; + } +} +Assert.equal(obj.getName(), "obj"); +Assert.equal(obj2.getName(), 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..18409de810992214c873b5e44375ca4851d75ed1 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/this_key_word/this_key_word_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: > + The type of this in an expression depends on the location in which the reference takes place:In the global namespace, this is of type Any. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' +let obj = { + a: 1024, + ADD(a: number) { + return this.a + a; + }, + getType() { + return typeof this; + }, + getThis() { + return this; + } +} +Assert.equal(obj.ADD(1), 1025); +Assert.equal(obj.getType(), 'object'); +let obj2 = obj.getThis(); +Assert.equal(JSON.stringify(obj2), '{"a":1024}'); + +const t1 = this; +Assert.isUndefined(t1); + +const t2 = globalThis; +Assert.equal(t2.JSON.stringify(obj.getThis()), JSON.stringify(obj2.getThis())); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..17020e1b08acf89a581d1287cf1e648c7e184006 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_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: > + Type assertions check for assignment compatibility in both directions. + Thus, type assertions allow type conversions that might be correct, but aren't known to be correct. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class P{ } +class C extends P { } +function func(str: string): P { + if (str === "string") { + return new C(); + } + return C; +} +let c = func("string"); +Assert.equal(JSON.stringify(c), '{}'); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..e0a3eac6928f1e2e2183f34480efb29e6e1c81e4 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_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: > + type assertions are not checked at run-time and it is up to the programmer to guard against errors, + for example using the instanceof operator + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Foo { + foo: number; + cname: string; + constructor(foo: number = 123, cname: string = 'Foo') { + this.foo = foo; + this.cname = cname; + } +} +class Bar extends Foo { + bar: number; + cname: string; + constructor(bar: number = 1024, cname: string = 'Bar') { + super(); + this.bar = bar; + this.cname = cname; + } +} +function toFoo(): Foo { + return new Bar(); +} +let foo: Foo = toFoo(); +let bar: Bar = toFoo(); +function toBar(arg: Foo | Bar): Bar | string { + let bar: Bar; + if (arg instanceof Bar) { + bar = arg; + return bar; + } + return "Can not"; +} +let foo1 = toBar(new Foo()); +let foo2 = toBar(foo); +let bar1 = toBar(bar); +Assert.equal(foo1, 'Can not'); +Assert.equal(JSON.stringify(foo2), '{"foo":123,"cname":"Bar","bar":1024}'); +Assert.equal(JSON.stringify(foo2), JSON.stringify(bar1)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..076898c01cfca1663639f0bcc11bb731b3ed5a88 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_assertion/type_assertion_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: > + In a type assertion expression of the form < T > e, e is contextually typed by T and the resulting type of* e* is required to be assignable to T, + or T is required to be assignable to the widened form of the resulting type of e, or otherwise a compile-time error occurs. The type of the result is T. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function getLength(something: string | number): number { + if ((something).length) { + return (something).length + } else { + return something.toString().length + } +} +Assert.equal(getLength('length'), 6); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_1.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..08b585694dc1cf251346edbac4921d4971e3c188 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_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: > + Type guards are particular expression patterns involving the 'typeof' and 'instanceof' operators + that cause the types of variables or parameters to be narrowed to more specific types. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function fun(x: boolean | number | string) { + if (typeof x === 'string') { + Assert.isString(x); + } + if (typeof x === 'boolean') { + x = true; + Assert.isTrue(x); + } + if (typeof x === 'number') { + Assert.isNumber(x); + } +} +fun('string'); +fun(10); +fun(false); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_10.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..c96d6494826baf243f2ff1fe47c574d657898f39 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_10.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 type guard of the form !expr, + when true, narrows the type of x by expr when false, + or when false, narrows the type of x by expr when true. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function func(x: string | number) { + if (!(typeof x === "string")) { + Assert.isNumber(x); + return x + 1; + } + else { + Assert.isString(x); + return x.length; + } +} +let a = func(10); +Assert.equal(a, 11); +let b = func('s'); +Assert.equal(b, 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_11.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_11.ts new file mode 100644 index 0000000000000000000000000000000000000000..9fcd82bbf2146dc3e223c8ae5de3508f0ef45a91 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_11.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 type guard of the form expr1 && expr2, + when true, narrows the type of x by expr1 when true and then by expr2 when true, or + when false, narrows the type of x to T1 | T2, where T1 is the type of x narrowed by expr1 when false, + and T2 is the type of x narrowed by expr1 when true and then by expr2 when false. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function func(x: string | number | undefined) { + if (typeof x === "string" && typeof x === "number") { + return undefined; + } + else { + return x; + } +} +let a = func(10); +Assert.isNumber(a); +let b = func('s'); +Assert.isString(b); +let c = func(undefined); +Assert.isUndefined(c); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_12.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_12.ts new file mode 100644 index 0000000000000000000000000000000000000000..23569fbadb74f49959194768624c5b6862ea77f8 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_12.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 type guard of the form expr1 || expr2, + when true, narrows the type of x to T1 | T2, where T1 is the type of x narrowed by expr1 when true, + and T2 is the type of x narrowed by expr1 when false and then by expr2 when true, or + when false, narrows the type of x by expr1 when false and then by expr2 when false. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function func(x: string | number | undefined) { + if (typeof x === "string" || typeof x === "number") { + return x; + } + else { + return undefined; + } +} +let a = func(10); +Assert.isNumber(a); +let b = func('s'); +Assert.isString(b); +let c = func(undefined); +Assert.isUndefined(c); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_13.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_13.ts new file mode 100644 index 0000000000000000000000000000000000000000..17c6536000674d86c9bcda5c4342dde976183014 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_13.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 type guard of any other form has no effect on the type of x. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Person { + name: string + age: number + public constructor(name: string, age: number) { + this.name = name + this.age = age + } +} +class Animal { + height: number + weight: number + public constructor(height: number, weight: number) { + this.height = height + this.weight = weight + } +} +function func(arg: Person | Animal) { + if ('age' in arg) { + Assert.isString(arg.name) + } + if ('height' in arg) { + Assert.isNumber(arg.height) + } +} +let p = new Person('x', 18); +func(p); +let a = new Animal(200, 180); +func(a); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_2.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b8027f7cda49941640c43d5e3e44c732efa856d --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_2.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: > + In the false branch statement of an 'if' statement, + the type of a variable or parameter is narrowed by a type guard in the 'if' condition when false, + provided no part of the 'if' statement contains assignments to the variable or parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function h_func(h_x: number | string) { + if (!(typeof h_x === 'string')) { + Assert.isNumber(h_x); + return h_x; + } + else { + return h_x.length + } +} +let a = h_func(10); +Assert.equal(a, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_3.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..d092aec559884a0561476c1dfc3fad81764e1caa --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_3.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: > + In the true expression of a conditional expression, + the type of a variable or parameter is narrowed by a type guard in the condition when true, + provided no part of the conditional expression contains assignments to the variable or parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function func(arg: string | (() => string)) { + let x = typeof arg == "string" ? arg : arg(); + return x; +} +let result = func("abc"); +Assert.isString(result); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_4.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..1497787e835d6da66ba914901741f8c8f3f0535e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_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: > + In the false expression of a conditional expression, + the type of a variable or parameter is narrowed by a type guard in the condition when false, + provided no part of the conditional expression contains assignments to the variable or parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function func(arg: string | (() => string)) { + var x = typeof arg !== "string" ? arg() : arg; + return x; +} +let result = func("abc"); +Assert.isString(result); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_5.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..8eaf5a06cf2e04237e2a6896014c2711bc384c5e --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_5.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: > + In the right operand of a && operation, + the type of a variable or parameter is narrowed by a type guard in the left operand when true, + provided neither operand contains assignments to the variable or parameter. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function fun(obj: any) { + typeof obj === "number" && obj + Assert.isNumber(obj) +} +fun(12); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_6.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..0040c18bebeda3fd74126b7e167651715a08c3f9 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_6.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 type guard of the form 'x instanceof C', where x is not of type Any, + C is of a subtype of the global type 'Function', and C has a property named 'prototype', + when true, narrows the type of x to the type of the 'prototype' property in C provided it is a subtype of the type of x, + or, if the type of x is a union type, removes from the type of x all constituent types that aren't subtypes of the type of the 'prototype' property in C, + or when false, has no effect on the type of x. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +class Person { + height: number + age: number + public constructor(height: number, age: number) { + this.height = height + this.age = age + } +} +class Animal { + height: number + weight: number + public constructor(height: number, weight: number) { + this.height = height + this.weight = weight + } +} +function func(arg: Person | Animal) { + if (arg instanceof Person) { + return arg.age = 18 + } + if (arg instanceof Animal) { + return arg.weight = 300 + } +} +var p = new Person(150, 18) +Assert.equal(func(p), 18) +var a = new Animal(200, 180) +Assert.equal(func(a), 300); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_7.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..7d3ef1a40d332968ac840df39b12338199f3b250 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_7.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 type guard of the form typeof x === s, where s is a string literal with the value 'string', 'number', or 'boolean', + when true, narrows the type of x to the given primitive type provided it is a subtype of the type of x, + or, if the type of x is a union type, removes from the type of x all constituent types that aren't subtypes of the given primitive type, + or when false, removes the primitive type from the type of x. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function func(x: string | number) { + if (typeof x === "string") { + Assert.isString(x); + return x.length; + } + else { + Assert.isNumber(x); + return x + 1; + } +} +let a = func(10); +Assert.equal(a, 11); +let b = func('s'); +Assert.equal(b, 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_8.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..53c3fab07316ff45064f484b7b4abb1567a0d858 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_8.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 type guard of the form typeof x === s, where s is a string literal with any value but 'string', 'number', or 'boolean', + when true, if x is a union type, removes from the type of x all constituent types that are subtypes of the string, number, or boolean primitive type, + or when false, has no effect on the type of x. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function f1(x: string | number | undefined) { + if (typeof x === "undefined") { + return undefined + } + else { + return x + } +} +var a = f1(10) +Assert.isNumber(a) +var b = f1('s') +Assert.isString(b) +var c = f1(undefined) +Assert.isUndefined(c) +function f2(x: string | number | boolean) { + if (typeof x === "undefined") { + return undefined + } + else { + return x + } +} +var a1 = f2(10) +Assert.isNumber(a1) +var b1 = f2('s') +Assert.isString(b1) +var c1 = f2(true) +Assert.isBoolean(c1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_9.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..c026b9e2f7628b6b358564543398e0a5c5a58e56 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/type_guards/type_guards_9.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 type guard of the form typeof x !== s, where s is a string literal, + when true, narrows the type of x by typeof x === s when false, + or when false, narrows the type of x by typeof x === s when true. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +function func(x: string | number) { + if (typeof x !== "string") { + Assert.isNumber(x); + return x + 1; + } + else { + Assert.isString(x); + return x.length; + } +} +let a = func(10); +Assert.equal(a, 11); +let b = func('s'); +Assert.equal(b, 1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_!_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_!_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..a897e3afac9c5d5bd3ddb9c0663d5d4a7ea40f42 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_!_operator.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: > + The ! operator permits its operand to be of any type and produces a result of the Boolean primitive type. + Two unary ! operators in sequence can conveniently be used to convert a value of any type to the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var num1: number = 20 +var num2: number = 90 +var res = !((num1 > 50) && (num2 > 80)) +Assert.isTrue(res) +function func(): any { +} +var b = !!func() +Assert.isFalse(b); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_++_and_--_operators.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_++_and_--_operators.ts new file mode 100644 index 0000000000000000000000000000000000000000..a3a4b162f033220605c2b18d8ef70f05140ebd61 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_++_and_--_operators.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 ++ and -- operators in prefix or postfix form, require their operand to be of type Any, the Number primitive type, or an enum type, + and classified as a reference. They produce a result of the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var a: any = '10'; +a++; +a--; +--a; +++a; +Assert.isNumber(a); +Assert.equal(a, 10); +enum e { + A, + B, + C, + D +} +let b = e.A +b++; +b--; +++b; +--b; +Assert.isNumber(b); +Assert.equal(b, 0); +var c: number = 10; +c++; +++c; +--c; +c--; +Assert.isNumber(c); +Assert.equal(c, 10); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_+_and_~_operators.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_+_and_~_operators.ts new file mode 100644 index 0000000000000000000000000000000000000000..d33f144c3c178aab24faa8f2cb13833c3a15ff1c --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_+_and_~_operators.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 +, -, and ~ operators permit their operand to be of any type and produce a result of the Number primitive type. + The unary + operator can conveniently be used to convert a value of any type to the Number primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +var a: any = 'a' +a = a + 1 +a = a - 1 +a = ~a +Assert.isNumber(a); +Assert.equal(a, -1); +function func() { } +var n = +func(); +Assert.isTrue(Number.isNaN(n)); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_delete_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_delete_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..159678417b37b1aacc16829c9b16ee1d1696a349 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_delete_operator.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: > + The 'delete' operator takes an operand of any type and produces a result of the Boolean primitive type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +interface Foo { + prop1?: string + prop2?: boolean + prop3?: number +} +let obj: Foo = { + prop1: 'value', + prop2: true, + prop3: 20 +} +Assert.isBoolean(delete obj.prop1); \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_typeof_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_typeof_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..06eb874eda6938ef1e96ff6faf789d4bca85ef22 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_typeof_operator.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: > + The 'typeof' operator takes an operand of any type and produces a value of the String primitive type. In positions where a type is expected, 'typeof' can also be used in a type query to produce the type of an expression. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let aa = 1 +let bb = typeof aa +Assert.equal(bb, 'number') +let cc: typeof aa = 2 +Assert.equal(cc, 2) \ No newline at end of file diff --git a/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_void_operator.ts b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_void_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..501fb22f61a4d602ee664a2b2f6f1a1298413a05 --- /dev/null +++ b/es2panda/test/ts_extra_tests/test_ts_cases/spec/expressions/unary_operators/the_void_operator.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 'void' operator takes an operand of any type and produces the value 'undefined'. The type of the result is the Undefined type. + module: ESNext + isCurrent: true + ---*/ + + +import { Assert } from '../../../../suite/assert.js' + +let num: any = 10 +num = 'string' +let un = void num +Assert.isUndefined(un); \ No newline at end of file