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 authored and JamesHenry committed Jun 19, 2019
1 parent 48548ea commit 9a387b0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/eslint-plugin/docs/rules/promise-function-async.md
Original file line number Diff line number Diff line change
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 (`false` 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
10 changes: 9 additions & 1 deletion packages/eslint-plugin/src/rules/promise-function-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as util from '../util';

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

if (!util.containsTypeByName(returnType, allAllowedPromiseNames)) {
if (
!util.containsTypeByName(returnType, allowAny!, allAllowedPromiseNames)
) {
return;
}

Expand Down
7 changes: 4 additions & 3 deletions packages/eslint-plugin/src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import ts from 'typescript';
*/
export function containsTypeByName(
type: ts.Type,
allowAny: boolean,
allowedNames: Set<string>,
): boolean {
if (isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return true;
return !allowAny;
}

if (isTypeReference(type)) {
Expand All @@ -30,13 +31,13 @@ export function containsTypeByName(
}

if (isUnionOrIntersectionType(type)) {
return type.types.some(t => containsTypeByName(t, allowedNames));
return type.types.some(t => containsTypeByName(t, allowAny, allowedNames));
}

const bases = type.getBaseTypes();
return (
typeof bases !== 'undefined' &&
bases.some(t => containsTypeByName(t, allowedNames))
bases.some(t => containsTypeByName(t, allowAny, allowedNames))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const asyncPromiseFunctionExpressionB = async function() { return new Promise<vo
`
class Test {
public nonAsyncNonPromiseArrowFunction = (n: number) => n;
public nonAsyncNonPromiseMethod() {
return 0;
}
Expand Down Expand Up @@ -71,10 +70,58 @@ const invalidAsyncModifiers = {
`export function valid(n: number) { return n; }`,
`export default function invalid(n: number) { return n; }`,
`class Foo { constructor() { } }`,
{
code: `
function returnsAny(): any {
return 0;
}
`,
options: [
{
allowAny: true,
},
],
},
{
code: `
function returnsUnknown(): unknown {
return 0;
}
`,
options: [
{
allowAny: true,
},
],
},
],
invalid: [
{
code: `
function returnsAny(): any {
return 0;
}
`,
errors: [
{
messageId,
},
],
},
{
code: `
function returnsUnknown(): unknown {
return 0;
}
`,
errors: [
{
messageId,
},
],
},
{
code: `
const nonAsyncPromiseFunctionExpressionA = function(p: Promise<void>) { return p; };
`,
errors: [
Expand Down

0 comments on commit 9a387b0

Please sign in to comment.