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.0 // NOT OK 18 const b: number = 12.0 // NOT OK 19 // NOT OK 20 const c: number = 13.0 // NOT OK 21 // NOT OK 22 const d: number = 14.0 23 const e: number = 15.0 // NOT OK 24 console.log('' + 1.0/2.0) // NOT OK 25} 26 27namespace NumericSemanticsDone { 28 const a: number = 11.0 29 const b: number = 12.0 30 const c: number = 13.0 31 const d: number = 14.0 32 const e: number = 15.0 33 console.log('' + 1.0/2.0) 34} 35 36namespace NoNumericSemantics { 37 interface X1 { name: number, __2: number} 38 interface X2 { name: number, _2: number} 39 let x: X1 = {name: 20.0, __2: 30.0} // OK 40 console.log(x.__2); // OK 41 42 let x_fix: X2 = {name: 20.0, _2: 20.0}; 43 44 let x_non = {name: 20.0, 2: 20.0}; // OK 45 46 const arr = [] 47 console.log(arr[2]); 48 49 // Number bases 50 let c5: number = 0x123; // Hexadecimal 51 let c6: number = 0o123; // Octal 52 let c7: number = 0b101; // Binary 53 54 let e1: number = 1e0; 55 let e2: number = 1E1; 56 let e3: number = 1e+1; 57 let e4: number = 1E-1; 58 let e5: number = +1e2; 59 let e6: number = -1E2; 60 61 let h: number = arr[12. as int] 62 63 enum E { 64 A = 1.0, 65 B = 2.0 66 } 67} 68 69namespace NumericSemanticsOther { 70 let e7: number = 1e4 + 11.0; 71} 72 73namespace BeCareful { 74 `${1.0/2.0}` 75 76} 77 78namespace NoDiffInArk1_1To1_2 { 79 const a1 = `${1.0/2.0}` // NOT OK 80 const a2 = `${1.0/2.0}` 81 const b1 = `20${20.0 | 21.0 | 22.0 | 23.0}` // NOT OK 82 const b2 = `20${20.0 | 21.0 | 22.0 | 23.0}` 83 const c1 = `20 + ${20.0}` // NOT OK 84 const c2 = `20 + ${20.0}` 85 console.log(a1,a2,b1,b2,c1,c2) 86 87 // Automatically delete decimal parts during bitwise operations 88 let e: number = 15.0 // NOT OK 89 // NOT OK 90 let e1: number = e & 3.0; // NOT OK 91 let e2: number = e | 3.0; // NOT OK 92} 93 94namespace GenericTypeCase { 95 function ReturnGenericNumber<T>(a: T): T { 96 return a 97 } 98 99 function ReturnGenericArry<T>(a: T): T[] { 100 return [a] 101 } 102 103 ReturnGenericNumber(1.0) // NOT OK, generic type is 104 ReturnGenericNumber(true ? 1.0 : 2.0) // NOT OK 105 ReturnGenericArry(1.0) // NOT OK 106 107 function TestReturnGenericNumber<T>(a: T[]): T[] { 108 return a.map(item => item) // OK, not report arkts-numeric-semantic 109 } 110 111 function MapCase(a: number[]): number { 112 let groupNum: number = new Set(a.map(item => item)).size; // OK, not report arkts-numeric-semantic 113 return groupNum; 114 } 115 116 function foo<T>(v:T):T{return v} 117 foo(12.0)/24.0 // NOT OK 118 foo(12.0)/24.0 // NOT OK 119 120 function foo1<T,U>(v:T, u:U, b:boolean):T|U{ 121 return b ? v: u 122 } 123 foo1(12.0, 8.0, true)/24.0 // NOT OK 124 foo1(12.0, 8.0, false)/24.0 // NOT OK 125 126 console.log(foo1(12.0/24.0, 8.0, true)) // NOT OK 127 console.log(foo1<number,number>(12.0/24.0, 8.0, true)) // NOT OK 128 console.log(foo1<number,number>(12.0/24.0, 8.0, true)) 129}