typedef.js
9.36 KB
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
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const experimental_utils_1 = require("@typescript-eslint/experimental-utils");
const util = __importStar(require("../util"));
exports.default = util.createRule({
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: {
["arrayDestructuring" /* ArrayDestructuring */]: { type: 'boolean' },
["arrowParameter" /* ArrowParameter */]: { type: 'boolean' },
["memberVariableDeclaration" /* MemberVariableDeclaration */]: { type: 'boolean' },
["objectDestructuring" /* ObjectDestructuring */]: { type: 'boolean' },
["parameter" /* Parameter */]: { type: 'boolean' },
["propertyDeclaration" /* PropertyDeclaration */]: { type: 'boolean' },
["variableDeclaration" /* VariableDeclaration */]: { type: 'boolean' },
["variableDeclarationIgnoreFunction" /* VariableDeclarationIgnoreFunction */]: { type: 'boolean' },
},
},
],
type: 'suggestion',
},
defaultOptions: [
{
["arrayDestructuring" /* ArrayDestructuring */]: false,
["arrowParameter" /* ArrowParameter */]: false,
["memberVariableDeclaration" /* MemberVariableDeclaration */]: false,
["objectDestructuring" /* ObjectDestructuring */]: false,
["parameter" /* Parameter */]: false,
["propertyDeclaration" /* PropertyDeclaration */]: false,
["variableDeclaration" /* VariableDeclaration */]: false,
["variableDeclarationIgnoreFunction" /* VariableDeclarationIgnoreFunction */]: false,
},
],
create(context, [options]) {
function report(location, name) {
context.report({
node: location,
messageId: name ? 'expectedTypedefNamed' : 'expectedTypedef',
data: { name },
});
}
function getNodeName(node) {
return node.type === experimental_utils_1.AST_NODE_TYPES.Identifier ? node.name : undefined;
}
function isForOfStatementContext(node) {
let current = node.parent;
while (current) {
switch (current.type) {
case experimental_utils_1.AST_NODE_TYPES.VariableDeclarator:
case experimental_utils_1.AST_NODE_TYPES.VariableDeclaration:
case experimental_utils_1.AST_NODE_TYPES.ObjectPattern:
case experimental_utils_1.AST_NODE_TYPES.ArrayPattern:
case experimental_utils_1.AST_NODE_TYPES.Property:
current = current.parent;
break;
case experimental_utils_1.AST_NODE_TYPES.ForOfStatement:
return true;
default:
current = undefined;
}
}
return false;
}
function checkParameters(params) {
for (const param of params) {
let annotationNode;
switch (param.type) {
case experimental_utils_1.AST_NODE_TYPES.AssignmentPattern:
annotationNode = param.left;
break;
case experimental_utils_1.AST_NODE_TYPES.TSParameterProperty:
annotationNode = param.parameter;
// Check TS parameter property with default value like `constructor(private param: string = 'something') {}`
if (annotationNode &&
annotationNode.type === experimental_utils_1.AST_NODE_TYPES.AssignmentPattern) {
annotationNode = annotationNode.left;
}
break;
default:
annotationNode = param;
break;
}
if (annotationNode !== undefined && !annotationNode.typeAnnotation) {
report(param, getNodeName(param));
}
}
}
function isVariableDeclarationIgnoreFunction(node) {
return (!!options["variableDeclarationIgnoreFunction" /* VariableDeclarationIgnoreFunction */] &&
(node.type === experimental_utils_1.AST_NODE_TYPES.FunctionExpression ||
node.type === experimental_utils_1.AST_NODE_TYPES.ArrowFunctionExpression));
}
return {
ArrayPattern(node) {
var _a;
if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === experimental_utils_1.AST_NODE_TYPES.RestElement &&
node.parent.typeAnnotation) {
return;
}
if (options["arrayDestructuring" /* ArrayDestructuring */] &&
!node.typeAnnotation &&
!isForOfStatementContext(node)) {
report(node);
}
},
ArrowFunctionExpression(node) {
if (options["arrowParameter" /* ArrowParameter */]) {
checkParameters(node.params);
}
},
ClassProperty(node) {
if (node.value && isVariableDeclarationIgnoreFunction(node.value)) {
return;
}
if (options["memberVariableDeclaration" /* MemberVariableDeclaration */] &&
!node.typeAnnotation) {
report(node, node.key.type === experimental_utils_1.AST_NODE_TYPES.Identifier
? node.key.name
: undefined);
}
},
'FunctionDeclaration, FunctionExpression'(node) {
if (options["parameter" /* Parameter */]) {
checkParameters(node.params);
}
},
ObjectPattern(node) {
if (options["objectDestructuring" /* ObjectDestructuring */] &&
!node.typeAnnotation &&
!isForOfStatementContext(node)) {
report(node);
}
},
'TSIndexSignature, TSPropertySignature'(node) {
if (options["propertyDeclaration" /* PropertyDeclaration */] && !node.typeAnnotation) {
report(node, node.type === experimental_utils_1.AST_NODE_TYPES.TSPropertySignature
? getNodeName(node.key)
: undefined);
}
},
VariableDeclarator(node) {
if (!options["variableDeclaration" /* VariableDeclaration */] ||
node.id.typeAnnotation ||
(node.id.type === experimental_utils_1.AST_NODE_TYPES.ArrayPattern &&
!options["arrayDestructuring" /* ArrayDestructuring */]) ||
(node.id.type === experimental_utils_1.AST_NODE_TYPES.ObjectPattern &&
!options["objectDestructuring" /* ObjectDestructuring */]) ||
(node.init && isVariableDeclarationIgnoreFunction(node.init))) {
return;
}
let current = node.parent;
while (current) {
switch (current.type) {
case experimental_utils_1.AST_NODE_TYPES.VariableDeclaration:
// Keep looking upwards
current = current.parent;
break;
case experimental_utils_1.AST_NODE_TYPES.ForOfStatement:
case experimental_utils_1.AST_NODE_TYPES.ForInStatement:
// Stop traversing and don't report an error
return;
default:
// Stop traversing
current = undefined;
break;
}
}
report(node, getNodeName(node.id));
},
};
},
});
//# sourceMappingURL=typedef.js.map