From 3fb37e4f848446b7975b4bb7e7a7a98ed5a94e66 Mon Sep 17 00:00:00 2001 From: Utku Enes GURSEL Date: Mon, 21 Jul 2025 22:21:42 +0300 Subject: [PATCH] check concatArray for bound Issue: ICNUG5 Description: concatArray getting added to array-bound-check rule Signed-off-by: Utku Enes GURSEL --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 16 ++++++- .../linter/src/lib/autofixes/Autofixer.ts | 6 ++- ets2panda/linter/src/lib/utils/TsUtils.ts | 4 +- .../linter/src/lib/utils/consts/Literals.ts | 1 + .../linter/test/main/runtime_array_bound.ets | 13 ++++++ .../main/runtime_array_bound.ets.args.json | 36 ++++++++-------- .../main/runtime_array_bound.ets.arkts2.json | 42 ++++++++++++++++++- .../test/main/runtime_array_bound.ets.json | 28 ++++++------- .../main/runtime_array_bound.ets.migrate.ets | 13 ++++++ .../main/runtime_array_bound.ets.migrate.json | 12 +++++- .../test/main/stdlib_array.ets.arkts2.json | 12 +++++- .../test/main/undefined_check_calls.ets.json | 2 +- 12 files changed, 145 insertions(+), 40 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 17e696930b..afaca4febf 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -11503,12 +11503,26 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { return ts.isCallExpression(node) || ts.isArrowFunction(node) || ts.isFunctionExpression(node); } + private isConcatArray(accessedNode: ts.Node): boolean { + if (!ts.isIdentifier(accessedNode)) { + return false; + } + const decl = this.tsUtils.getDeclarationNode(accessedNode); + if (!decl) { + return false; + } + + const type = this.tsTypeChecker.getTypeAtLocation(decl); + return TsUtils.isConcatArrayType(type); + } + private getArrayAccessInfo(expr: ts.ElementAccessExpression): false | ArrayAccess { if (!ts.isIdentifier(expr.expression)) { return false; } const baseType = this.tsTypeChecker.getTypeAtLocation(expr.expression); - if (!this.tsUtils.isArray(baseType)) { + + if (!this.tsUtils.isArray(baseType) && !this.isConcatArray(expr.expression)) { return false; } const accessArgument = expr.argumentExpression; diff --git a/ets2panda/linter/src/lib/autofixes/Autofixer.ts b/ets2panda/linter/src/lib/autofixes/Autofixer.ts index 8ab3dc2c12..9e38dc79ea 100644 --- a/ets2panda/linter/src/lib/autofixes/Autofixer.ts +++ b/ets2panda/linter/src/lib/autofixes/Autofixer.ts @@ -3462,7 +3462,11 @@ export class Autofixer { return undefined; } - const replacementText = this.nonCommentPrinter.printNode(ts.EmitHint.Unspecified, replacement, expression.getSourceFile()); + const replacementText = this.nonCommentPrinter.printNode( + ts.EmitHint.Unspecified, + replacement, + expression.getSourceFile() + ); return [{ start: expression.getStart(), end: expression.getEnd(), replacementText }]; } diff --git a/ets2panda/linter/src/lib/utils/TsUtils.ts b/ets2panda/linter/src/lib/utils/TsUtils.ts index 5b3fabffbf..e477539ee9 100644 --- a/ets2panda/linter/src/lib/utils/TsUtils.ts +++ b/ets2panda/linter/src/lib/utils/TsUtils.ts @@ -46,7 +46,7 @@ import { ETS } from './consts/TsSuffix'; import { STRINGLITERAL_NUMBER, STRINGLITERAL_NUMBER_ARRAY } from './consts/StringLiteral'; import { ETS_MODULE, PATH_SEPARATOR, VALID_OHM_COMPONENTS_MODULE_PATH } from './consts/OhmUrl'; import { EXTNAME_ETS, EXTNAME_JS, EXTNAME_D_ETS } from './consts/ExtensionName'; -import { STRING_ERROR_LITERAL } from './consts/Literals'; +import { CONCAT_ARRAY, STRING_ERROR_LITERAL } from './consts/Literals'; export const PROMISE_METHODS = new Set(['all', 'race', 'any', 'resolve', 'allSettled']); export const SYMBOL = 'Symbol'; @@ -334,7 +334,7 @@ export class TsUtils { TsUtils.isTypeReference(tsType) && tsType.typeArguments?.length === 1 && tsType.target.typeParameters?.length === 1 && - tsType.getSymbol()?.getName() === 'ConcatArray' + tsType.getSymbol()?.getName() === CONCAT_ARRAY ); } diff --git a/ets2panda/linter/src/lib/utils/consts/Literals.ts b/ets2panda/linter/src/lib/utils/consts/Literals.ts index 4b54349261..df8054e343 100644 --- a/ets2panda/linter/src/lib/utils/consts/Literals.ts +++ b/ets2panda/linter/src/lib/utils/consts/Literals.ts @@ -14,3 +14,4 @@ */ export const STRING_ERROR_LITERAL = 'Error'; +export const CONCAT_ARRAY = 'ConcatArray'; diff --git a/ets2panda/linter/test/main/runtime_array_bound.ets b/ets2panda/linter/test/main/runtime_array_bound.ets index 934e9df563..33c5e11fbf 100644 --- a/ets2panda/linter/test/main/runtime_array_bound.ets +++ b/ets2panda/linter/test/main/runtime_array_bound.ets @@ -295,3 +295,16 @@ let values: string[] = Object.values(arr); for (let i = 0; i < keys.length; i++) { values[i]; } + +let concatArray: ConcatArray = new Array(1.0, 2.0, 3.0); + +let tempNum: number = concatArray[5]; + + +for (let i = 0; i < concatArray.length; i++) { + concatArray[i] +}; + +if (concatArray.length > 10) { + concatArray[10] +}; diff --git a/ets2panda/linter/test/main/runtime_array_bound.ets.args.json b/ets2panda/linter/test/main/runtime_array_bound.ets.args.json index 8a4be28991..18566d7d5a 100644 --- a/ets2panda/linter/test/main/runtime_array_bound.ets.args.json +++ b/ets2panda/linter/test/main/runtime_array_bound.ets.args.json @@ -1,20 +1,20 @@ { - "copyright": [ - "Copyright (c) 2025 Huawei Device Co., Ltd.", - "Licensed under the Apache License, Version 2.0 (the 'License');", - "you may not use this file except in compliance with the License.", - "You may obtain a copy of the License at", - "", - "http://www.apache.org/licenses/LICENSE-2.0", - "", - "Unless required by applicable law or agreed to in writing, software", - "distributed under the License is distributed on an 'AS IS' BASIS,", - "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", - "See the License for the specific language governing permissions and", - "limitations under the License." - ], - "mode": { - "arkts2": "", - "migrate": "--arkts-2" - } + "copyright": [ + "Copyright (c) 2025 Huawei Device Co., Ltd.", + "Licensed under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License.", + "You may obtain a copy of the License at", + "", + "http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an 'AS IS' BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + ], + "mode": { + "arkts2": "", + "migrate": "--arkts-2" + } } diff --git a/ets2panda/linter/test/main/runtime_array_bound.ets.arkts2.json b/ets2panda/linter/test/main/runtime_array_bound.ets.arkts2.json index ac6388b655..eeab471943 100644 --- a/ets2panda/linter/test/main/runtime_array_bound.ets.arkts2.json +++ b/ets2panda/linter/test/main/runtime_array_bound.ets.arkts2.json @@ -2513,6 +2513,46 @@ "suggest": "", "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", "severity": "ERROR" + }, + { + "line": 301, + "column": 23, + "endLine": 301, + "endColumn": 37, + "problem": "RuntimeArrayCheck", + "suggest": "", + "rule": "Array bound not checked. (arkts-runtime-array-check)", + "severity": "ERROR" + }, + { + "line": 304, + "column": 10, + "endLine": 304, + "endColumn": 15, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 304, + "column": 14, + "endLine": 304, + "endColumn": 15, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 308, + "column": 26, + "endLine": 308, + "endColumn": 28, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" } ] -} \ No newline at end of file +} diff --git a/ets2panda/linter/test/main/runtime_array_bound.ets.json b/ets2panda/linter/test/main/runtime_array_bound.ets.json index bd1b7140ca..9f305c86d7 100644 --- a/ets2panda/linter/test/main/runtime_array_bound.ets.json +++ b/ets2panda/linter/test/main/runtime_array_bound.ets.json @@ -1,17 +1,17 @@ { - "copyright": [ - "Copyright (c) 2025 Huawei Device Co., Ltd.", - "Licensed under the Apache License, Version 2.0 (the 'License');", - "you may not use this file except in compliance with the License.", - "You may obtain a copy of the License at", - "", - "http://www.apache.org/licenses/LICENSE-2.0", - "", - "Unless required by applicable law or agreed to in writing, software", - "distributed under the License is distributed on an 'AS IS' BASIS,", - "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", - "See the License for the specific language governing permissions and", - "limitations under the License." - ], + "copyright": [ + "Copyright (c) 2025 Huawei Device Co., Ltd.", + "Licensed under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License.", + "You may obtain a copy of the License at", + "", + "http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an 'AS IS' BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + ], "result": [] } diff --git a/ets2panda/linter/test/main/runtime_array_bound.ets.migrate.ets b/ets2panda/linter/test/main/runtime_array_bound.ets.migrate.ets index 31f0749e9f..5531ae7190 100644 --- a/ets2panda/linter/test/main/runtime_array_bound.ets.migrate.ets +++ b/ets2panda/linter/test/main/runtime_array_bound.ets.migrate.ets @@ -295,3 +295,16 @@ let values: string[] = Object.values(arr); for (let i: number = 0.0; i < keys.length; i++) { values[i as int]; } + +let concatArray: ConcatArray = new Array(1.0, 2.0, 3.0); + +let tempNum: number = concatArray[5]; + + +for (let i: number = 0.0; i < concatArray.length; i++) { + concatArray[i as int] +}; + +if (concatArray.length > 10.0) { + concatArray[10] +}; diff --git a/ets2panda/linter/test/main/runtime_array_bound.ets.migrate.json b/ets2panda/linter/test/main/runtime_array_bound.ets.migrate.json index 3d9a0f6ccc..84420569c7 100644 --- a/ets2panda/linter/test/main/runtime_array_bound.ets.migrate.json +++ b/ets2panda/linter/test/main/runtime_array_bound.ets.migrate.json @@ -233,6 +233,16 @@ "suggest": "", "rule": "Array type is immutable in ArkTS1.2 (arkts-array-type-immutable)", "severity": "ERROR" + }, + { + "line": 301, + "column": 23, + "endLine": 301, + "endColumn": 37, + "problem": "RuntimeArrayCheck", + "suggest": "", + "rule": "Array bound not checked. (arkts-runtime-array-check)", + "severity": "ERROR" } ] -} \ No newline at end of file +} diff --git a/ets2panda/linter/test/main/stdlib_array.ets.arkts2.json b/ets2panda/linter/test/main/stdlib_array.ets.arkts2.json index e1640cf89e..8e3ee4d132 100644 --- a/ets2panda/linter/test/main/stdlib_array.ets.arkts2.json +++ b/ets2panda/linter/test/main/stdlib_array.ets.arkts2.json @@ -324,6 +324,16 @@ "rule": "Array bound not checked. (arkts-runtime-array-check)", "severity": "ERROR" }, + { + "line": 49, + "column": 26, + "endLine": 49, + "endColumn": 30, + "problem": "RuntimeArrayCheck", + "suggest": "", + "rule": "Array bound not checked. (arkts-runtime-array-check)", + "severity": "ERROR" + }, { "line": 53, "column": 10, @@ -385,4 +395,4 @@ "severity": "ERROR" } ] -} \ No newline at end of file +} diff --git a/ets2panda/linter/test/main/undefined_check_calls.ets.json b/ets2panda/linter/test/main/undefined_check_calls.ets.json index 65d18bdd6c..6ba7098c4c 100644 --- a/ets2panda/linter/test/main/undefined_check_calls.ets.json +++ b/ets2panda/linter/test/main/undefined_check_calls.ets.json @@ -145,4 +145,4 @@ "severity": "ERROR" } ] -} \ No newline at end of file +} -- Gitee