1
+ import { ASTUtils , TSESTree } from '@typescript-eslint/experimental-utils' ;
1
2
import {
2
- ESLintUtils ,
3
- TSESTree ,
4
- ASTUtils ,
5
- } from '@typescript-eslint/experimental-utils' ;
6
- import { getDocsUrl } from '../utils' ;
7
- import { isBlockStatement , isCallExpression } from '../node-utils' ;
3
+ getPropertyIdentifierNode ,
4
+ isBlockStatement ,
5
+ isCallExpression ,
6
+ } from '../node-utils' ;
7
+ import { createTestingLibraryRule } from '../create-testing-library-rule' ;
8
8
9
9
export const RULE_NAME = 'no-wait-for-empty-callback' ;
10
10
export type MessageIds = 'noWaitForEmptyCallback' ;
11
11
type Options = [ ] ;
12
12
13
- const WAIT_EXPRESSION_QUERY =
14
- 'CallExpression[callee.name=/^(waitFor|waitForElementToBeRemoved)$/]' ;
15
-
16
- export default ESLintUtils . RuleCreator ( getDocsUrl ) < Options , MessageIds > ( {
13
+ export default createTestingLibraryRule < Options , MessageIds > ( {
17
14
name : RULE_NAME ,
18
15
meta : {
19
16
type : 'suggestion' ,
@@ -33,11 +30,24 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
33
30
defaultOptions : [ ] ,
34
31
35
32
// trimmed down implementation of https://github.com/eslint/eslint/blob/master/lib/rules/no-empty-function.js
36
- // TODO: var referencing any of previously mentioned?
37
- create : function ( context ) {
33
+ create ( context , _ , helpers ) {
34
+ function isValidWaitFor ( node : TSESTree . Node ) : boolean {
35
+ const parentCallExpression = node . parent as TSESTree . CallExpression ;
36
+ const parentIdentifier = getPropertyIdentifierNode ( parentCallExpression ) ;
37
+
38
+ return helpers . isAsyncUtil ( parentIdentifier , [
39
+ 'waitFor' ,
40
+ 'waitForElementToBeRemoved' ,
41
+ ] ) ;
42
+ }
43
+
38
44
function reportIfEmpty (
39
45
node : TSESTree . ArrowFunctionExpression | TSESTree . FunctionExpression
40
46
) {
47
+ if ( ! isValidWaitFor ( node ) ) {
48
+ return ;
49
+ }
50
+
41
51
if (
42
52
isBlockStatement ( node . body ) &&
43
53
node . body . body . length === 0 &&
@@ -56,17 +66,27 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
56
66
}
57
67
58
68
function reportNoop ( node : TSESTree . Identifier ) {
69
+ if ( ! isValidWaitFor ( node ) ) {
70
+ return ;
71
+ }
72
+
59
73
context . report ( {
60
74
node,
61
75
loc : node . loc . start ,
62
76
messageId : 'noWaitForEmptyCallback' ,
77
+ data : {
78
+ methodName :
79
+ isCallExpression ( node . parent ) &&
80
+ ASTUtils . isIdentifier ( node . parent . callee ) &&
81
+ node . parent . callee . name ,
82
+ } ,
63
83
} ) ;
64
84
}
65
85
66
86
return {
67
- [ ` ${ WAIT_EXPRESSION_QUERY } > ArrowFunctionExpression` ] : reportIfEmpty ,
68
- [ ` ${ WAIT_EXPRESSION_QUERY } > FunctionExpression` ] : reportIfEmpty ,
69
- [ ` ${ WAIT_EXPRESSION_QUERY } > Identifier[name="noop"]` ] : reportNoop ,
87
+ 'CallExpression > ArrowFunctionExpression' : reportIfEmpty ,
88
+ 'CallExpression > FunctionExpression' : reportIfEmpty ,
89
+ 'CallExpression > Identifier[name="noop"]' : reportNoop ,
70
90
} ;
71
91
} ,
72
92
} ) ;
0 commit comments