/
no-type-alias.ts
281 lines (269 loc) · 7.82 KB
/
no-type-alias.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
type Values =
| 'always'
| 'never'
| 'in-unions'
| 'in-intersections'
| 'in-unions-and-intersections';
const enumValues: Values[] = [
'always',
'never',
'in-unions',
'in-intersections',
'in-unions-and-intersections',
];
type Options = [
{
allowAliases?: Values;
allowCallbacks?: 'always' | 'never';
allowLiterals?: Values;
allowMappedTypes?: Values;
allowTupleTypes?: Values;
},
];
type MessageIds = 'noTypeAlias' | 'noCompositionAlias';
type CompositionType =
| AST_NODE_TYPES.TSUnionType
| AST_NODE_TYPES.TSIntersectionType;
interface TypeWithLabel {
node: TSESTree.Node;
compositionType: CompositionType | null;
}
export default util.createRule<Options, MessageIds>({
name: 'no-type-alias',
meta: {
type: 'suggestion',
docs: {
description: 'Disallow the use of type aliases',
category: 'Stylistic Issues',
// too opinionated to be recommended
recommended: false,
},
messages: {
noTypeAlias: 'Type {{alias}} are not allowed.',
noCompositionAlias:
'{{typeName}} in {{compositionType}} types are not allowed.',
},
schema: [
{
type: 'object',
properties: {
allowAliases: {
enum: enumValues,
},
allowCallbacks: {
enum: ['always', 'never'],
},
allowLiterals: {
enum: enumValues,
},
allowMappedTypes: {
enum: enumValues,
},
allowTupleTypes: {
enum: enumValues,
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
{
allowAliases: 'never',
allowCallbacks: 'never',
allowLiterals: 'never',
allowMappedTypes: 'never',
allowTupleTypes: 'never',
},
],
create(
context,
[
{
allowAliases,
allowCallbacks,
allowLiterals,
allowMappedTypes,
allowTupleTypes,
},
],
) {
const unions = ['always', 'in-unions', 'in-unions-and-intersections'];
const intersections = [
'always',
'in-intersections',
'in-unions-and-intersections',
];
const compositions = [
'in-unions',
'in-intersections',
'in-unions-and-intersections',
];
const aliasTypes = new Set([
AST_NODE_TYPES.TSArrayType,
AST_NODE_TYPES.TSTypeReference,
AST_NODE_TYPES.TSLiteralType,
AST_NODE_TYPES.TSTypeQuery,
AST_NODE_TYPES.TSIndexedAccessType,
]);
/**
* Determines if the composition type is supported by the allowed flags.
* @param isTopLevel a flag indicating this is the top level node.
* @param compositionType the composition type (either TSUnionType or TSIntersectionType)
* @param allowed the currently allowed flags.
*/
function isSupportedComposition(
isTopLevel: boolean,
compositionType: CompositionType | null,
allowed: string,
): boolean {
return (
!compositions.includes(allowed) ||
(!isTopLevel &&
((compositionType === AST_NODE_TYPES.TSUnionType &&
unions.includes(allowed)) ||
(compositionType === AST_NODE_TYPES.TSIntersectionType &&
intersections.includes(allowed))))
);
}
/**
* Gets the message to be displayed based on the node type and whether the node is a top level declaration.
* @param node the location
* @param compositionType the type of composition this alias is part of (undefined if not
* part of a composition)
* @param isRoot a flag indicating we are dealing with the top level declaration.
* @param type the kind of type alias being validated.
*/
function reportError(
node: TSESTree.Node,
compositionType: CompositionType | null,
isRoot: boolean,
type: string,
): void {
if (isRoot) {
return context.report({
node,
messageId: 'noTypeAlias',
data: {
alias: type.toLowerCase(),
},
});
}
return context.report({
node,
messageId: 'noCompositionAlias',
data: {
compositionType:
compositionType === AST_NODE_TYPES.TSUnionType
? 'union'
: 'intersection',
typeName: type,
},
});
}
const isValidTupleType = (type: TypeWithLabel): boolean => {
if (type.node.type === AST_NODE_TYPES.TSTupleType) {
return true;
}
if (type.node.type === AST_NODE_TYPES.TSTypeOperator) {
if (
['keyof', 'readonly'].includes(type.node.operator) &&
type.node.typeAnnotation &&
type.node.typeAnnotation.type === AST_NODE_TYPES.TSTupleType
) {
return true;
}
}
return false;
};
const checkAndReport = (
optionValue: Values,
isTopLevel: boolean,
type: TypeWithLabel,
label: string,
): void => {
if (
optionValue === 'never' ||
!isSupportedComposition(isTopLevel, type.compositionType, optionValue)
) {
reportError(type.node, type.compositionType, isTopLevel, label);
}
};
/**
* Validates the node looking for aliases, callbacks and literals.
* @param node the node to be validated.
* @param compositionType the type of composition this alias is part of (null if not
* part of a composition)
* @param isTopLevel a flag indicating this is the top level node.
*/
function validateTypeAliases(
type: TypeWithLabel,
isTopLevel = false,
): void {
if (type.node.type === AST_NODE_TYPES.TSFunctionType) {
// callback
if (allowCallbacks === 'never') {
reportError(type.node, type.compositionType, isTopLevel, 'Callbacks');
}
} else if (type.node.type === AST_NODE_TYPES.TSTypeLiteral) {
// literal object type
checkAndReport(allowLiterals!, isTopLevel, type, 'Literals');
} else if (type.node.type === AST_NODE_TYPES.TSMappedType) {
// mapped type
checkAndReport(allowMappedTypes!, isTopLevel, type, 'Mapped types');
} else if (isValidTupleType(type)) {
// tuple types
checkAndReport(allowTupleTypes!, isTopLevel, type, 'Tuple Types');
} else if (
type.node.type.endsWith('Keyword') ||
aliasTypes.has(type.node.type)
) {
// alias / keyword
checkAndReport(allowAliases!, isTopLevel, type, 'Aliases');
} else {
// unhandled type - shouldn't happen
reportError(type.node, type.compositionType, isTopLevel, 'Unhandled');
}
}
/**
* Flatten the given type into an array of its dependencies
*/
function getTypes(
node: TSESTree.Node,
compositionType: CompositionType | null = null,
): TypeWithLabel[] {
if (
node.type === AST_NODE_TYPES.TSUnionType ||
node.type === AST_NODE_TYPES.TSIntersectionType
) {
return node.types.reduce<TypeWithLabel[]>((acc, type) => {
acc.push(...getTypes(type, node.type));
return acc;
}, []);
}
if (node.type === AST_NODE_TYPES.TSParenthesizedType) {
return getTypes(node.typeAnnotation, compositionType);
}
return [{ node, compositionType }];
}
return {
TSTypeAliasDeclaration(node): void {
const types = getTypes(node.typeAnnotation);
if (types.length === 1) {
// is a top level type annotation
validateTypeAliases(types[0], true);
} else {
// is a composition type
types.forEach(type => {
validateTypeAliases(type);
});
}
},
};
},
});