-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathno-wait-for-empty-callback.ts
72 lines (66 loc) · 2.07 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
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl } from '../utils';
import {
isBlockStatement,
isCallExpression,
isIdentifier,
} from '../node-utils';
export const RULE_NAME = 'no-wait-for-empty-callback';
export type MessageIds = 'noWaitForEmptyCallback';
type Options = [];
const WAIT_EXPRESSION_QUERY =
'CallExpression[callee.name=/^(waitFor|waitForElementToBeRemoved)$/]';
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
"It's preferred to avoid empty callbacks in `waitFor` and `waitForElementToBeRemoved`",
category: 'Best Practices',
recommended: 'error',
},
messages: {
noWaitForEmptyCallback:
'Avoid passing empty callback to `{{ methodName }}`. Insert an assertion instead.',
},
fixable: null,
schema: [],
},
defaultOptions: [],
// trimmed down implementation of https://github.com/eslint/eslint/blob/master/lib/rules/no-empty-function.js
// TODO: var referencing any of previously mentioned?
create: function(context) {
function reportIfEmpty(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression
) {
if (
isBlockStatement(node.body) &&
node.body.body.length === 0 &&
isCallExpression(node.parent) &&
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) {
context.report({
node,
loc: node.loc.start,
messageId: 'noWaitForEmptyCallback',
});
}
return {
[`${WAIT_EXPRESSION_QUERY} > ArrowFunctionExpression`]: reportIfEmpty,
[`${WAIT_EXPRESSION_QUERY} > FunctionExpression`]: reportIfEmpty,
[`${WAIT_EXPRESSION_QUERY} > Identifier[name="noop"]`]: reportNoop,
};
},
});