explicitReturnTypeUtils.js
8.27 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
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isTypedFunctionExpression = exports.doesImmediatelyReturnFunctionExpression = exports.checkFunctionReturnType = exports.checkFunctionExpressionReturnType = void 0;
const experimental_utils_1 = require("@typescript-eslint/experimental-utils");
const astUtils_1 = require("./astUtils");
const nullThrows_1 = require("./nullThrows");
/**
* Creates a report location for the given function.
* The location only encompasses the "start" of the function, and not the body
*
* eg.
* function foo(args) {}
* ^^^^^^^^^^^^^^^^^^
*
* get y(args) {}
* ^^^^^^^^^^^
*
* const x = (args) => {}
* ^^^^^^^^^
*/
function getReporLoc(node, sourceCode) {
/**
* Returns start column position
* @param node
*/
function getLocStart() {
/* highlight method name */
const parent = node.parent;
if (parent &&
(parent.type === experimental_utils_1.AST_NODE_TYPES.MethodDefinition ||
(parent.type === experimental_utils_1.AST_NODE_TYPES.Property && parent.method))) {
return parent.loc.start;
}
return node.loc.start;
}
/**
* Returns end column position
* @param node
*/
function getLocEnd() {
/* highlight `=>` */
if (node.type === experimental_utils_1.AST_NODE_TYPES.ArrowFunctionExpression) {
return sourceCode.getTokenBefore(node.body, token => token.type === experimental_utils_1.AST_TOKEN_TYPES.Punctuator && token.value === '=>').loc.end;
}
return sourceCode.getTokenBefore(node.body).loc.end;
}
return {
start: getLocStart(),
end: getLocEnd(),
};
}
/**
* Checks if a node is a variable declarator with a type annotation.
* ```
* const x: Foo = ...
* ```
*/
function isVariableDeclaratorWithTypeAnnotation(node) {
return (node.type === experimental_utils_1.AST_NODE_TYPES.VariableDeclarator && !!node.id.typeAnnotation);
}
/**
* Checks if a node is a class property with a type annotation.
* ```
* public x: Foo = ...
* ```
*/
function isClassPropertyWithTypeAnnotation(node) {
return node.type === experimental_utils_1.AST_NODE_TYPES.ClassProperty && !!node.typeAnnotation;
}
/**
* Checks if a node belongs to:
* ```
* new Foo(() => {})
* ^^^^^^^^
* ```
*/
function isConstructorArgument(node) {
return node.type === experimental_utils_1.AST_NODE_TYPES.NewExpression;
}
/**
* Checks if a node belongs to:
* ```
* const x: Foo = { prop: () => {} }
* const x = { prop: () => {} } as Foo
* const x = <Foo>{ prop: () => {} }
* ```
*/
function isPropertyOfObjectWithType(property) {
if (!property || property.type !== experimental_utils_1.AST_NODE_TYPES.Property) {
return false;
}
const objectExpr = property.parent; // this shouldn't happen, checking just in case
/* istanbul ignore if */ if (!objectExpr ||
objectExpr.type !== experimental_utils_1.AST_NODE_TYPES.ObjectExpression) {
return false;
}
const parent = objectExpr.parent; // this shouldn't happen, checking just in case
/* istanbul ignore if */ if (!parent) {
return false;
}
return (astUtils_1.isTypeAssertion(parent) ||
isClassPropertyWithTypeAnnotation(parent) ||
isVariableDeclaratorWithTypeAnnotation(parent) ||
isFunctionArgument(parent));
}
/**
* Checks if a function belongs to:
* ```
* () => () => ...
* () => function () { ... }
* () => { return () => ... }
* () => { return function () { ... } }
* function fn() { return () => ... }
* function fn() { return function() { ... } }
* ```
*/
function doesImmediatelyReturnFunctionExpression({ body, }) {
// Should always have a body; really checking just in case
/* istanbul ignore if */ if (!body) {
return false;
}
// Check if body is a block with a single statement
if (body.type === experimental_utils_1.AST_NODE_TYPES.BlockStatement && body.body.length === 1) {
const [statement] = body.body;
// Check if that statement is a return statement with an argument
if (statement.type === experimental_utils_1.AST_NODE_TYPES.ReturnStatement &&
!!statement.argument) {
// If so, check that returned argument as body
body = statement.argument;
}
}
// Check if the body being returned is a function expression
return (body.type === experimental_utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
body.type === experimental_utils_1.AST_NODE_TYPES.FunctionExpression);
}
exports.doesImmediatelyReturnFunctionExpression = doesImmediatelyReturnFunctionExpression;
/**
* Checks if a node belongs to:
* ```
* foo(() => 1)
* ```
*/
function isFunctionArgument(parent, callee) {
return (parent.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
// make sure this isn't an IIFE
parent.callee !== callee);
}
/**
* Checks if a function belongs to:
* ```
* () => ({ action: 'xxx' } as const)
* ```
*/
function returnsConstAssertionDirectly(node) {
const { body } = node;
if (astUtils_1.isTypeAssertion(body)) {
const { typeAnnotation } = body;
if (typeAnnotation.type === experimental_utils_1.AST_NODE_TYPES.TSTypeReference) {
const { typeName } = typeAnnotation;
if (typeName.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
typeName.name === 'const') {
return true;
}
}
}
return false;
}
/**
* True when the provided function expression is typed.
*/
function isTypedFunctionExpression(node, options) {
const parent = nullThrows_1.nullThrows(node.parent, nullThrows_1.NullThrowsReasons.MissingParent);
if (!options.allowTypedFunctionExpressions) {
return false;
}
return (astUtils_1.isTypeAssertion(parent) ||
isVariableDeclaratorWithTypeAnnotation(parent) ||
isClassPropertyWithTypeAnnotation(parent) ||
isPropertyOfObjectWithType(parent) ||
isFunctionArgument(parent, node) ||
isConstructorArgument(parent));
}
exports.isTypedFunctionExpression = isTypedFunctionExpression;
/**
* Check whether the function expression return type is either typed or valid
* with the provided options.
*/
function isValidFunctionExpressionReturnType(node, options) {
if (isTypedFunctionExpression(node, options)) {
return true;
}
const parent = nullThrows_1.nullThrows(node.parent, nullThrows_1.NullThrowsReasons.MissingParent);
if (options.allowExpressions &&
parent.type !== experimental_utils_1.AST_NODE_TYPES.VariableDeclarator &&
parent.type !== experimental_utils_1.AST_NODE_TYPES.MethodDefinition &&
parent.type !== experimental_utils_1.AST_NODE_TYPES.ExportDefaultDeclaration &&
parent.type !== experimental_utils_1.AST_NODE_TYPES.ClassProperty) {
return true;
}
// https://github.com/typescript-eslint/typescript-eslint/issues/653
if (options.allowDirectConstAssertionInArrowFunctions &&
node.type === experimental_utils_1.AST_NODE_TYPES.ArrowFunctionExpression &&
returnsConstAssertionDirectly(node)) {
return true;
}
return false;
}
/**
* Check that the function expression or declaration is valid.
*/
function isValidFunctionReturnType(node, options) {
if (options.allowHigherOrderFunctions &&
doesImmediatelyReturnFunctionExpression(node)) {
return true;
}
if (node.returnType || astUtils_1.isConstructor(node.parent) || astUtils_1.isSetter(node.parent)) {
return true;
}
return false;
}
/**
* Checks if a function declaration/expression has a return type.
*/
function checkFunctionReturnType(node, options, sourceCode, report) {
if (isValidFunctionReturnType(node, options)) {
return;
}
report(getReporLoc(node, sourceCode));
}
exports.checkFunctionReturnType = checkFunctionReturnType;
/**
* Checks if a function declaration/expression has a return type.
*/
function checkFunctionExpressionReturnType(node, options, sourceCode, report) {
if (isValidFunctionExpressionReturnType(node, options)) {
return;
}
checkFunctionReturnType(node, options, sourceCode, report);
}
exports.checkFunctionExpressionReturnType = checkFunctionExpressionReturnType;
//# sourceMappingURL=explicitReturnTypeUtils.js.map