/
typedef.ts
175 lines (164 loc) · 5.18 KB
/
typedef.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
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
const enum OptionKeys {
ArrayDestructuring = 'arrayDestructuring',
ArrowParameter = 'arrowParameter',
MemberVariableDeclaration = 'memberVariableDeclaration',
ObjectDestructuring = 'objectDestructuring',
Parameter = 'parameter',
PropertyDeclaration = 'propertyDeclaration',
VariableDeclaration = 'variableDeclaration',
}
type Options = { [k in OptionKeys]?: boolean };
type MessageIds = 'expectedTypedef' | 'expectedTypedefNamed';
export default util.createRule<[Options], MessageIds>({
name: 'typedef',
meta: {
docs: {
description: 'Requires type annotations to exist',
category: 'Stylistic Issues',
recommended: false,
},
messages: {
expectedTypedef: 'expected a type annotation',
expectedTypedefNamed: 'expected {{name}} to have a type annotation',
},
schema: [
{
type: 'object',
properties: {
[OptionKeys.ArrayDestructuring]: { type: 'boolean' },
[OptionKeys.ArrowParameter]: { type: 'boolean' },
[OptionKeys.MemberVariableDeclaration]: { type: 'boolean' },
[OptionKeys.ObjectDestructuring]: { type: 'boolean' },
[OptionKeys.Parameter]: { type: 'boolean' },
[OptionKeys.PropertyDeclaration]: { type: 'boolean' },
[OptionKeys.VariableDeclaration]: { type: 'boolean' },
},
},
],
type: 'suggestion',
},
defaultOptions: [
{
[OptionKeys.ArrowParameter]: true,
[OptionKeys.MemberVariableDeclaration]: true,
[OptionKeys.Parameter]: true,
[OptionKeys.PropertyDeclaration]: true,
},
],
create(context, [options]) {
function report(location: TSESTree.Node, name?: string) {
context.report({
node: location,
messageId: name ? 'expectedTypedefNamed' : 'expectedTypedef',
data: { name },
});
}
function getNodeName(node: TSESTree.Parameter | TSESTree.PropertyName) {
return node.type === AST_NODE_TYPES.Identifier ? node.name : undefined;
}
function checkParameters(params: TSESTree.Parameter[]) {
for (const param of params) {
let annotationNode: TSESTree.Node | undefined;
switch (param.type) {
case AST_NODE_TYPES.AssignmentPattern:
annotationNode = param.left;
break;
case AST_NODE_TYPES.TSParameterProperty:
annotationNode = param.parameter;
break;
default:
annotationNode = param;
break;
}
if (annotationNode !== undefined && !annotationNode.typeAnnotation) {
report(param, getNodeName(param));
}
}
}
return {
ArrayPattern(node) {
if (options[OptionKeys.ArrayDestructuring] && !node.typeAnnotation) {
report(node);
}
},
ArrowFunctionExpression(node) {
if (options[OptionKeys.ArrowParameter]) {
checkParameters(node.params);
}
},
ClassProperty(node) {
if (
options[OptionKeys.MemberVariableDeclaration] &&
!node.typeAnnotation
) {
report(
node,
node.key.type === AST_NODE_TYPES.Identifier
? node.key.name
: undefined,
);
}
},
'FunctionDeclaration, FunctionExpression'(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
) {
if (options[OptionKeys.Parameter]) {
checkParameters(node.params);
}
},
ObjectPattern(node) {
if (options[OptionKeys.ObjectDestructuring] && !node.typeAnnotation) {
report(node);
}
},
'TSIndexSignature, TSPropertySignature'(
node: TSESTree.TSIndexSignature | TSESTree.TSPropertySignature,
) {
if (options[OptionKeys.PropertyDeclaration] && !node.typeAnnotation) {
report(
node,
node.type === AST_NODE_TYPES.TSPropertySignature
? getNodeName(node.key)
: undefined,
);
}
},
VariableDeclarator(node) {
if (
options[OptionKeys.VariableDeclaration] &&
!node.id.typeAnnotation
) {
// Are we inside a context that does not allow type annotations?
let typeAnnotationRequired = true;
let current: TSESTree.Node | undefined = node.parent;
while (current) {
switch (current.type) {
case AST_NODE_TYPES.VariableDeclaration:
// Keep looking upwards
current = current.parent;
break;
case AST_NODE_TYPES.ForOfStatement:
case AST_NODE_TYPES.ForInStatement:
// Stop traversing and don't report an error
typeAnnotationRequired = false;
current = undefined;
break;
default:
// Stop traversing
current = undefined;
break;
}
}
if (typeAnnotationRequired) {
report(node, getNodeName(node.id));
}
}
},
};
},
});