Skip to content

Commit

Permalink
fix(eslint-plugin): [promise-function-async] allow any as return value
Browse files Browse the repository at this point in the history
  • Loading branch information
madbence committed May 27, 2019
1 parent cca1714 commit 2ab678a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/eslint-plugin/docs/rules/promise-function-async.md
Expand Up @@ -36,6 +36,7 @@ async function functionDeturnsPromise() {

Options may be provided as an object with:

- `allowAny` to indicate that `any` or `unknown` shouldn't be considered Promises (`true` by default).
- `allowedPromiseNames` to indicate any extra names of classes or interfaces to be considered Promises when returned.

In addition, each of the following properties may be provided, and default to `true`:
Expand All @@ -50,6 +51,7 @@ In addition, each of the following properties may be provided, and default to `t
"@typescript-eslint/promise-function-async": [
"error",
{
"allowAny": true,
"allowedPromiseNames": ["Thenable"],
"checkArrowFunctions": true,
"checkFunctionDeclarations": true,
Expand Down
14 changes: 14 additions & 0 deletions packages/eslint-plugin/src/rules/promise-function-async.ts
@@ -1,8 +1,11 @@
import ts from 'typescript';
import { isTypeFlagSet } from 'tsutils';
import { TSESTree } from '@typescript-eslint/experimental-utils';
import * as util from '../util';

type Options = [
{
allowAny?: boolean;
allowedPromiseNames?: string[];
checkArrowFunctions?: boolean;
checkFunctionDeclarations?: boolean;
Expand All @@ -29,6 +32,9 @@ export default util.createRule<Options, MessageIds>({
{
type: 'object',
properties: {
allowAny: {
type: 'boolean',
},
allowedPromiseNames: {
type: 'array',
items: {
Expand All @@ -54,6 +60,7 @@ export default util.createRule<Options, MessageIds>({
},
defaultOptions: [
{
allowAny: true,
allowedPromiseNames: [],
checkArrowFunctions: true,
checkFunctionDeclarations: true,
Expand All @@ -65,6 +72,7 @@ export default util.createRule<Options, MessageIds>({
context,
[
{
allowAny,
allowedPromiseNames,
checkArrowFunctions,
checkFunctionDeclarations,
Expand All @@ -90,6 +98,12 @@ export default util.createRule<Options, MessageIds>({
}
const returnType = checker.getReturnTypeOfSignature(signatures[0]);

if (
allowAny &&
isTypeFlagSet(returnType, ts.TypeFlags.Any | ts.TypeFlags.Unknown)
) {
return;
}
if (!util.containsTypeByName(returnType, allAllowedPromiseNames)) {
return;
}
Expand Down
58 changes: 58 additions & 0 deletions packages/eslint-plugin/tests/rules/promise-function-async.test.ts
Expand Up @@ -16,6 +16,16 @@ const ruleTester = new RuleTester({
ruleTester.run('promise-function-async', rule, {
valid: [
`
function returnsAny(): any {
return 0;
}
`,
`
function returnsUnknown(): unknown {
return 0;
}
`,
`
const nonAsyncNonPromiseArrowFunction = (n: number) => n;
`,
`
Expand All @@ -31,6 +41,10 @@ const asyncPromiseFunctionExpressionB = async function() { return new Promise<vo
class Test {
public nonAsyncNonPromiseArrowFunction = (n: number) => n;
public constructor() {
return 0 as any;
}
public nonAsyncNonPromiseMethod() {
return 0;
}
Expand All @@ -42,6 +56,14 @@ class Test {
public async asyncPromiseMethodB() {
return new Promise<void>();
}
public get getterThatReturnsAny() {
return 0 as any;
}
public set setterThatReturnsAny(value: number) {
return 0 as any;
}
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/227
Expand Down Expand Up @@ -287,5 +309,41 @@ const returnAllowedType = () => new PromiseType();
},
],
},
{
code: `
function returnsAny(): any {
return 0;
}
`,
options: [
{
allowAny: false,
},
],
errors: [
{
line: 2,
messageId,
},
],
},
{
code: `
function returnsUnknown(): unknown {
return 0;
}
`,
options: [
{
allowAny: false,
},
],
errors: [
{
line: 2,
messageId,
},
],
},
],
});

0 comments on commit 2ab678a

Please sign in to comment.