diff --git a/linter-4.2/src/TypeScriptLinter.ts b/linter-4.2/src/TypeScriptLinter.ts index c2a79bc87d4d7350fd3f530e5046ce2534d4d920..2c505db757051a3e4dd420250a54ff685e3784fb 100644 --- a/linter-4.2/src/TypeScriptLinter.ts +++ b/linter-4.2/src/TypeScriptLinter.ts @@ -1942,18 +1942,25 @@ export class TypeScriptLinter { 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 '.*'\.$/; 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 '.*'\.$/; + let inLibCall = this.tsUtils.isLibraryType(this.tsTypeChecker.getTypeAtLocation(callExpr.expression)); + const chainCheck = (n: ts.DiagnosticMessageChain): boolean => { - if (n.code == TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE && - (n.messageText.match(TYPE_UNKNOWN_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE) || - n.messageText.match(TYPE_NULL_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE) || - n.messageText.match(TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE))) { - return false; + if (n.code == TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE) { + if (n.messageText.match(TYPE_UNKNOWN_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE)) { + return false; + } + if (n.messageText.match(TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE)) { + return false; + } + if (inLibCall && n.messageText.match(TYPE_NULL_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE)) { + return false; + } } return n.next == undefined ? true : chainCheck(n.next[0]); }; const msgCheck = (msg: string): boolean => { - if (this.tsUtils.isLibraryType(this.tsTypeChecker.getTypeAtLocation(callExpr.expression))) { + if (inLibCall) { const match = msg.match(ARGUMENT_OF_TYPE_NULL_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_RE) || msg.match(ARGUMENT_OF_TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_RE); return !match; diff --git a/linter-4.2/test/null_check_calls.ts b/linter-4.2/test/null_check_calls.ts index 18bcaa037e2ad9769e4af2be55978dc84bed139a..b79ac3409082bfe1f5eff1e2b2d119df6c8af985 100644 --- a/linter-4.2/test/null_check_calls.ts +++ b/linter-4.2/test/null_check_calls.ts @@ -37,3 +37,11 @@ function fff(a: Array): void {} fff(null); ff(null); f(null); + + +((a: number) => a)(null); +(a: null | number) => ((b: number) => b)(a); + +function inc(a: number) { return a + 1 } +function neg2null(x: number) { return x > 0 ? x : null } +inc(neg2null(-1)); diff --git a/linter-4.2/test/null_check_calls.ts.relax.json b/linter-4.2/test/null_check_calls.ts.relax.json index 9d5793764dd46a1404fda4ed4c2b2e76e5b0996b..b40e140bace347bfea81d26b7ad363e38c4e7b03 100644 --- a/linter-4.2/test/null_check_calls.ts.relax.json +++ b/linter-4.2/test/null_check_calls.ts.relax.json @@ -17,16 +17,27 @@ { "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 'null' 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 'null' 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 'null' 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 'null' is not assignable to parameter of type 'TaskGroup'." + "problem": "StrictDiagnostic" }, { "line": 37, "column": 5, - "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[]'." + "problem": "StrictDiagnostic" + }, + { + "line": 42, + "column": 20, + "problem": "StrictDiagnostic" + }, + { + "line": 43, + "column": 42, + "problem": "StrictDiagnostic" + }, + { + "line": 47, + "column": 5, + "problem": "StrictDiagnostic" } ] } \ No newline at end of file diff --git a/linter-4.2/test/null_check_calls.ts.strict.json b/linter-4.2/test/null_check_calls.ts.strict.json index 9d5793764dd46a1404fda4ed4c2b2e76e5b0996b..4b6f2852dd29cd8acf45c216092ffd83f45f3ebb 100644 --- a/linter-4.2/test/null_check_calls.ts.strict.json +++ b/linter-4.2/test/null_check_calls.ts.strict.json @@ -14,19 +14,35 @@ "limitations under the License." ], "nodes": [ + { + "line": 43, + "column": 1, + "problem": "LimitedReturnTypeInference" + }, { "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 'null' 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 'null' 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 'null' 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 'null' is not assignable to parameter of type 'TaskGroup'." + "problem": "StrictDiagnostic" }, { "line": 37, "column": 5, - "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[]'." + "problem": "StrictDiagnostic" + }, + { + "line": 42, + "column": 20, + "problem": "StrictDiagnostic" + }, + { + "line": 43, + "column": 42, + "problem": "StrictDiagnostic" + }, + { + "line": 47, + "column": 5, + "problem": "StrictDiagnostic" } ] } \ No newline at end of file diff --git a/linter/src/TypeScriptLinter.ts b/linter/src/TypeScriptLinter.ts index ea461af04a92274d4da8b40833635b00880d692e..94be3f3bfef8c1cf59d6056100d13e1c765786ce 100644 --- a/linter/src/TypeScriptLinter.ts +++ b/linter/src/TypeScriptLinter.ts @@ -1564,18 +1564,25 @@ export class TypeScriptLinter { 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 '.*'\.$/; 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 '.*'\.$/; + let inLibCall = this.tsUtils.isLibraryType(this.tsTypeChecker.getTypeAtLocation(callExpr.expression)); + const chainCheck = (n: ts.DiagnosticMessageChain): boolean => { - if (n.code == TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE && - (n.messageText.match(TYPE_UNKNOWN_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE) || - n.messageText.match(TYPE_NULL_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE) || - n.messageText.match(TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE))) { - return false; + if (n.code == TYPE_0_IS_NOT_ASSIGNABLE_TO_TYPE_1_ERROR_CODE) { + if (n.messageText.match(TYPE_UNKNOWN_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE)) { + return false; + } + if (n.messageText.match(TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE)) { + return false; + } + if (inLibCall && n.messageText.match(TYPE_NULL_IS_NOT_ASSIGNABLE_TO_TYPE_1_RE)) { + return false; + } } return n.next == undefined ? true : chainCheck(n.next[0]); }; const msgCheck = (msg: string): boolean => { - if (this.tsUtils.isLibraryType(this.tsTypeChecker.getTypeAtLocation(callExpr.expression))) { + if (inLibCall) { const match = msg.match(ARGUMENT_OF_TYPE_NULL_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_RE) || msg.match(ARGUMENT_OF_TYPE_UNDEFINED_IS_NOT_ASSIGNABLE_TO_PARAMETER_OF_TYPE_1_RE); return !match; diff --git a/linter/test/null_check_calls.ts b/linter/test/null_check_calls.ts index 18bcaa037e2ad9769e4af2be55978dc84bed139a..b79ac3409082bfe1f5eff1e2b2d119df6c8af985 100644 --- a/linter/test/null_check_calls.ts +++ b/linter/test/null_check_calls.ts @@ -37,3 +37,11 @@ function fff(a: Array): void {} fff(null); ff(null); f(null); + + +((a: number) => a)(null); +(a: null | number) => ((b: number) => b)(a); + +function inc(a: number) { return a + 1 } +function neg2null(x: number) { return x > 0 ? x : null } +inc(neg2null(-1)); diff --git a/linter/test/null_check_calls.ts.relax.json b/linter/test/null_check_calls.ts.relax.json index 9d5793764dd46a1404fda4ed4c2b2e76e5b0996b..b40e140bace347bfea81d26b7ad363e38c4e7b03 100644 --- a/linter/test/null_check_calls.ts.relax.json +++ b/linter/test/null_check_calls.ts.relax.json @@ -17,16 +17,27 @@ { "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 'null' 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 'null' 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 'null' 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 'null' is not assignable to parameter of type 'TaskGroup'." + "problem": "StrictDiagnostic" }, { "line": 37, "column": 5, - "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[]'." + "problem": "StrictDiagnostic" + }, + { + "line": 42, + "column": 20, + "problem": "StrictDiagnostic" + }, + { + "line": 43, + "column": 42, + "problem": "StrictDiagnostic" + }, + { + "line": 47, + "column": 5, + "problem": "StrictDiagnostic" } ] } \ No newline at end of file diff --git a/linter/test/null_check_calls.ts.strict.json b/linter/test/null_check_calls.ts.strict.json index 9d5793764dd46a1404fda4ed4c2b2e76e5b0996b..4b6f2852dd29cd8acf45c216092ffd83f45f3ebb 100644 --- a/linter/test/null_check_calls.ts.strict.json +++ b/linter/test/null_check_calls.ts.strict.json @@ -14,19 +14,35 @@ "limitations under the License." ], "nodes": [ + { + "line": 43, + "column": 1, + "problem": "LimitedReturnTypeInference" + }, { "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 'null' 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 'null' 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 'null' 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 'null' is not assignable to parameter of type 'TaskGroup'." + "problem": "StrictDiagnostic" }, { "line": 37, "column": 5, - "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[]'." + "problem": "StrictDiagnostic" + }, + { + "line": 42, + "column": 20, + "problem": "StrictDiagnostic" + }, + { + "line": 43, + "column": 42, + "problem": "StrictDiagnostic" + }, + { + "line": 47, + "column": 5, + "problem": "StrictDiagnostic" } ] } \ No newline at end of file