1+ import { ASTUtils , TSESTree } from '@typescript-eslint/experimental-utils' ;
12import {
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' ;
88
99export const RULE_NAME = 'no-wait-for-empty-callback' ;
1010export type MessageIds = 'noWaitForEmptyCallback' ;
1111type Options = [ ] ;
1212
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 > ( {
1714 name : RULE_NAME ,
1815 meta : {
1916 type : 'suggestion' ,
@@ -33,11 +30,24 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
3330 defaultOptions : [ ] ,
3431
3532 // 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+
3844 function reportIfEmpty (
3945 node : TSESTree . ArrowFunctionExpression | TSESTree . FunctionExpression
4046 ) {
47+ if ( ! isValidWaitFor ( node ) ) {
48+ return ;
49+ }
50+
4151 if (
4252 isBlockStatement ( node . body ) &&
4353 node . body . body . length === 0 &&
@@ -56,17 +66,27 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5666 }
5767
5868 function reportNoop ( node : TSESTree . Identifier ) {
69+ if ( ! isValidWaitFor ( node ) ) {
70+ return ;
71+ }
72+
5973 context . report ( {
6074 node,
6175 loc : node . loc . start ,
6276 messageId : 'noWaitForEmptyCallback' ,
77+ data : {
78+ methodName :
79+ isCallExpression ( node . parent ) &&
80+ ASTUtils . isIdentifier ( node . parent . callee ) &&
81+ node . parent . callee . name ,
82+ } ,
6383 } ) ;
6484 }
6585
6686 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 ,
7090 } ;
7191 } ,
7292} ) ;
0 commit comments