From d690c8dff3636d8c8a9a38bd422e0bedbd1d72cb Mon Sep 17 00:00:00 2001 From: thomas michael wallace Date: Sun, 18 Oct 2020 19:48:42 +0100 Subject: [PATCH] fix(eslint-plugin): [return-await] do not auto-fix when type is `any`/`unknown` (#2671) --- .../eslint-plugin/src/rules/return-await.ts | 20 ++++++- .../tests/rules/return-await.test.ts | 52 ++++++++++++++++++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/return-await.ts b/packages/eslint-plugin/src/rules/return-await.ts index edbfcb593ed..c8e7f102b09 100644 --- a/packages/eslint-plugin/src/rules/return-await.ts +++ b/packages/eslint-plugin/src/rules/return-await.ts @@ -1,7 +1,7 @@ import { AST_NODE_TYPES, - TSESTree, TSESLint, + TSESTree, } from '@typescript-eslint/experimental-utils'; import * as tsutils from 'tsutils'; import * as ts from 'typescript'; @@ -170,10 +170,26 @@ export default util.createRule({ } if (isAwait && !isThenable) { + // any/unknown could be thenable; do not auto-fix + const useAutoFix = !( + util.isTypeAnyType(type) || util.isTypeUnknownType(type) + ); + const fix = (fixer: TSESLint.RuleFixer): TSESLint.RuleFix | null => + removeAwait(fixer, node); + context.report({ messageId: 'nonPromiseAwait', node, - fix: fixer => removeAwait(fixer, node), + ...(useAutoFix + ? { fix } + : { + suggest: [ + { + messageId: 'nonPromiseAwait', + fix, + }, + ], + }), }); return; } diff --git a/packages/eslint-plugin/tests/rules/return-await.test.ts b/packages/eslint-plugin/tests/rules/return-await.test.ts index 887768a6106..73056f6091c 100644 --- a/packages/eslint-plugin/tests/rules/return-await.test.ts +++ b/packages/eslint-plugin/tests/rules/return-await.test.ts @@ -1,5 +1,5 @@ import rule from '../../src/rules/return-await'; -import { getFixturesRootDir, RuleTester, noFormat } from '../RuleTester'; +import { getFixturesRootDir, noFormat, RuleTester } from '../RuleTester'; const rootDir = getFixturesRootDir(); @@ -312,6 +312,56 @@ ruleTester.run('return-await', rule, { }, ], }, + { + code: ` +const fn = (): any => null; +async function test() { + return await fn(); +} + `.trimRight(), + errors: [ + { + line: 4, + messageId: 'nonPromiseAwait', + suggestions: [ + { + messageId: 'nonPromiseAwait', + output: ` +const fn = (): any => null; +async function test() { + return fn(); +} + `.trimRight(), + }, + ], + }, + ], + }, + { + code: ` +const fn = (): unknown => null; +async function test() { + return await fn(); +} + `.trimRight(), + errors: [ + { + line: 4, + messageId: 'nonPromiseAwait', + suggestions: [ + { + messageId: 'nonPromiseAwait', + output: ` +const fn = (): unknown => null; +async function test() { + return fn(); +} + `.trimRight(), + }, + ], + }, + ], + }, { code: 'const test = async () => await Promise.resolve(1);', output: 'const test = async () => Promise.resolve(1);',