From 0e889384b278c3662d5122972e3e0fe03ed1de4f Mon Sep 17 00:00:00 2001 From: zhongning5 Date: Tue, 17 Jun 2025 15:07:58 +0800 Subject: [PATCH] Fix for NoTuplesArrays Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICFDZ7 Test scenarios: fix bug Signed-off-by: zhongning5 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 19 +- .../linter/src/lib/utils/consts/ArraysAPI.ts | 49 +++ .../func_inferred_type_args.ets.arkts2.json | 10 + .../linter/test/main/no_tuples_arrays.ets | 42 ++- .../main/no_tuples_arrays.ets.arkts2.json | 280 ++++++++++++++++++ 5 files changed, 396 insertions(+), 4 deletions(-) create mode 100755 ets2panda/linter/src/lib/utils/consts/ArraysAPI.ts diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 4b72c290e0..da05d4e149 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -140,6 +140,7 @@ import type { ArrayAccess, UncheckedIdentifier, CheckedIdentifier } from './util import { CheckResult } from './utils/consts/RuntimeCheckAPI'; import { NUMBER_LITERAL } from './utils/consts/RuntimeCheckAPI'; import { globalApiAssociatedInfo } from './utils/consts/AssociatedInfo'; +import { ARRAY_API_LIST } from './utils/consts/ArraysAPI'; interface InterfaceSymbolTypeResult { propNames: string[]; @@ -1385,10 +1386,10 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.propertyAccessExpressionForBuiltin(node as ts.PropertyAccessExpression); this.checkConstrutorAccess(node as ts.PropertyAccessExpression); this.handleTaskPoolDeprecatedUsages(node as ts.PropertyAccessExpression); + this.handleNoTuplesArraysForPropertyAccessExpression(node as ts.PropertyAccessExpression); if (ts.isCallExpression(node.parent) && node === node.parent.expression) { return; } - const propertyAccessNode = node as ts.PropertyAccessExpression; const exprSym = this.tsUtils.trueSymbolAtLocation(propertyAccessNode); const baseExprSym = this.tsUtils.trueSymbolAtLocation(propertyAccessNode.expression); @@ -6690,14 +6691,26 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } if ( this.tsUtils.isOrDerivedFrom(lhsType, this.tsUtils.isArray) && - this.tsUtils.isOrDerivedFrom(rhsType, TsUtils.isTuple) || + this.tsUtils.isOrDerivedFrom(rhsType, TsUtils.isTuple) || this.tsUtils.isOrDerivedFrom(rhsType, this.tsUtils.isArray) && - this.tsUtils.isOrDerivedFrom(lhsType, TsUtils.isTuple) + this.tsUtils.isOrDerivedFrom(lhsType, TsUtils.isTuple) ) { this.incrementCounters(node, FaultID.NoTuplesArrays); } } + private handleNoTuplesArraysForPropertyAccessExpression(node: ts.PropertyAccessExpression): void { + if (!this.options.arkts2) { + return; + } + const lhsType = this.tsTypeChecker.getTypeAtLocation(node.expression); + if (this.tsUtils.isOrDerivedFrom(lhsType, TsUtils.isTuple)) { + if (ARRAY_API_LIST.includes(node.name.text)) { + this.incrementCounters(node, FaultID.NoTuplesArrays); + } + } + } + private handleArrayTypeImmutable(node: ts.Node, lhsType: ts.Type, rhsType: ts.Type, rhsExpr?: ts.Expression): void { if (!this.options.arkts2) { return; diff --git a/ets2panda/linter/src/lib/utils/consts/ArraysAPI.ts b/ets2panda/linter/src/lib/utils/consts/ArraysAPI.ts new file mode 100755 index 0000000000..4076fa0f93 --- /dev/null +++ b/ets2panda/linter/src/lib/utils/consts/ArraysAPI.ts @@ -0,0 +1,49 @@ +/* + * 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. + */ + +export const ARRAY_API_LIST = [ + 'length', + 'concat', + 'copyWithin', + 'entries', + 'every', + 'fill', + 'filter', + 'find', + 'findIndex', + 'flat', + 'flatMap', + 'forEach', + 'includes', + 'indexOf', + 'join', + 'keys', + 'lastIndexOf', + 'map', + 'pop', + 'push', + 'reduce', + 'reduceRight', + 'reverse', + 'shift', + 'slice', + 'some', + 'sort', + 'splice', + 'toLocaleString', + 'toString', + 'unshift', + 'values' +]; diff --git a/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json b/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json index c7c9c047cb..eee42f58f0 100755 --- a/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json +++ b/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json @@ -294,6 +294,16 @@ "rule": "Object literals cannot be used as type declarations (arkts-no-obj-literals-as-types)", "severity": "ERROR" }, + { + "line": 73, + "column": 1, + "endLine": 73, + "endColumn": 11, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, { "line": 78, "column": 37, diff --git a/ets2panda/linter/test/main/no_tuples_arrays.ets b/ets2panda/linter/test/main/no_tuples_arrays.ets index 93ab572c51..1ebcd176a4 100644 --- a/ets2panda/linter/test/main/no_tuples_arrays.ets +++ b/ets2panda/linter/test/main/no_tuples_arrays.ets @@ -87,4 +87,44 @@ let tuple19: [number, boolean] = array17 as [number, boolean] //error const originalArray: [number] = [1, 2, 3, 4, 5]; const array18 = originalArray.map((value) => value % 2 === 0 ? true : value * 2); let tuple20: [number, boolean] = array18 as [number, boolean] //error -let array20: (number)[] = originalArray as (number)[] //error \ No newline at end of file +let array20: (number)[] = originalArray as (number)[] //error + +const inputArray: readonly [Record, [], () => void] = [ + {}, + [], + () => { + } +]; +const even = (element: Record | [] | (() => void)): boolean => { + return typeof element === 'function'; +}; +const res = inputArray.some(even); // error +console.log("res:" + JSON.stringify(res)); +console.log(''+inputArray.length) // error +console.log(inputArray.toString()) // error +typeof inputArray.toLocaleString(); // error +function getConcat() { + inputArray.concat(); // error + return inputArray.join(','); // error +} +class Demo{ + set(){ + inputArray.slice(1,2); // error + inputArray.indexOf([]); // error + } + get(){ + return inputArray.lastIndexOf([]); // error + } +} +inputArray.every(()=>{}) // error +inputArray.some(()=>{}) // error +inputArray.forEach(()=>{}) // error +inputArray.map(()=>{}) // error +inputArray.filter(()=>{}) // error +inputArray.reduce((acc, item) => acc + 1, 0); // error +inputArray.reduceRight((acc, item) => acc + 1, 0); // error +inputArray.find((item) => Array.isArray(item)); // error +inputArray.includes(() => {}); // error +inputArray.flat() // error +inputArray.flatMap((item) => [item]); // error +inputArray.findIndex((item) => typeof item === 'function'); // error \ No newline at end of file diff --git a/ets2panda/linter/test/main/no_tuples_arrays.ets.arkts2.json b/ets2panda/linter/test/main/no_tuples_arrays.ets.arkts2.json index 9af978fcfc..e578984b44 100644 --- a/ets2panda/linter/test/main/no_tuples_arrays.ets.arkts2.json +++ b/ets2panda/linter/test/main/no_tuples_arrays.ets.arkts2.json @@ -374,6 +374,16 @@ "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", "severity": "ERROR" }, + { + "line": 88, + "column": 17, + "endLine": 88, + "endColumn": 34, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, { "line": 88, "column": 54, @@ -423,6 +433,276 @@ "suggest": "", "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", "severity": "ERROR" + }, + { + "line": 101, + "column": 13, + "endLine": 101, + "endColumn": 28, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 103, + "column": 16, + "endLine": 103, + "endColumn": 33, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 104, + "column": 13, + "endLine": 104, + "endColumn": 32, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 105, + "column": 8, + "endLine": 105, + "endColumn": 33, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 107, + "column": 3, + "endLine": 107, + "endColumn": 20, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 108, + "column": 10, + "endLine": 108, + "endColumn": 25, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 112, + "column": 5, + "endLine": 112, + "endColumn": 21, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 112, + "column": 22, + "endLine": 112, + "endColumn": 23, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 112, + "column": 24, + "endLine": 112, + "endColumn": 25, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 113, + "column": 5, + "endLine": 113, + "endColumn": 23, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 116, + "column": 12, + "endLine": 116, + "endColumn": 34, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 119, + "column": 1, + "endLine": 119, + "endColumn": 17, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 120, + "column": 1, + "endLine": 120, + "endColumn": 16, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 121, + "column": 1, + "endLine": 121, + "endColumn": 19, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 122, + "column": 1, + "endLine": 122, + "endColumn": 15, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 123, + "column": 1, + "endLine": 123, + "endColumn": 18, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 124, + "column": 1, + "endLine": 124, + "endColumn": 18, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 124, + "column": 40, + "endLine": 124, + "endColumn": 41, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 124, + "column": 43, + "endLine": 124, + "endColumn": 44, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 125, + "column": 1, + "endLine": 125, + "endColumn": 23, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 125, + "column": 45, + "endLine": 125, + "endColumn": 46, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 125, + "column": 48, + "endLine": 125, + "endColumn": 49, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 126, + "column": 1, + "endLine": 126, + "endColumn": 16, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 127, + "column": 1, + "endLine": 127, + "endColumn": 20, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 128, + "column": 1, + "endLine": 128, + "endColumn": 16, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 129, + "column": 1, + "endLine": 129, + "endColumn": 19, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" + }, + { + "line": 130, + "column": 1, + "endLine": 130, + "endColumn": 21, + "problem": "NoTuplesArrays", + "suggest": "", + "rule": "Array and tuple are different type(arkts-no-tuples-arrays)", + "severity": "ERROR" } ] } \ No newline at end of file -- Gitee