/
no-invalid-void-type.ts
208 lines (193 loc) · 6.35 KB
/
no-invalid-void-type.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
interface Options {
allowInGenericTypeArguments?: boolean | string[];
allowAsThisParameter?: boolean;
}
type MessageIds =
| 'invalidVoidForGeneric'
| 'invalidVoidNotReturnOrGeneric'
| 'invalidVoidNotReturn'
| 'invalidVoidNotReturnOrThisParam'
| 'invalidVoidNotReturnOrThisParamOrGeneric';
export default util.createRule<[Options], MessageIds>({
name: 'no-invalid-void-type',
meta: {
type: 'problem',
docs: {
description:
'Disallows usage of `void` type outside of generic or return types',
category: 'Best Practices',
recommended: false,
},
messages: {
invalidVoidForGeneric:
'{{ generic }} may not have void as a type variable',
invalidVoidNotReturnOrGeneric:
'void is only valid as a return type or generic type variable',
invalidVoidNotReturn: 'void is only valid as a return type',
invalidVoidNotReturnOrThisParam:
'void is only valid as return type or type of `this` parameter',
invalidVoidNotReturnOrThisParamOrGeneric:
'void is only valid as a return type or generic type variable or the type of a `this` parameter',
},
schema: [
{
type: 'object',
properties: {
allowInGenericTypeArguments: {
oneOf: [
{ type: 'boolean' },
{
type: 'array',
items: { type: 'string' },
minLength: 1,
},
],
},
allowAsThisParameter: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
{ allowInGenericTypeArguments: true, allowAsThisParameter: false },
],
create(context, [{ allowInGenericTypeArguments, allowAsThisParameter }]) {
const validParents: AST_NODE_TYPES[] = [
AST_NODE_TYPES.TSTypeAnnotation, //
];
const invalidGrandParents: AST_NODE_TYPES[] = [
AST_NODE_TYPES.TSPropertySignature,
AST_NODE_TYPES.CallExpression,
AST_NODE_TYPES.ClassProperty,
AST_NODE_TYPES.Identifier,
];
const validUnionMembers: AST_NODE_TYPES[] = [
AST_NODE_TYPES.TSVoidKeyword,
AST_NODE_TYPES.TSNeverKeyword,
];
if (allowInGenericTypeArguments === true) {
validParents.push(AST_NODE_TYPES.TSTypeParameterInstantiation);
}
/**
* @brief check if the given void keyword is used as a valid generic type
*
* reports if the type parametrized by void is not in the whitelist, or
* allowInGenericTypeArguments is false.
* no-op if the given void keyword is not used as generic type
*/
function checkGenericTypeArgument(node: TSESTree.TSVoidKeyword): void {
// only matches T<..., void, ...>
// extra check for precaution
/* istanbul ignore next */
if (
node.parent?.type !== AST_NODE_TYPES.TSTypeParameterInstantiation ||
node.parent.parent?.type !== AST_NODE_TYPES.TSTypeReference
) {
return;
}
// check whitelist
if (Array.isArray(allowInGenericTypeArguments)) {
const sourceCode = context.getSourceCode();
const fullyQualifiedName = sourceCode
.getText(node.parent.parent.typeName)
.replace(/ /gu, '');
if (
!allowInGenericTypeArguments
.map(s => s.replace(/ /gu, ''))
.includes(fullyQualifiedName)
) {
context.report({
messageId: 'invalidVoidForGeneric',
data: { generic: fullyQualifiedName },
node,
});
}
return;
}
if (!allowInGenericTypeArguments) {
context.report({
messageId: allowAsThisParameter
? 'invalidVoidNotReturnOrThisParam'
: 'invalidVoidNotReturn',
node,
});
}
}
/**
* @brief checks that a union containing void is valid
* @return true if every member of the union is specified as a valid type in
* validUnionMembers, or is a valid generic type parametrized by void
*/
function isValidUnionType(node: TSESTree.TSUnionType): boolean {
return node.types.every(
member =>
validUnionMembers.includes(member.type) ||
// allows any T<..., void, ...> here, checked by checkGenericTypeArgument
(member.type === AST_NODE_TYPES.TSTypeReference &&
member.typeParameters?.type ===
AST_NODE_TYPES.TSTypeParameterInstantiation &&
member.typeParameters?.params
.map(param => param.type)
.includes(AST_NODE_TYPES.TSVoidKeyword)),
);
}
return {
TSVoidKeyword(node: TSESTree.TSVoidKeyword): void {
/* istanbul ignore next */
if (!node.parent?.parent) {
return;
}
// checks T<..., void, ...> against specification of allowInGenericArguments option
if (
node.parent.type === AST_NODE_TYPES.TSTypeParameterInstantiation &&
node.parent.parent.type === AST_NODE_TYPES.TSTypeReference
) {
checkGenericTypeArgument(node);
return;
}
// union w/ void must contain types from validUnionMembers, or a valid generic void type
if (
node.parent.type === AST_NODE_TYPES.TSUnionType &&
isValidUnionType(node.parent)
) {
return;
}
// this parameter is ok to be void.
if (
allowAsThisParameter &&
node.parent.type === AST_NODE_TYPES.TSTypeAnnotation &&
node.parent.parent.type === AST_NODE_TYPES.Identifier &&
node.parent.parent.name === 'this'
) {
return;
}
// default cases
if (
validParents.includes(node.parent.type) &&
!invalidGrandParents.includes(node.parent.parent.type)
) {
return;
}
context.report({
messageId:
allowInGenericTypeArguments && allowAsThisParameter
? 'invalidVoidNotReturnOrThisParamOrGeneric'
: allowInGenericTypeArguments
? 'invalidVoidNotReturnOrGeneric'
: allowAsThisParameter
? 'invalidVoidNotReturnOrThisParam'
: 'invalidVoidNotReturn',
node,
});
},
};
},
});