From f4172f1f311c40d47b3086b1ee0cf216aaf14d67 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Fri, 26 Apr 2024 21:41:40 +0900 Subject: [PATCH] fix(eslint-plugin): [no-unsafe-return] handle union types --- .../src/rules/no-unsafe-return.ts | 2 +- .../tests/rules/no-unsafe-return.test.ts | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-unsafe-return.ts b/packages/eslint-plugin/src/rules/no-unsafe-return.ts index fa301bafa65..ef308450bd8 100644 --- a/packages/eslint-plugin/src/rules/no-unsafe-return.ts +++ b/packages/eslint-plugin/src/rules/no-unsafe-return.ts @@ -104,7 +104,7 @@ export default createRule({ // If there is an explicit type annotation *and* that type matches the actual // function return type, we shouldn't complain (it's intentional, even if unsafe) if (functionTSNode.type) { - for (const signature of functionType.getCallSignatures()) { + for (const signature of tsutils.getCallSignaturesOfType(functionType)) { if ( returnNodeType === signature.getReturnType() || isTypeFlagSet( diff --git a/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts b/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts index 42a798ffe11..36c6103bbc2 100644 --- a/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts @@ -45,6 +45,14 @@ function foo() { function foo(): any { return {} as any; } + `, + ` +declare function foo(arg: () => any): void; +foo((): any => 'foo' as any); + `, + ` +declare function foo(arg: null | (() => any)): void; +foo((): any => 'foo' as any); `, // explicit any array return type is allowed, if you want to be unsafe like that ` @@ -408,5 +416,19 @@ function bar() { }, ], }, + { + code: ` +declare function foo(arg: null | (() => any)): void; +foo(() => 'foo' as any); + `, + errors: [ + { + messageId: 'unsafeReturn', + line: 3, + column: 11, + endColumn: 23, + }, + ], + }, ], });