From 5ac4c1f57e83da68123880e698ffa65fb230f846 Mon Sep 17 00:00:00 2001 From: fanglou Date: Fri, 22 Aug 2025 14:18:14 +0800 Subject: [PATCH] ArkTS1.2 not support unfixed length tuple Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICU885 Signed-off-by: fanglou --- ets2panda/linter/rule-config.json | 3 +- ets2panda/linter/src/cli/CommandLineParser.ts | 6 +- ets2panda/linter/src/lib/CookBookMsg.ts | 2 + ets2panda/linter/src/lib/FaultAttrs.ts | 1 + ets2panda/linter/src/lib/FaultDesc.ts | 1 + ets2panda/linter/src/lib/Problems.ts | 1 + ets2panda/linter/src/lib/TypeScriptLinter.ts | 12 ++- .../scan/ProblemStatisticsCommonFunction.ts | 9 +- .../utils/functions/ConfiguredRulesProcess.ts | 2 +- .../test/main/unfixed_tuple_negative.ets | 30 +++++++ .../main/unfixed_tuple_negative.ets.args.json | 19 ++++ .../unfixed_tuple_negative.ets.arkts2.json | 88 +++++++++++++++++++ .../test/main/unfixed_tuple_negative.ets.json | 17 ++++ .../test/main/unfixed_tuple_positive.ets | 36 ++++++++ .../main/unfixed_tuple_positive.ets.args.json | 19 ++++ .../unfixed_tuple_positive.ets.arkts2.json | 17 ++++ .../test/main/unfixed_tuple_positive.ets.json | 17 ++++ 17 files changed, 273 insertions(+), 7 deletions(-) create mode 100644 ets2panda/linter/test/main/unfixed_tuple_negative.ets create mode 100644 ets2panda/linter/test/main/unfixed_tuple_negative.ets.args.json create mode 100644 ets2panda/linter/test/main/unfixed_tuple_negative.ets.arkts2.json create mode 100644 ets2panda/linter/test/main/unfixed_tuple_negative.ets.json create mode 100644 ets2panda/linter/test/main/unfixed_tuple_positive.ets create mode 100644 ets2panda/linter/test/main/unfixed_tuple_positive.ets.args.json create mode 100644 ets2panda/linter/test/main/unfixed_tuple_positive.ets.arkts2.json create mode 100644 ets2panda/linter/test/main/unfixed_tuple_positive.ets.json diff --git a/ets2panda/linter/rule-config.json b/ets2panda/linter/rule-config.json index 673ad51d19..7435c3c134 100644 --- a/ets2panda/linter/rule-config.json +++ b/ets2panda/linter/rule-config.json @@ -80,7 +80,8 @@ "arkts-not-support-tuple-generic-validation", "arkts-no-optional-tuple-type", "arkts-no-large-numeric-literal", - "arkts-no-instanceof-func" + "arkts-no-instanceof-func", + "arkts-no-unfixed-len-tuple" ], "interop": [ "arkts-interop-js2s-inherit-js-class", diff --git a/ets2panda/linter/src/cli/CommandLineParser.ts b/ets2panda/linter/src/cli/CommandLineParser.ts index 83d0e13743..e84fb2a7ad 100644 --- a/ets2panda/linter/src/cli/CommandLineParser.ts +++ b/ets2panda/linter/src/cli/CommandLineParser.ts @@ -22,7 +22,11 @@ import type { CommandLineOptions } from '../lib/CommandLineOptions'; import { cookBookTag } from '../lib/CookBookMsg'; import { Logger } from '../lib/Logger'; import { ARKTS_IGNORE_DIRS_OH_MODULES } from '../lib/utils/consts/ArktsIgnorePaths'; -import { getConfiguredRuleTags, getConfigureRulePath, getRulesFromConfig } from '../lib/utils/functions/ConfiguredRulesProcess'; +import { + getConfiguredRuleTags, + getConfigureRulePath, + getRulesFromConfig +} from '../lib/utils/functions/ConfiguredRulesProcess'; import { extractRuleTags } from '../lib/utils/functions/CookBookUtils'; import { logTscDiagnostic } from '../lib/utils/functions/LogTscDiagnostic'; diff --git a/ets2panda/linter/src/lib/CookBookMsg.ts b/ets2panda/linter/src/lib/CookBookMsg.ts index 7f1377fa3c..a6699b74cd 100644 --- a/ets2panda/linter/src/lib/CookBookMsg.ts +++ b/ets2panda/linter/src/lib/CookBookMsg.ts @@ -437,6 +437,8 @@ cookBookTag[408] = 'The property of IteratorResult is not supported (arkts-built cookBookTag[409] = 'No optional tuple type (arkts-no-optional-tuple-type)'; cookBookTag[410] = 'Numeric literal exceeds allowed range (arkts-no-large-numeric-literal)'; cookBookTag[411] = '"instanceof" operator can\'t be applied to function (arkts-no-instanceof-func)'; +cookBookTag[412] = 'No unfixed length tuple support (arkts-no-unfixed-len-tuple)'; + for (let i = 0; i <= cookBookTag.length; i++) { cookBookMsg[i] = ''; } diff --git a/ets2panda/linter/src/lib/FaultAttrs.ts b/ets2panda/linter/src/lib/FaultAttrs.ts index aa48bb969b..8ce464cf4f 100644 --- a/ets2panda/linter/src/lib/FaultAttrs.ts +++ b/ets2panda/linter/src/lib/FaultAttrs.ts @@ -309,3 +309,4 @@ faultsAttrs[FaultID.BuiltinIteratorResultValue] = new FaultAttributes(408); faultsAttrs[FaultID.OptionalTupleType] = new FaultAttributes(409); faultsAttrs[FaultID.LargeNumericLiteral] = new FaultAttributes(410); faultsAttrs[FaultID.InstanceOfFunction] = new FaultAttributes(411); +faultsAttrs[FaultID.unfixedTuple] = new FaultAttributes(412); diff --git a/ets2panda/linter/src/lib/FaultDesc.ts b/ets2panda/linter/src/lib/FaultDesc.ts index 6eb954570e..90fbf7d3d9 100644 --- a/ets2panda/linter/src/lib/FaultDesc.ts +++ b/ets2panda/linter/src/lib/FaultDesc.ts @@ -298,3 +298,4 @@ faultDesc[FaultID.BuiltinDisableApi] = 'Disable Api'; faultDesc[FaultID.BuiltinIteratorResultValue] = 'IteratorResult.value is not supported'; faultDesc[FaultID.OptionalTupleType] = 'No optional tuple type'; faultDesc[FaultID.LargeNumericLiteral] = 'Numeric literal exceeds allowed range'; +faultDesc[FaultID.unfixedTuple] = 'No unfixed tuple'; diff --git a/ets2panda/linter/src/lib/Problems.ts b/ets2panda/linter/src/lib/Problems.ts index e9908007e0..84a74f0fb5 100644 --- a/ets2panda/linter/src/lib/Problems.ts +++ b/ets2panda/linter/src/lib/Problems.ts @@ -298,6 +298,7 @@ export enum FaultID { OptionalTupleType, LargeNumericLiteral, InstanceOfFunction, + unfixedTuple, // this should always be last enum LAST_ID } diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 6481f26d69..2f507778d1 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -554,7 +554,8 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { [ts.SyntaxKind.AwaitExpression, this.handleAwaitExpression], [ts.SyntaxKind.PostfixUnaryExpression, this.handlePostfixUnaryExpression], [ts.SyntaxKind.BigIntLiteral, this.handleBigIntLiteral], - [ts.SyntaxKind.NumericLiteral, this.handleNumericLiteral] + [ts.SyntaxKind.NumericLiteral, this.handleNumericLiteral], + [ts.SyntaxKind.RestType, this.handleRestType] ]); lint(): void { @@ -15453,4 +15454,13 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { '"toJson" and "fromJson" (arkui-persistencev2-connect-serialization)'; this.incrementCounters(callExpr, FaultID.PersistenceV2ConnectNeedAddParam, undefined, errorMsg); } + + private handleRestType(node: ts.Node): void { + if (!this.options.arkts2) { + return; + } + if (node.parent && ts.isTupleTypeNode(node.parent)) { + this.incrementCounters(node, FaultID.unfixedTuple); + } + } } diff --git a/ets2panda/linter/src/lib/statistics/scan/ProblemStatisticsCommonFunction.ts b/ets2panda/linter/src/lib/statistics/scan/ProblemStatisticsCommonFunction.ts index a9c638bd7d..678929a489 100644 --- a/ets2panda/linter/src/lib/statistics/scan/ProblemStatisticsCommonFunction.ts +++ b/ets2panda/linter/src/lib/statistics/scan/ProblemStatisticsCommonFunction.ts @@ -165,7 +165,10 @@ export async function generateScanProbelemStatisticsReport( ); } -function getProcessedRuleToNumbersMap(ruleToNumbersMap: Map, wholeLinterRules: string[]) : Map { +function getProcessedRuleToNumbersMap( + ruleToNumbersMap: Map, + wholeLinterRules: string[] +): Map { const processedRuleToNumbersMap: Map = new Map(); const homecheckRuleToNumbersMap: Map = ruleToNumbersMap; wholeLinterRules.forEach((ruleName) => { @@ -173,10 +176,10 @@ function getProcessedRuleToNumbersMap(ruleToNumbersMap: Map, who homecheckRuleToNumbersMap.delete(ruleName); processedRuleToNumbersMap.set(ruleName, ruleNumber); }); - + homecheckRuleToNumbersMap.forEach((number, ruleName) => { processedRuleToNumbersMap.set(ruleName, number); - }) + }); return processedRuleToNumbersMap; } diff --git a/ets2panda/linter/src/lib/utils/functions/ConfiguredRulesProcess.ts b/ets2panda/linter/src/lib/utils/functions/ConfiguredRulesProcess.ts index 65ee056abc..6b531d29c3 100644 --- a/ets2panda/linter/src/lib/utils/functions/ConfiguredRulesProcess.ts +++ b/ets2panda/linter/src/lib/utils/functions/ConfiguredRulesProcess.ts @@ -97,7 +97,7 @@ function isStringArray(value: any): value is string[] { ); } -export function getwholeRules() : string[] { +export function getwholeRules(): string[] { const configureRulePath = getDefaultConfigurePath(); const configuredRulesMap = getRulesFromConfig(configureRulePath); return Array.from(configuredRulesMap.values()).flat(); diff --git a/ets2panda/linter/test/main/unfixed_tuple_negative.ets b/ets2panda/linter/test/main/unfixed_tuple_negative.ets new file mode 100644 index 0000000000..05723324d0 --- /dev/null +++ b/ets2panda/linter/test/main/unfixed_tuple_negative.ets @@ -0,0 +1,30 @@ +/* + * 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. + */ + +const s: [string, ...boolean[]] = ['',true] // report error for unfixed tuple + +type Tuple1 = [string, ...boolean[]]; // report error for unfixed tuple + +type Tuple2 = [number, string, ...number[]]; // report error for unfixed tuple + +type Tuple3 = [...string[]]; // report error for unfixed tuple + +type Tuple4 = [...boolean[], number]; // report error for unfixed tuple + +type NestedTuple = [string, ...[number, boolean][]]; // report error for unfixed tuple + +function logTuple(args: [string, ...number[]]) { // report error for unfixed tuple + +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/unfixed_tuple_negative.ets.args.json b/ets2panda/linter/test/main/unfixed_tuple_negative.ets.args.json new file mode 100644 index 0000000000..bc4d2071da --- /dev/null +++ b/ets2panda/linter/test/main/unfixed_tuple_negative.ets.args.json @@ -0,0 +1,19 @@ +{ + "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": "" + } +} diff --git a/ets2panda/linter/test/main/unfixed_tuple_negative.ets.arkts2.json b/ets2panda/linter/test/main/unfixed_tuple_negative.ets.arkts2.json new file mode 100644 index 0000000000..cf32008b80 --- /dev/null +++ b/ets2panda/linter/test/main/unfixed_tuple_negative.ets.arkts2.json @@ -0,0 +1,88 @@ +{ + "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": [ + { + "line": 16, + "column": 19, + "endLine": 16, + "endColumn": 31, + "problem": "unfixedTuple", + "suggest": "", + "rule": "No unfixed length tuple support (arkts-no-unfixed-len-tuple)", + "severity": "ERROR" + }, + { + "line": 18, + "column": 24, + "endLine": 18, + "endColumn": 36, + "problem": "unfixedTuple", + "suggest": "", + "rule": "No unfixed length tuple support (arkts-no-unfixed-len-tuple)", + "severity": "ERROR" + }, + { + "line": 20, + "column": 32, + "endLine": 20, + "endColumn": 43, + "problem": "unfixedTuple", + "suggest": "", + "rule": "No unfixed length tuple support (arkts-no-unfixed-len-tuple)", + "severity": "ERROR" + }, + { + "line": 22, + "column": 16, + "endLine": 22, + "endColumn": 27, + "problem": "unfixedTuple", + "suggest": "", + "rule": "No unfixed length tuple support (arkts-no-unfixed-len-tuple)", + "severity": "ERROR" + }, + { + "line": 24, + "column": 16, + "endLine": 24, + "endColumn": 28, + "problem": "unfixedTuple", + "suggest": "", + "rule": "No unfixed length tuple support (arkts-no-unfixed-len-tuple)", + "severity": "ERROR" + }, + { + "line": 26, + "column": 29, + "endLine": 26, + "endColumn": 51, + "problem": "unfixedTuple", + "suggest": "", + "rule": "No unfixed length tuple support (arkts-no-unfixed-len-tuple)", + "severity": "ERROR" + }, + { + "line": 28, + "column": 34, + "endLine": 28, + "endColumn": 45, + "problem": "unfixedTuple", + "suggest": "", + "rule": "No unfixed length tuple support (arkts-no-unfixed-len-tuple)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/unfixed_tuple_negative.ets.json b/ets2panda/linter/test/main/unfixed_tuple_negative.ets.json new file mode 100644 index 0000000000..ca88f857e9 --- /dev/null +++ b/ets2panda/linter/test/main/unfixed_tuple_negative.ets.json @@ -0,0 +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." + ], + "result": [] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/unfixed_tuple_positive.ets b/ets2panda/linter/test/main/unfixed_tuple_positive.ets new file mode 100644 index 0000000000..e45b73893e --- /dev/null +++ b/ets2panda/linter/test/main/unfixed_tuple_positive.ets @@ -0,0 +1,36 @@ +/* + * 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. + */ + +const s: [string, boolean[]] = ['',[true]] // ok + +const s1: [string, boolean] = ['',true] // ok + +type Tuple1 = [string, boolean[]]; // ok + +type Tuple2 = [number, string, number[]]; // ok + +type Tuple3 = [string[]]; // ok + +type Tuple4 = [boolean[], number]; // ok + +type NestedTuple = [string, [number, boolean][]]; // ok + +function logTuple(args: [string, number[]]) { // ok + +} + +function sum(...numbers: number[]): number { // ok + return numbers.reduce((total, num) => total + num, 0); +} diff --git a/ets2panda/linter/test/main/unfixed_tuple_positive.ets.args.json b/ets2panda/linter/test/main/unfixed_tuple_positive.ets.args.json new file mode 100644 index 0000000000..bc4d2071da --- /dev/null +++ b/ets2panda/linter/test/main/unfixed_tuple_positive.ets.args.json @@ -0,0 +1,19 @@ +{ + "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": "" + } +} diff --git a/ets2panda/linter/test/main/unfixed_tuple_positive.ets.arkts2.json b/ets2panda/linter/test/main/unfixed_tuple_positive.ets.arkts2.json new file mode 100644 index 0000000000..ca88f857e9 --- /dev/null +++ b/ets2panda/linter/test/main/unfixed_tuple_positive.ets.arkts2.json @@ -0,0 +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." + ], + "result": [] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/unfixed_tuple_positive.ets.json b/ets2panda/linter/test/main/unfixed_tuple_positive.ets.json new file mode 100644 index 0000000000..ca88f857e9 --- /dev/null +++ b/ets2panda/linter/test/main/unfixed_tuple_positive.ets.json @@ -0,0 +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." + ], + "result": [] +} \ No newline at end of file -- Gitee