-
Notifications
You must be signed in to change notification settings - Fork 233
/
valid-expect-in-promise.ts
461 lines (382 loc) · 12 KB
/
valid-expect-in-promise.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
KnownCallExpression,
ModifierName,
createRule,
getAccessorValue,
getNodeName,
isExpectCall,
isFunction,
isIdentifier,
isSupportedAccessor,
isTestCaseCall,
parseExpectCall,
} from './utils';
type PromiseChainCallExpression = KnownCallExpression<
'then' | 'catch' | 'finally'
>;
const isPromiseChainCall = (
node: TSESTree.Node,
): node is PromiseChainCallExpression => {
if (
node.type === AST_NODE_TYPES.CallExpression &&
node.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.callee.property)
) {
// promise methods should have at least 1 argument
if (node.arguments.length === 0) {
return false;
}
switch (getAccessorValue(node.callee.property)) {
case 'then':
return node.arguments.length < 3;
case 'catch':
case 'finally':
return node.arguments.length < 2;
}
}
return false;
};
const findTopMostCallExpression = (
node: TSESTree.CallExpression,
): TSESTree.CallExpression => {
let topMostCallExpression = node;
let { parent } = node;
while (parent) {
if (parent.type === AST_NODE_TYPES.CallExpression) {
topMostCallExpression = parent;
parent = parent.parent;
continue;
}
if (parent.type !== AST_NODE_TYPES.MemberExpression) {
break;
}
parent = parent.parent;
}
return topMostCallExpression;
};
const isTestCaseCallWithCallbackArg = (
node: TSESTree.CallExpression,
): boolean => {
if (!isTestCaseCall(node)) {
return false;
}
const isJestEach = getNodeName(node).endsWith('.each');
if (
isJestEach &&
node.callee.type !== AST_NODE_TYPES.TaggedTemplateExpression
) {
// isJestEach but not a TaggedTemplateExpression, so this must be
// the `jest.each([])()` syntax which this rule doesn't support due
// to its complexity (see jest-community/eslint-plugin-jest#710)
// so we return true to trigger bailout
return true;
}
if (isJestEach || node.arguments.length >= 2) {
const [, callback] = node.arguments;
const callbackArgIndex = Number(isJestEach);
return (
callback &&
isFunction(callback) &&
callback.params.length === 1 + callbackArgIndex
);
}
return false;
};
const isPromiseMethodThatUsesValue = (
node: TSESTree.AwaitExpression | TSESTree.ReturnStatement,
identifier: TSESTree.Identifier,
): boolean => {
const { name } = identifier;
if (node.argument === null) {
return false;
}
if (
node.argument.type === AST_NODE_TYPES.CallExpression &&
node.argument.arguments.length > 0
) {
const nodeName = getNodeName(node.argument);
if (['Promise.all', 'Promise.allSettled'].includes(nodeName as string)) {
const [firstArg] = node.argument.arguments;
if (
firstArg.type === AST_NODE_TYPES.ArrayExpression &&
firstArg.elements.some(nod => isIdentifier(nod, name))
) {
return true;
}
}
if (
['Promise.resolve', 'Promise.reject'].includes(nodeName as string) &&
node.argument.arguments.length === 1
) {
return isIdentifier(node.argument.arguments[0], name);
}
}
return isIdentifier(node.argument, name);
};
/**
* Attempts to determine if the runtime value represented by the given `identifier`
* is `await`ed as an argument along the given call expression
*/
const isValueAwaitedInArguments = (
name: string,
call: TSESTree.CallExpression,
): boolean => {
let node: TSESTree.Node = call;
while (node) {
if (node.type === AST_NODE_TYPES.CallExpression) {
for (const argument of node.arguments) {
if (
argument.type === AST_NODE_TYPES.AwaitExpression &&
isIdentifier(argument.argument, name)
) {
return true;
}
}
node = node.callee;
}
if (node.type !== AST_NODE_TYPES.MemberExpression) {
break;
}
node = node.object;
}
return false;
};
const getLeftMostCallExpression = (
call: TSESTree.CallExpression,
): TSESTree.CallExpression => {
let leftMostCallExpression: TSESTree.CallExpression = call;
let node: TSESTree.Node = call;
while (node) {
if (node.type === AST_NODE_TYPES.CallExpression) {
leftMostCallExpression = node;
node = node.callee;
}
if (node.type !== AST_NODE_TYPES.MemberExpression) {
break;
}
node = node.object;
}
return leftMostCallExpression;
};
/**
* Attempts to determine if the runtime value represented by the given `identifier`
* is `await`ed or `return`ed within the given `body` of statements
*/
const isValueAwaitedOrReturned = (
identifier: TSESTree.Identifier,
body: TSESTree.Statement[],
): boolean => {
const { name } = identifier;
for (const node of body) {
// skip all nodes that are before this identifier, because they'd probably
// be affecting a different runtime value (e.g. due to reassignment)
if (node.range[0] <= identifier.range[0]) {
continue;
}
if (node.type === AST_NODE_TYPES.ReturnStatement) {
return isPromiseMethodThatUsesValue(node, identifier);
}
if (node.type === AST_NODE_TYPES.ExpressionStatement) {
// it's possible that we're awaiting the value as an argument
if (node.expression.type === AST_NODE_TYPES.CallExpression) {
if (isValueAwaitedInArguments(name, node.expression)) {
return true;
}
const leftMostCall = getLeftMostCallExpression(node.expression);
if (
isExpectCall(leftMostCall) &&
leftMostCall.arguments.length > 0 &&
isIdentifier(leftMostCall.arguments[0], name)
) {
const { modifier } = parseExpectCall(leftMostCall);
if (
modifier?.name === ModifierName.resolves ||
modifier?.name === ModifierName.rejects
) {
return true;
}
}
}
if (
node.expression.type === AST_NODE_TYPES.AwaitExpression &&
isPromiseMethodThatUsesValue(node.expression, identifier)
) {
return true;
}
// (re)assignment changes the runtime value, so if we've not found an
// await or return already we act as if we've reached the end of the body
if (node.expression.type === AST_NODE_TYPES.AssignmentExpression) {
// unless we're assigning to the same identifier, in which case
// we might be chaining off the existing promise value
if (
isIdentifier(node.expression.left, name) &&
getNodeName(node.expression.right)?.startsWith(`${name}.`) &&
isPromiseChainCall(node.expression.right)
) {
continue;
}
break;
}
}
if (
node.type === AST_NODE_TYPES.BlockStatement &&
isValueAwaitedOrReturned(identifier, node.body)
) {
return true;
}
}
return false;
};
const findFirstBlockBodyUp = (
node: TSESTree.Node,
): TSESTree.BlockStatement['body'] => {
let parent: TSESTree.Node['parent'] = node;
while (parent) {
if (parent.type === AST_NODE_TYPES.BlockStatement) {
return parent.body;
}
parent = parent.parent;
}
/* istanbul ignore next */
throw new Error(
`Could not find BlockStatement - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
);
};
const isDirectlyWithinTestCaseCall = (node: TSESTree.Node): boolean => {
let parent: TSESTree.Node['parent'] = node;
while (parent) {
if (isFunction(parent)) {
parent = parent.parent;
return !!(
parent?.type === AST_NODE_TYPES.CallExpression && isTestCaseCall(parent)
);
}
parent = parent.parent;
}
return false;
};
const isVariableAwaitedOrReturned = (
variable: TSESTree.VariableDeclarator,
): boolean => {
const body = findFirstBlockBodyUp(variable);
// it's pretty much impossible for us to track destructuring assignments,
// so we return true to bailout gracefully
if (!isIdentifier(variable.id)) {
return true;
}
return isValueAwaitedOrReturned(variable.id, body);
};
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description:
'Ensure promises that have expectations in their chain are valid',
recommended: 'error',
},
messages: {
expectInFloatingPromise:
"This promise should either be returned or awaited to ensure the expects in it's chain are called",
},
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
let inTestCaseWithDoneCallback = false;
// an array of booleans representing each promise chain we enter, with the
// boolean value representing if we think a given chain contains an expect
// in it's body.
//
// since we only care about the inner-most chain, we represent the state in
// reverse with the inner-most being the first item, as that makes it
// slightly less code to assign to by not needing to know the length
const chains: boolean[] = [];
return {
CallExpression(node: TSESTree.CallExpression) {
// there are too many ways that the done argument could be used with
// promises that contain expect that would make the promise safe for us
if (isTestCaseCallWithCallbackArg(node)) {
inTestCaseWithDoneCallback = true;
return;
}
// if this call expression is a promise chain, add it to the stack with
// value of "false", as we assume there are no expect calls initially
if (isPromiseChainCall(node)) {
chains.unshift(false);
return;
}
// if we're within a promise chain, and this call expression looks like
// an expect call, mark the deepest chain as having an expect call
if (chains.length > 0 && isExpectCall(node)) {
chains[0] = true;
}
},
'CallExpression:exit'(node: TSESTree.CallExpression) {
// there are too many ways that the "done" argument could be used to
// make promises containing expects safe in a test for us to be able to
// accurately check, so we just bail out completely if it's present
if (inTestCaseWithDoneCallback) {
if (isTestCaseCall(node)) {
inTestCaseWithDoneCallback = false;
}
return;
}
if (!isPromiseChainCall(node)) {
return;
}
// since we're exiting this call expression (which is a promise chain)
// we remove it from the stack of chains, since we're unwinding
const hasExpectCall = chains.shift();
// if the promise chain we're exiting doesn't contain an expect,
// then we don't need to check it for anything
if (!hasExpectCall) {
return;
}
const { parent } = findTopMostCallExpression(node);
// if we don't have a parent (which is technically impossible at runtime)
// or our parent is not directly within the test case, we stop checking
// because we're most likely in the body of a function being defined
// within the test, which we can't track
if (!parent || !isDirectlyWithinTestCaseCall(parent)) {
return;
}
switch (parent.type) {
case AST_NODE_TYPES.VariableDeclarator: {
if (isVariableAwaitedOrReturned(parent)) {
return;
}
break;
}
case AST_NODE_TYPES.AssignmentExpression: {
if (
parent.left.type === AST_NODE_TYPES.Identifier &&
isValueAwaitedOrReturned(
parent.left,
findFirstBlockBodyUp(parent),
)
) {
return;
}
break;
}
case AST_NODE_TYPES.ExpressionStatement:
break;
case AST_NODE_TYPES.ReturnStatement:
case AST_NODE_TYPES.AwaitExpression:
default:
return;
}
context.report({
messageId: 'expectInFloatingPromise',
node: parent,
});
},
};
},
});