From b75a023bf7b9fba90cdaf269a0247781c43d3486 Mon Sep 17 00:00:00 2001 From: Urakov Alexandr Date: Thu, 28 Sep 2023 11:07:25 +0300 Subject: [PATCH] [ArkTS][Linter] Fix an off-by-one error in the return type autofix Signed-off-by: Urakov Alexandr --- linter/src/Autofixer.ts | 2 +- linter/test/func_return_type.ts | 6 +- linter/test/func_return_type.ts.autofix.json | 113 ++++++++++++++----- linter/test/func_return_type.ts.strict.json | 95 ++++++++++++---- 4 files changed, 165 insertions(+), 51 deletions(-) diff --git a/linter/src/Autofixer.ts b/linter/src/Autofixer.ts index a336755ee..551287463 100644 --- a/linter/src/Autofixer.ts +++ b/linter/src/Autofixer.ts @@ -178,7 +178,7 @@ function getReturnTypePosition(funcLikeDecl: ts.FunctionLikeDeclaration): number const children = funcLikeDecl.getChildren(); for (let i = children.length - 1; i >= 0; i--) { const child = children[i]; - if (child.kind === ts.SyntaxKind.CloseParenToken && child.getEnd() < postParametersPosition) + if (child.kind === ts.SyntaxKind.CloseParenToken && child.getEnd() <= postParametersPosition) return child.getEnd(); } } diff --git a/linter/test/func_return_type.ts b/linter/test/func_return_type.ts index 3070e2da0..1508df915 100644 --- a/linter/test/func_return_type.ts +++ b/linter/test/func_return_type.ts @@ -126,4 +126,8 @@ class C { m5() { // Not fixable return z(30); } -} \ No newline at end of file +} + +function no_space_before_body(x: number){ // Need fix + return w(x) +} diff --git a/linter/test/func_return_type.ts.autofix.json b/linter/test/func_return_type.ts.autofix.json index 2511cd09a..cbee15e8b 100644 --- a/linter/test/func_return_type.ts.autofix.json +++ b/linter/test/func_return_type.ts.autofix.json @@ -25,7 +25,9 @@ "end": 631, "replacementText": ": number" } - ] + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 23, @@ -38,7 +40,9 @@ "end": 726, "replacementText": ": string" } - ] + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 33, @@ -51,37 +55,49 @@ "end": 857, "replacementText": ": number[]" } - ] + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 40, "column": 1, "problem": "LimitedReturnTypeInference", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 46, "column": 1, "problem": "LimitedReturnTypeInference", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 54, "column": 1, "problem": "LimitedReturnTypeInference", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 57, "column": 1, "problem": "LimitedReturnTypeInference", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 57, "column": 15, "problem": "AnyType", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)" }, { "line": 62, @@ -92,9 +108,11 @@ { "start": 1273, "end": 1324, - "replacementText": "(c: C): C => {\r\n return f2(c);\r\n}" + "replacementText": "(c: C): C => {\n return f2(c);\n}" } - ] + ], + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 65, @@ -105,9 +123,11 @@ { "start": 1337, "end": 1378, - "replacementText": "(c: C) => {\r\n return new C();\r\n}" + "replacementText": "(c: C) => {\n return new C();\n}" } - ] + ], + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 69, @@ -118,9 +138,11 @@ { "start": 1394, "end": 1449, - "replacementText": "(x: number, y: number) => {\r\n return x + y;\r\n}" + "replacementText": "(x: number, y: number) => {\n return x + y;\n}" } - ] + ], + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 73, @@ -131,9 +153,11 @@ { "start": 1463, "end": 1509, - "replacementText": "(): Map => {\r\n return f5();\r\n}" + "replacementText": "(): Map => {\n return f5();\n}" } - ] + ], + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 76, @@ -144,21 +168,27 @@ { "start": 1522, "end": 1577, - "replacementText": "() => {\r\n return new Map();\r\n}" + "replacementText": "() => {\n return new Map();\n}" } - ] + ], + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 80, "column": 12, "problem": "FunctionExpression", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 80, "column": 12, "problem": "LimitedReturnTypeInference", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 86, @@ -171,7 +201,9 @@ "end": 1686, "replacementText": ": T" } - ] + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 97, @@ -184,13 +216,17 @@ "end": 1848, "replacementText": ": (x: number) => string" } - ] + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 104, "column": 12, "problem": "LimitedReturnTypeInference", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 110, @@ -203,7 +239,9 @@ "end": 2037, "replacementText": ": number" } - ] + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 114, @@ -216,7 +254,9 @@ "end": 2090, "replacementText": ": number" } - ] + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 122, @@ -229,13 +269,32 @@ "end": 2177, "replacementText": ": number" } - ] + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 126, "column": 3, "problem": "LimitedReturnTypeInference", - "autofixable": false + "autofixable": false, + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" + }, + { + "line": 131, + "column": 1, + "problem": "LimitedReturnTypeInference", + "autofixable": true, + "autofix": [ + { + "start": 2310, + "end": 2310, + "replacementText": ": number" + } + ], + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" } ] } \ No newline at end of file diff --git a/linter/test/func_return_type.ts.strict.json b/linter/test/func_return_type.ts.strict.json index 7655777a4..873872127 100644 --- a/linter/test/func_return_type.ts.strict.json +++ b/linter/test/func_return_type.ts.strict.json @@ -17,112 +17,163 @@ { "line": 16, "column": 1, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 23, "column": 1, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 33, "column": 1, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 40, "column": 1, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 46, "column": 1, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 54, "column": 1, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 57, "column": 1, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 57, "column": 15, - "problem": "AnyType" + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)" }, { "line": 62, "column": 12, - "problem": "FunctionExpression" + "problem": "FunctionExpression", + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 65, "column": 12, - "problem": "FunctionExpression" + "problem": "FunctionExpression", + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 69, "column": 12, - "problem": "FunctionExpression" + "problem": "FunctionExpression", + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 73, "column": 12, - "problem": "FunctionExpression" + "problem": "FunctionExpression", + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 76, "column": 12, - "problem": "FunctionExpression" + "problem": "FunctionExpression", + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 80, "column": 12, - "problem": "FunctionExpression" + "problem": "FunctionExpression", + "suggest": "", + "rule": "Use arrow functions instead of function expressions (arkts-no-func-expressions)" }, { "line": 80, "column": 12, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 86, "column": 12, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 97, "column": 12, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 104, "column": 12, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 110, "column": 3, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 114, "column": 3, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 122, "column": 3, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 126, "column": 3, - "problem": "LimitedReturnTypeInference" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" + }, + { + "line": 131, + "column": 1, + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" } ] } \ No newline at end of file -- Gitee