From 21a620d312a3ea0cd5816331f047722d934485d1 Mon Sep 17 00:00:00 2001 From: Urakov Alexandr Date: Fri, 3 Nov 2023 13:31:59 +0300 Subject: [PATCH] [ArkTS][Linter] Fix suppression of strict diagnostics for overloaded functions (#14328) --- linter/src/TypeScriptLinter.ts | 4 +++ .../LibraryTypeCallDiagnosticChecker.ts | 9 +++++++ linter/test/null_check_calls.ts.relax.json | 21 +++++++++------- linter/test/null_check_calls.ts.strict.json | 25 +++++++++++-------- .../test/undefined_check_calls.ts.relax.json | 7 ------ .../test/undefined_check_calls.ts.strict.json | 7 ------ 6 files changed, 40 insertions(+), 33 deletions(-) diff --git a/linter/src/TypeScriptLinter.ts b/linter/src/TypeScriptLinter.ts index 863cd054..f9d81f93 100644 --- a/linter/src/TypeScriptLinter.ts +++ b/linter/src/TypeScriptLinter.ts @@ -50,6 +50,7 @@ import { ReportAutofixCallback } from './autofixes/ReportAutofixCallback'; import { DiagnosticChecker } from './utils/functions/DiagnosticChecker'; import { ARGUMENT_OF_TYPE_0_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_ERROR_CODE, + NO_OVERLOAD_MATCHES_THIS_CALL_ERROR_CODE, TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE, LibraryTypeCallDiagnosticChecker } from './utils/functions/LibraryTypeCallDiagnosticChecker'; @@ -1566,6 +1567,9 @@ export class TypeScriptLinter { [ARGUMENT_OF_TYPE_0_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_ERROR_CODE]: (pos: number) => { return this.checkInRange(rangesToFilter, pos); }, + [NO_OVERLOAD_MATCHES_THIS_CALL_ERROR_CODE]: (pos: number) => { + return this.checkInRange([{begin: callExpr.pos, end: callExpr.end}], pos); + }, [TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE]: (pos: number) => { return this.checkInRange(rangesToFilter, pos); } diff --git a/linter/src/utils/functions/LibraryTypeCallDiagnosticChecker.ts b/linter/src/utils/functions/LibraryTypeCallDiagnosticChecker.ts index 72778922..3adf03b3 100644 --- a/linter/src/utils/functions/LibraryTypeCallDiagnosticChecker.ts +++ b/linter/src/utils/functions/LibraryTypeCallDiagnosticChecker.ts @@ -24,6 +24,7 @@ export const TYPE_NULL_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE = /^Type 'null' is not ass export const TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE = /^Type 'undefined' is not assignable to type '.*'\.$/; export const ARGUMENT_OF_TYPE_0_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_ERROR_CODE = 2345; +export const NO_OVERLOAD_MATCHES_THIS_CALL_ERROR_CODE = 2769; export const ARGUMENT_OF_TYPE_NULL_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_RE = /^Argument of type 'null' is not assignable to parameter of type '.*'\.$/; export const ARGUMENT_OF_TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_RE = /^Argument of type 'undefined' is not assignable to parameter of type '.*'\.$/; @@ -64,6 +65,14 @@ export class LibraryTypeCallDiagnosticChecker implements DiagnosticChecker { return false; } } + if (chain.code == ARGUMENT_OF_TYPE_0_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_ERROR_CODE) { + if (this.inLibCall && chain.messageText.match(ARGUMENT_OF_TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_RE)) { + return false; + } + if (this.inLibCall && chain.messageText.match(ARGUMENT_OF_TYPE_NULL_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_RE)) { + return false; + } + } return chain.next == undefined ? true : this.checkMessageChain(chain.next[0]); }; diff --git a/linter/test/null_check_calls.ts.relax.json b/linter/test/null_check_calls.ts.relax.json index 8d879ca4..dda25acb 100644 --- a/linter/test/null_check_calls.ts.relax.json +++ b/linter/test/null_check_calls.ts.relax.json @@ -14,30 +14,33 @@ "limitations under the License." ], "nodes": [ - { - "line": 33, - "column": 12, - "problem": "StrictDiagnostic" - }, { "line": 37, "column": 5, - "problem": "StrictDiagnostic" + "problem": "StrictDiagnostic", + "suggest": "Argument of type 'null' is not assignable to parameter of type 'number[]'.", + "rule": "Argument of type 'null' is not assignable to parameter of type 'number[]'." }, { "line": 42, "column": 20, - "problem": "StrictDiagnostic" + "problem": "StrictDiagnostic", + "suggest": "Argument of type 'null' is not assignable to parameter of type 'number'.", + "rule": "Argument of type 'null' is not assignable to parameter of type 'number'." }, { "line": 43, "column": 42, - "problem": "StrictDiagnostic" + "problem": "StrictDiagnostic", + "suggest": "Argument of type 'number | null' is not assignable to parameter of type 'number'.\n Type 'null' is not assignable to type 'number'.", + "rule": "Argument of type 'number | null' is not assignable to parameter of type 'number'.\n Type 'null' is not assignable to type 'number'." }, { "line": 47, "column": 5, - "problem": "StrictDiagnostic" + "problem": "StrictDiagnostic", + "suggest": "Argument of type 'number | null' is not assignable to parameter of type 'number'.\n Type 'null' is not assignable to type 'number'.", + "rule": "Argument of type 'number | null' is not assignable to parameter of type 'number'.\n Type 'null' is not assignable to type 'number'." }, { "line": 52, diff --git a/linter/test/null_check_calls.ts.strict.json b/linter/test/null_check_calls.ts.strict.json index fef4e06a..6ae1263e 100644 --- a/linter/test/null_check_calls.ts.strict.json +++ b/linter/test/null_check_calls.ts.strict.json @@ -17,32 +17,37 @@ { "line": 43, "column": 1, - "problem": "LimitedReturnTypeInference" - }, - { - "line": 33, - "column": 12, - "problem": "StrictDiagnostic" + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)" }, { "line": 37, "column": 5, - "problem": "StrictDiagnostic" + "problem": "StrictDiagnostic", + "suggest": "Argument of type 'null' is not assignable to parameter of type 'number[]'.", + "rule": "Argument of type 'null' is not assignable to parameter of type 'number[]'." }, { "line": 42, "column": 20, - "problem": "StrictDiagnostic" + "problem": "StrictDiagnostic", + "suggest": "Argument of type 'null' is not assignable to parameter of type 'number'.", + "rule": "Argument of type 'null' is not assignable to parameter of type 'number'." }, { "line": 43, "column": 42, - "problem": "StrictDiagnostic" + "problem": "StrictDiagnostic", + "suggest": "Argument of type 'number | null' is not assignable to parameter of type 'number'.\n Type 'null' is not assignable to type 'number'.", + "rule": "Argument of type 'number | null' is not assignable to parameter of type 'number'.\n Type 'null' is not assignable to type 'number'." }, { "line": 47, "column": 5, - "problem": "StrictDiagnostic" + "problem": "StrictDiagnostic", + "suggest": "Argument of type 'number | null' is not assignable to parameter of type 'number'.\n Type 'null' is not assignable to type 'number'.", + "rule": "Argument of type 'number | null' is not assignable to parameter of type 'number'.\n Type 'null' is not assignable to type 'number'." }, { "line": 52, diff --git a/linter/test/undefined_check_calls.ts.relax.json b/linter/test/undefined_check_calls.ts.relax.json index 33bf02fd..21f9e869 100644 --- a/linter/test/undefined_check_calls.ts.relax.json +++ b/linter/test/undefined_check_calls.ts.relax.json @@ -14,13 +14,6 @@ "limitations under the License." ], "nodes": [ - { - "line": 33, - "column": 12, - "problem": "StrictDiagnostic", - "suggest": "No overload matches this call.\n Overload 1 of 2, '(func: Function, ...args: unknown[]): Promise', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'Function'.\n Overload 2 of 2, '(group: TaskGroup, priority?: Priority | undefined): Promise', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'TaskGroup'.", - "rule": "No overload matches this call.\n Overload 1 of 2, '(func: Function, ...args: unknown[]): Promise', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'Function'.\n Overload 2 of 2, '(group: TaskGroup, priority?: Priority | undefined): Promise', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'TaskGroup'." - }, { "line": 37, "column": 5, diff --git a/linter/test/undefined_check_calls.ts.strict.json b/linter/test/undefined_check_calls.ts.strict.json index 33bf02fd..21f9e869 100644 --- a/linter/test/undefined_check_calls.ts.strict.json +++ b/linter/test/undefined_check_calls.ts.strict.json @@ -14,13 +14,6 @@ "limitations under the License." ], "nodes": [ - { - "line": 33, - "column": 12, - "problem": "StrictDiagnostic", - "suggest": "No overload matches this call.\n Overload 1 of 2, '(func: Function, ...args: unknown[]): Promise', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'Function'.\n Overload 2 of 2, '(group: TaskGroup, priority?: Priority | undefined): Promise', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'TaskGroup'.", - "rule": "No overload matches this call.\n Overload 1 of 2, '(func: Function, ...args: unknown[]): Promise', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'Function'.\n Overload 2 of 2, '(group: TaskGroup, priority?: Priority | undefined): Promise', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'TaskGroup'." - }, { "line": 37, "column": 5, -- Gitee