Skip to content

Commit 9a387b0

Browse files
madbenceJamesHenry
authored andcommittedJun 19, 2019
fix(eslint-plugin): [promise-function-async] allow any as return value (#553)
1 parent 48548ea commit 9a387b0

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed
 

‎packages/eslint-plugin/docs/rules/promise-function-async.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async function functionDeturnsPromise() {
3636

3737
Options may be provided as an object with:
3838

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

4142
In addition, each of the following properties may be provided, and default to `true`:
@@ -50,6 +51,7 @@ In addition, each of the following properties may be provided, and default to `t
5051
"@typescript-eslint/promise-function-async": [
5152
"error",
5253
{
54+
"allowAny": true,
5355
"allowedPromiseNames": ["Thenable"],
5456
"checkArrowFunctions": true,
5557
"checkFunctionDeclarations": true,

‎packages/eslint-plugin/src/rules/promise-function-async.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as util from '../util';
33

44
type Options = [
55
{
6+
allowAny?: boolean;
67
allowedPromiseNames?: string[];
78
checkArrowFunctions?: boolean;
89
checkFunctionDeclarations?: boolean;
@@ -29,6 +30,9 @@ export default util.createRule<Options, MessageIds>({
2930
{
3031
type: 'object',
3132
properties: {
33+
allowAny: {
34+
type: 'boolean',
35+
},
3236
allowedPromiseNames: {
3337
type: 'array',
3438
items: {
@@ -54,6 +58,7 @@ export default util.createRule<Options, MessageIds>({
5458
},
5559
defaultOptions: [
5660
{
61+
allowAny: false,
5762
allowedPromiseNames: [],
5863
checkArrowFunctions: true,
5964
checkFunctionDeclarations: true,
@@ -65,6 +70,7 @@ export default util.createRule<Options, MessageIds>({
6570
context,
6671
[
6772
{
73+
allowAny,
6874
allowedPromiseNames,
6975
checkArrowFunctions,
7076
checkFunctionDeclarations,
@@ -90,7 +96,9 @@ export default util.createRule<Options, MessageIds>({
9096
}
9197
const returnType = checker.getReturnTypeOfSignature(signatures[0]);
9298

93-
if (!util.containsTypeByName(returnType, allAllowedPromiseNames)) {
99+
if (
100+
!util.containsTypeByName(returnType, allowAny!, allAllowedPromiseNames)
101+
) {
94102
return;
95103
}
96104

‎packages/eslint-plugin/src/util/types.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import ts from 'typescript';
1212
*/
1313
export function containsTypeByName(
1414
type: ts.Type,
15+
allowAny: boolean,
1516
allowedNames: Set<string>,
1617
): boolean {
1718
if (isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
18-
return true;
19+
return !allowAny;
1920
}
2021

2122
if (isTypeReference(type)) {
@@ -30,13 +31,13 @@ export function containsTypeByName(
3031
}
3132

3233
if (isUnionOrIntersectionType(type)) {
33-
return type.types.some(t => containsTypeByName(t, allowedNames));
34+
return type.types.some(t => containsTypeByName(t, allowAny, allowedNames));
3435
}
3536

3637
const bases = type.getBaseTypes();
3738
return (
3839
typeof bases !== 'undefined' &&
39-
bases.some(t => containsTypeByName(t, allowedNames))
40+
bases.some(t => containsTypeByName(t, allowAny, allowedNames))
4041
);
4142
}
4243

‎packages/eslint-plugin/tests/rules/promise-function-async.test.ts

+48-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const asyncPromiseFunctionExpressionB = async function() { return new Promise<vo
3030
`
3131
class Test {
3232
public nonAsyncNonPromiseArrowFunction = (n: number) => n;
33-
3433
public nonAsyncNonPromiseMethod() {
3534
return 0;
3635
}
@@ -71,10 +70,58 @@ const invalidAsyncModifiers = {
7170
`export function valid(n: number) { return n; }`,
7271
`export default function invalid(n: number) { return n; }`,
7372
`class Foo { constructor() { } }`,
73+
{
74+
code: `
75+
function returnsAny(): any {
76+
return 0;
77+
}
78+
`,
79+
options: [
80+
{
81+
allowAny: true,
82+
},
83+
],
84+
},
85+
{
86+
code: `
87+
function returnsUnknown(): unknown {
88+
return 0;
89+
}
90+
`,
91+
options: [
92+
{
93+
allowAny: true,
94+
},
95+
],
96+
},
7497
],
7598
invalid: [
7699
{
77100
code: `
101+
function returnsAny(): any {
102+
return 0;
103+
}
104+
`,
105+
errors: [
106+
{
107+
messageId,
108+
},
109+
],
110+
},
111+
{
112+
code: `
113+
function returnsUnknown(): unknown {
114+
return 0;
115+
}
116+
`,
117+
errors: [
118+
{
119+
messageId,
120+
},
121+
],
122+
},
123+
{
124+
code: `
78125
const nonAsyncPromiseFunctionExpressionA = function(p: Promise<void>) { return p; };
79126
`,
80127
errors: [

0 commit comments

Comments
 (0)
Please sign in to comment.