Skip to content
Permalink
Browse files

fix(eslint-plugin): [return-await] do not auto-fix when type is `any`…

…/`unknown` (#2671)
  • Loading branch information
thomasmichaelwallace committed Oct 18, 2020
1 parent 90a5878 commit d690c8dff3636d8c8a9a38bd422e0bedbd1d72cb
@@ -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;
}
@@ -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);',

0 comments on commit d690c8d

Please sign in to comment.
You can’t perform that action at this time.