Skip to content

Commit d690c8d

Browse files
fix(eslint-plugin): [return-await] do not auto-fix when type is any/unknown (typescript-eslint#2671)
1 parent 90a5878 commit d690c8d

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

packages/eslint-plugin/src/rules/return-await.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
AST_NODE_TYPES,
3-
TSESTree,
43
TSESLint,
4+
TSESTree,
55
} from '@typescript-eslint/experimental-utils';
66
import * as tsutils from 'tsutils';
77
import * as ts from 'typescript';
@@ -170,10 +170,26 @@ export default util.createRule({
170170
}
171171

172172
if (isAwait && !isThenable) {
173+
// any/unknown could be thenable; do not auto-fix
174+
const useAutoFix = !(
175+
util.isTypeAnyType(type) || util.isTypeUnknownType(type)
176+
);
177+
const fix = (fixer: TSESLint.RuleFixer): TSESLint.RuleFix | null =>
178+
removeAwait(fixer, node);
179+
173180
context.report({
174181
messageId: 'nonPromiseAwait',
175182
node,
176-
fix: fixer => removeAwait(fixer, node),
183+
...(useAutoFix
184+
? { fix }
185+
: {
186+
suggest: [
187+
{
188+
messageId: 'nonPromiseAwait',
189+
fix,
190+
},
191+
],
192+
}),
177193
});
178194
return;
179195
}

packages/eslint-plugin/tests/rules/return-await.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import rule from '../../src/rules/return-await';
2-
import { getFixturesRootDir, RuleTester, noFormat } from '../RuleTester';
2+
import { getFixturesRootDir, noFormat, RuleTester } from '../RuleTester';
33

44
const rootDir = getFixturesRootDir();
55

@@ -312,6 +312,56 @@ ruleTester.run('return-await', rule, {
312312
},
313313
],
314314
},
315+
{
316+
code: `
317+
const fn = (): any => null;
318+
async function test() {
319+
return await fn();
320+
}
321+
`.trimRight(),
322+
errors: [
323+
{
324+
line: 4,
325+
messageId: 'nonPromiseAwait',
326+
suggestions: [
327+
{
328+
messageId: 'nonPromiseAwait',
329+
output: `
330+
const fn = (): any => null;
331+
async function test() {
332+
return fn();
333+
}
334+
`.trimRight(),
335+
},
336+
],
337+
},
338+
],
339+
},
340+
{
341+
code: `
342+
const fn = (): unknown => null;
343+
async function test() {
344+
return await fn();
345+
}
346+
`.trimRight(),
347+
errors: [
348+
{
349+
line: 4,
350+
messageId: 'nonPromiseAwait',
351+
suggestions: [
352+
{
353+
messageId: 'nonPromiseAwait',
354+
output: `
355+
const fn = (): unknown => null;
356+
async function test() {
357+
return fn();
358+
}
359+
`.trimRight(),
360+
},
361+
],
362+
},
363+
],
364+
},
315365
{
316366
code: 'const test = async () => await Promise.resolve(1);',
317367
output: 'const test = async () => Promise.resolve(1);',

0 commit comments

Comments
 (0)