diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 15cb57df9756729c54421c58b47a3e02152778ff..25469fcc2cfd979da30a9c3ef53ba19087e02fac 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -8349,11 +8349,22 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { return lhsPromiseLikeType.typeArguments[0]; } + private getVariableDeclarationType(node: ts.Node): string { + let typeStr: string = ''; + const b = ts.findAncestor(node, ts.isVariableDeclaration); + + if (b?.type) { + const type = this.tsTypeChecker.getTypeAtLocation(b.type).getNonNullableType(); + typeStr = this.tsTypeChecker.typeToString(type); + } + return typeStr; + } + private handleArrayTypeImmutable(node: ts.Node, lhsType: ts.Type, rhsType: ts.Type, rhsExpr?: ts.Expression): void { if (!this.options.arkts2) { return; } - + const varDeclType: string = this.getVariableDeclarationType(node); const possibleLhsType = this.isExprReturnedFromAsyncFunction(rhsExpr, lhsType); if (possibleLhsType) { lhsType = possibleLhsType; @@ -8375,6 +8386,9 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (possibleLhsTypeStr) { lhsTypeStr = possibleLhsTypeStr; } + if (varDeclType === rhsTypeStr) { + return; + } if (lhsTypeStr !== rhsTypeStr) { this.incrementCounters(node, FaultID.ArrayTypeImmutable); diff --git a/ets2panda/linter/test/main/arkts-array-type-immutable.ets b/ets2panda/linter/test/main/arkts-array-type-immutable.ets index 3c018c68ab24c13ba22459b3696df7c95d28fe6c..9b6a0939f13bf6a7ebc1fa5bd66dff8e87396e54 100644 --- a/ets2panda/linter/test/main/arkts-array-type-immutable.ets +++ b/ets2panda/linter/test/main/arkts-array-type-immutable.ets @@ -138,6 +138,7 @@ const array20 = originalArray.map((value) => value % 2 === 0 ? true : value * 2) let array21: [number, boolean] = array20 as [number, boolean] let array22: (number|string)[] = array20 as (number)[] //error const array23: (number)[] = [1, 2, 3, 4, 5]; +const array24: string[] = [] as string[]; let aaa: number[] = [1] let bbb: (number | string)[] = aaa //error diff --git a/ets2panda/linter/test/main/arkts-array-type-immutable.ets.arkts2.json b/ets2panda/linter/test/main/arkts-array-type-immutable.ets.arkts2.json index 8ef0297136e121f20ea195d5538589891b9259ce..2ce7bb5a1db8eb9343dc88769deef761b3521675 100644 --- a/ets2panda/linter/test/main/arkts-array-type-immutable.ets.arkts2.json +++ b/ets2panda/linter/test/main/arkts-array-type-immutable.ets.arkts2.json @@ -224,16 +224,6 @@ "rule": "Array type is immutable in ArkTS1.2 (arkts-array-type-immutable)", "severity": "ERROR" }, - { - "line": 129, - "column": 35, - "endLine": 129, - "endColumn": 62, - "problem": "ArrayTypeImmutable", - "suggest": "", - "rule": "Array type is immutable in ArkTS1.2 (arkts-array-type-immutable)", - "severity": "ERROR" - }, { "line": 132, "column": 5, @@ -325,9 +315,9 @@ "severity": "ERROR" }, { - "line": 143, + "line": 144, "column": 5, - "endLine": 143, + "endLine": 144, "endColumn": 35, "problem": "ArrayTypeImmutable", "suggest": "", @@ -335,9 +325,9 @@ "severity": "ERROR" }, { - "line": 149, + "line": 150, "column": 40, - "endLine": 149, + "endLine": 150, "endColumn": 45, "problem": "BuiltinNewCtor", "suggest": "", @@ -345,9 +335,9 @@ "severity": "ERROR" }, { - "line": 149, + "line": 150, "column": 36, - "endLine": 149, + "endLine": 150, "endColumn": 47, "problem": "GenericCallNoTypeArgs", "suggest": "", @@ -355,9 +345,9 @@ "severity": "ERROR" }, { - "line": 150, + "line": 151, "column": 21, - "endLine": 150, + "endLine": 151, "endColumn": 26, "problem": "BuiltinNewCtor", "suggest": "", @@ -365,9 +355,9 @@ "severity": "ERROR" }, { - "line": 150, + "line": 151, "column": 17, - "endLine": 150, + "endLine": 151, "endColumn": 29, "problem": "GenericCallNoTypeArgs", "suggest": "", @@ -375,9 +365,9 @@ "severity": "ERROR" }, { - "line": 150, + "line": 151, "column": 17, - "endLine": 150, + "endLine": 151, "endColumn": 29, "problem": "UninitializedArrayElements", "suggest": "", @@ -385,9 +375,9 @@ "severity": "WARNING" }, { - "line": 151, + "line": 152, "column": 1, - "endLine": 151, + "endLine": 152, "endColumn": 54, "problem": "InteropCallReflect", "suggest": "", @@ -395,9 +385,9 @@ "severity": "ERROR" }, { - "line": 151, + "line": 152, "column": 9, - "endLine": 151, + "endLine": 152, "endColumn": 14, "problem": "LimitedStdLibApi", "suggest": "", @@ -405,9 +395,9 @@ "severity": "ERROR" }, { - "line": 155, + "line": 156, "column": 17, - "endLine": 155, + "endLine": 156, "endColumn": 22, "problem": "BuiltinNewCtor", "suggest": "", @@ -415,9 +405,9 @@ "severity": "ERROR" }, { - "line": 156, + "line": 157, "column": 1, - "endLine": 156, + "endLine": 157, "endColumn": 20, "problem": "ArrayTypeImmutable", "suggest": "", @@ -425,9 +415,9 @@ "severity": "ERROR" }, { - "line": 163, + "line": 164, "column": 43, - "endLine": 163, + "endLine": 164, "endColumn": 54, "problem": "ArrayTypeImmutable", "suggest": "", @@ -435,9 +425,9 @@ "severity": "ERROR" }, { - "line": 167, + "line": 168, "column": 5, - "endLine": 167, + "endLine": 168, "endColumn": 24, "problem": "ArrayTypeImmutable", "suggest": "", @@ -445,4 +435,4 @@ "severity": "ERROR" } ] -} \ No newline at end of file +} diff --git a/ets2panda/linter/test/main/arkts-array-type-immutable.ets.json b/ets2panda/linter/test/main/arkts-array-type-immutable.ets.json index d8f6775c620327300157f3944a39ef32a7656cc6..9e8e19ca60caa4099ded9b3b33deb3f2414a29d4 100644 --- a/ets2panda/linter/test/main/arkts-array-type-immutable.ets.json +++ b/ets2panda/linter/test/main/arkts-array-type-immutable.ets.json @@ -55,9 +55,9 @@ "severity": "ERROR" }, { - "line": 151, + "line": 152, "column": 1, - "endLine": 151, + "endLine": 152, "endColumn": 53, "problem": "LimitedStdLibApi", "suggest": "", @@ -65,9 +65,9 @@ "severity": "ERROR" }, { - "line": 151, + "line": 152, "column": 1, - "endLine": 151, + "endLine": 152, "endColumn": 54, "problem": "InteropCallReflect", "suggest": "", @@ -75,4 +75,4 @@ "severity": "ERROR" } ] -} \ No newline at end of file +}