1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16namespace NumericSemanticsReport { 17 const a: number = 11 // NOT OK 18 const b = 12 // NOT OK 19 const c = 13.0 // NOT OK 20 const d: number = 14.0 21 const e: number = 15 // NOT OK 22 console.log('' + 1/2) // NOT OK 23} 24 25namespace NumericSemanticsDone { 26 const a: number = 11.0 27 const b: number = 12.0 28 const c: number = 13.0 29 const d: number = 14.0 30 const e: number = 15.0 31 console.log('' + 1.0/2.0) 32} 33 34namespace NoNumericSemantics { 35 interface X1 { "name": number, 2: number} 36 interface X2 { "name": number, _2: number} 37 let x: X1 = {"name": 20.0, 2: 30.0} // OK 38 console.log(x[2]); // OK 39 40 let x_fix: X2 = {name: 20.0, _2: 20.0}; 41 42 let x_non = {name: 20.0, 2: 20.0}; // OK 43 44 const arr = [] 45 console.log(arr[2]); 46 47 // Number bases 48 let c5: number = 0x123; // Hexadecimal 49 let c6: number = 0o123; // Octal 50 let c7: number = 0b101; // Binary 51 52 let e1: number = 1e0; 53 let e2: number = 1E1; 54 let e3: number = 1e+1; 55 let e4: number = 1E-1; 56 let e5: number = +1e2; 57 let e6: number = -1E2; 58 59 let h: number = arr[12. as int] 60 61 enum E { 62 A = 1, 63 B = 2 64 } 65} 66 67namespace NumericSemanticsOther { 68 let e7: number = 1e4 + 11; 69} 70 71namespace BeCareful { 72 `${1/2}` 73 74} 75 76namespace NoDiffInArk1_1To1_2 { 77 const a1 = `${1/2}` // NOT OK 78 const a2 = `${1.0/2.0}` 79 const b1 = `20${20 | 21 | 22 | 23}` // NOT OK 80 const b2 = `20${20.0 | 21.0 | 22.0 | 23.0}` 81 const c1 = `20 + ${20}` // NOT OK 82 const c2 = `20 + ${20.0}` 83 console.log(a1,a2,b1,b2,c1,c2) 84 85 // Automatically delete decimal parts during bitwise operations 86 let e = 15 // NOT OK 87 let e1 = e & 3; // NOT OK 88 let e2 = e | 3; // NOT OK 89} 90 91namespace GenericTypeCase { 92 function ReturnGenericNumber<T>(a: T): T { 93 return a 94 } 95 96 function ReturnGenericArry<T>(a: T): T[] { 97 return [a] 98 } 99 100 ReturnGenericNumber(1) // NOT OK, generic type is 101 ReturnGenericNumber(true ? 1 : 2) // NOT OK 102 ReturnGenericArry(1) // NOT OK 103 104 function TestReturnGenericNumber<T>(a: T[]): T[] { 105 return a.map(item => item) // OK, not report arkts-numeric-semantic 106 } 107 108 function MapCase(a: number[]): number { 109 let groupNum: number = new Set(a.map(item => item)).size; // OK, not report arkts-numeric-semantic 110 return groupNum; 111 } 112 113 function foo<T>(v:T):T{return v} 114 foo(12)/24 // NOT OK 115 foo(12.0)/24 // NOT OK 116 117 function foo1<T,U>(v:T, u:U, b:boolean):T|U{ 118 return b ? v: u 119 } 120 foo1(12.0, 8, true)/24 // NOT OK 121 foo1(12.0, 8, false)/24 // NOT OK 122 123 console.log(foo1(12/24, 8, true)) // NOT OK 124 console.log(foo1<number,number>(12/24, 8, true)) // NOT OK 125 console.log(foo1<number,number>(12.0/24.0, 8, true)) 126}