/
require-await.ts
138 lines (121 loc) · 3.74 KB
/
require-await.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
TSESTree,
TSESLint,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/require-await';
import * as tsutils from 'tsutils';
import ts from 'typescript';
import * as util from '../util';
type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
interface ScopeInfo {
upper: ScopeInfo | null;
returnsPromise: boolean;
}
export default util.createRule<Options, MessageIds>({
name: 'require-await',
meta: {
type: 'suggestion',
docs: {
description: 'Disallow async functions which have no `await` expression',
category: 'Best Practices',
recommended: false,
},
schema: baseRule.meta.schema,
messages: baseRule.meta.messages,
},
defaultOptions: [],
create(context) {
const rules = baseRule.create(context);
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
let scopeInfo: ScopeInfo | null = null;
/**
* Push the scope info object to the stack.
*
* @returns {void}
*/
function enterFunction(
node:
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.ArrowFunctionExpression,
) {
scopeInfo = {
upper: scopeInfo,
returnsPromise: false,
};
switch (node.type) {
case AST_NODE_TYPES.FunctionDeclaration:
rules.FunctionDeclaration(node);
break;
case AST_NODE_TYPES.FunctionExpression:
rules.FunctionExpression(node);
break;
case AST_NODE_TYPES.ArrowFunctionExpression:
rules.ArrowFunctionExpression(node);
break;
}
}
/**
* Pop the top scope info object from the stack.
* Passes through to the base rule if the function doesn't return a promise
*
* @param {ASTNode} node - The node exiting
* @returns {void}
*/
function exitFunction(
node:
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.ArrowFunctionExpression,
) {
if (scopeInfo) {
if (!scopeInfo.returnsPromise) {
switch (node.type) {
case AST_NODE_TYPES.FunctionDeclaration:
rules['FunctionDeclaration:exit'](node);
break;
case AST_NODE_TYPES.FunctionExpression:
rules['FunctionExpression:exit'](node);
break;
case AST_NODE_TYPES.ArrowFunctionExpression:
rules['ArrowFunctionExpression:exit'](node);
break;
}
}
scopeInfo = scopeInfo.upper;
}
}
return {
'FunctionDeclaration[async = true]': enterFunction,
'FunctionExpression[async = true]': enterFunction,
'ArrowFunctionExpression[async = true]': enterFunction,
'FunctionDeclaration[async = true]:exit': exitFunction,
'FunctionExpression[async = true]:exit': exitFunction,
'ArrowFunctionExpression[async = true]:exit': exitFunction,
ReturnStatement(node: TSESTree.ReturnStatement) {
if (!scopeInfo) {
return;
}
const { expression } = parserServices.esTreeNodeToTSNodeMap.get(
node,
) as ts.ReturnStatement;
if (!expression) {
return;
}
const type = checker.getTypeAtLocation(expression);
if (tsutils.isThenableType(checker, expression, type)) {
scopeInfo.returnsPromise = true;
}
},
AwaitExpression: rules.AwaitExpression as TSESLint.RuleFunction<
TSESTree.Node
>,
ForOfStatement: rules.ForOfStatement as TSESLint.RuleFunction<
TSESTree.Node
>,
};
},
});