-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
consistent-return.ts
117 lines (105 loc) · 3.41 KB
/
consistent-return.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
import { type TSESTree } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';
import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../util';
import { createRule, getParserServices, isTypeFlagSet } from '../util';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
const baseRule = getESLintCoreRule('consistent-return');
type Options = InferOptionsTypeFromRule<typeof baseRule>;
type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>;
type FunctionNode =
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.ArrowFunctionExpression;
export default createRule<Options, MessageIds>({
name: 'consistent-return',
meta: {
type: 'suggestion',
docs: {
description:
'Require `return` statements to either always or never specify values',
extendsBaseRule: true,
requiresTypeChecking: true,
},
hasSuggestions: baseRule.meta.hasSuggestions,
schema: baseRule.meta.schema,
messages: baseRule.meta.messages,
},
defaultOptions: [],
create(context) {
const services = getParserServices(context);
const checker = services.program.getTypeChecker();
const rules = baseRule.create(context);
const functions: FunctionNode[] = [];
function enterFunction(node: FunctionNode): void {
functions.push(node);
}
function exitFunction(): void {
functions.pop();
}
function getCurrentFunction(): FunctionNode | null {
return functions[functions.length - 1] ?? null;
}
function isReturnPromiseVoid(
node: FunctionNode,
signature: ts.Signature,
): boolean {
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
const returnType = signature.getReturnType();
if (
tsutils.isThenableType(checker, tsNode, returnType) &&
tsutils.isTypeReference(returnType)
) {
const typeArgs = returnType.typeArguments;
const hasVoid = !!typeArgs?.some(typeArg =>
isTypeFlagSet(typeArg, ts.TypeFlags.Void),
);
return hasVoid;
}
return false;
}
function isReturnVoidOrThenableVoid(node: FunctionNode): boolean {
const functionType = services.getTypeAtLocation(node);
const callSignatures = functionType.getCallSignatures();
return callSignatures.some(signature => {
if (node.async) {
return isReturnPromiseVoid(node, signature);
}
const returnType = signature.getReturnType();
return isTypeFlagSet(returnType, ts.TypeFlags.Void);
});
}
return {
...rules,
FunctionDeclaration: enterFunction,
FunctionExpression: enterFunction,
ArrowFunctionExpression: enterFunction,
'FunctionDeclaration:exit'(node): void {
exitFunction();
rules['FunctionDeclaration:exit'](node);
},
'FunctionExpression:exit'(node): void {
exitFunction();
rules['FunctionExpression:exit'](node);
},
'ArrowFunctionExpression:exit'(node): void {
exitFunction();
rules['ArrowFunctionExpression:exit'](node);
},
ReturnStatement(node): void {
const functionNode = getCurrentFunction();
if (
!node.argument &&
functionNode &&
isReturnVoidOrThenableVoid(functionNode)
) {
return;
}
rules.ReturnStatement(node);
},
};
},
});