-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathno-wait-for-empty-callback.ts
99 lines (88 loc) · 2.61 KB
/
no-wait-for-empty-callback.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
import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
getPropertyIdentifierNode,
isCallExpression,
isEmptyFunction,
} from '../node-utils';
export const RULE_NAME = 'no-wait-for-empty-callback';
export type MessageIds = 'noWaitForEmptyCallback';
type Options = [];
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'Disallow empty callbacks for `waitFor` and `waitForElementToBeRemoved`',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
},
},
messages: {
noWaitForEmptyCallback:
'Avoid passing empty callback to `{{ methodName }}`. Insert an assertion instead.',
},
schema: [],
},
defaultOptions: [],
// trimmed down implementation of https://github.com/eslint/eslint/blob/master/lib/rules/no-empty-function.js
create(context, _, helpers) {
function isValidWaitFor(node: TSESTree.Node): boolean {
const parentCallExpression = node.parent as TSESTree.CallExpression;
const parentIdentifier = getPropertyIdentifierNode(parentCallExpression);
if (!parentIdentifier) {
return false;
}
return helpers.isAsyncUtil(parentIdentifier, [
'waitFor',
'waitForElementToBeRemoved',
]);
}
function reportIfEmpty(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression
) {
if (!isValidWaitFor(node)) {
return;
}
if (
isEmptyFunction(node) &&
isCallExpression(node.parent) &&
ASTUtils.isIdentifier(node.parent.callee)
) {
context.report({
node,
loc: node.body.loc.start,
messageId: 'noWaitForEmptyCallback',
data: {
methodName: node.parent.callee.name,
},
});
}
}
function reportNoop(node: TSESTree.Identifier) {
if (!isValidWaitFor(node)) {
return;
}
context.report({
node,
loc: node.loc.start,
messageId: 'noWaitForEmptyCallback',
data: {
methodName:
isCallExpression(node.parent) &&
ASTUtils.isIdentifier(node.parent.callee) &&
node.parent.callee.name,
},
});
}
return {
'CallExpression > ArrowFunctionExpression': reportIfEmpty,
'CallExpression > FunctionExpression': reportIfEmpty,
'CallExpression > Identifier[name="noop"]': reportNoop,
};
},
});