Skip to content

Commit

Permalink
fix(eslint-plugin): [return-await] do not auto-fix when type is any
Browse files Browse the repository at this point in the history
…/`unknown` (#2671)
  • Loading branch information
thomasmichaelwallace committed Oct 18, 2020
1 parent 90a5878 commit d690c8d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
20 changes: 18 additions & 2 deletions 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';
Expand Down Expand Up @@ -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;
}
Expand Down
52 changes: 51 additions & 1 deletion 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();

Expand Down Expand Up @@ -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);',
Expand Down

0 comments on commit d690c8d

Please sign in to comment.