Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [return-await] do not auto-fix when type is any/unknown #2671

Merged
merged 5 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/eslint-plugin/src/rules/return-await.ts
Original file line number Diff line number Diff line change
@@ -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 @@ -169,11 +169,24 @@ export default util.createRule({
return;
}

// any could be thenable; do not auto-fix
const useAutoFix = !util.isTypeAnyType(type);
const removeFix = (fixer: TSESLint.RuleFixer): TSESLint.RuleFix | null =>
removeAwait(fixer, node);
if (isAwait && !isThenable) {
context.report({
thomasmichaelwallace marked this conversation as resolved.
Show resolved Hide resolved
messageId: 'nonPromiseAwait',
node,
fix: fixer => removeAwait(fixer, node),
...(useAutoFix
? { fix: removeFix }
: {
suggest: [
{
messageId: 'nonPromiseAwait',
fix: removeFix,
},
],
}),
});
return;
}
Expand Down
23 changes: 22 additions & 1 deletion packages/eslint-plugin/tests/rules/return-await.test.ts
Original file line number Diff line number Diff line change
@@ -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,27 @@ ruleTester.run('return-await', rule, {
},
],
},
{
code: `
const fn = (): any => null;
async function test() {
return await fn();
}
`,
// output matches input because return type any is suggestion only
output: `
const fn = (): any => null;
async function test() {
return await fn();
}
`,
errors: [
{
line: 4,
messageId: 'nonPromiseAwait',
thomasmichaelwallace marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
{
code: 'const test = async () => await Promise.resolve(1);',
output: 'const test = async () => Promise.resolve(1);',
Expand Down