1
- import { ESLintUtils , TSESTree } from '@typescript-eslint/experimental-utils'
2
- import { getDocsUrl , hasTestingLibraryImportModule } from '../utils'
3
- import { isBlockStatement , findClosestCallNode , isMemberExpression , isCallExpression , isIdentifier } from '../node-utils'
1
+ import { ESLintUtils , TSESTree } from '@typescript-eslint/experimental-utils' ;
2
+ import { getDocsUrl , hasTestingLibraryImportModule } from '../utils' ;
3
+ import {
4
+ isBlockStatement ,
5
+ isMemberExpression ,
6
+ isCallExpression ,
7
+ isIdentifier ,
8
+ } from '../node-utils' ;
4
9
5
10
export const RULE_NAME = 'no-side-effects-wait-for' ;
6
11
7
- const WAIT_EXPRESSION_QUERY =
8
- 'CallExpression[callee.name=/^(waitFor)$/]' ;
12
+ const WAIT_EXPRESSION_QUERY = 'CallExpression[callee.name=/^(waitFor)$/]' ;
9
13
10
- const SIDE_EFFECTS : Array < string > = [ 'fireEvent' , 'userEvent' ]
14
+ const SIDE_EFFECTS : Array < string > = [ 'fireEvent' , 'userEvent' ] ;
11
15
12
16
export type MessageIds = 'noSideEffectsWaitFor' ;
13
17
type Options = [ ] ;
@@ -17,13 +21,13 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
17
21
meta : {
18
22
type : 'suggestion' ,
19
23
docs : {
20
- description :
21
- "It's preferred to avoid side effects in `waitFor`" ,
24
+ description : "It's preferred to avoid side effects in `waitFor`" ,
22
25
category : 'Best Practices' ,
23
26
recommended : false ,
24
27
} ,
25
28
messages : {
26
- noSideEffectsWaitFor : 'Avoid using side effects within `waitFor` callback' ,
29
+ noSideEffectsWaitFor :
30
+ 'Avoid using side effects within `waitFor` callback' ,
27
31
} ,
28
32
fixable : null ,
29
33
schema : [ ] ,
@@ -32,25 +36,27 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
32
36
create : function ( context ) {
33
37
let isImportingTestingLibrary = false ;
34
38
35
- function reportSideEffects (
36
- node : TSESTree . BlockStatement
37
- ) {
39
+ function reportSideEffects ( node : TSESTree . BlockStatement ) {
38
40
const hasSideEffects = ( body : Array < TSESTree . Node > ) : boolean =>
39
41
body . some ( ( node : TSESTree . ExpressionStatement ) => {
40
42
if (
41
43
isCallExpression ( node . expression ) &&
42
44
isMemberExpression ( node . expression . callee ) &&
43
45
isIdentifier ( node . expression . callee . object )
44
46
) {
45
- const object : TSESTree . Identifier = node . expression . callee . object
46
- const identifierName : string = object . name
47
- return SIDE_EFFECTS . includes ( identifierName )
47
+ const object : TSESTree . Identifier = node . expression . callee . object ;
48
+ const identifierName : string = object . name ;
49
+ return SIDE_EFFECTS . includes ( identifierName ) ;
48
50
} else {
49
- return false
51
+ return false ;
50
52
}
51
- } )
53
+ } ) ;
52
54
53
- if ( isImportingTestingLibrary && isBlockStatement ( node ) && hasSideEffects ( node . body ) ) {
55
+ if (
56
+ isImportingTestingLibrary &&
57
+ isBlockStatement ( node ) &&
58
+ hasSideEffects ( node . body )
59
+ ) {
54
60
context . report ( {
55
61
node,
56
62
loc : node . loc . start ,
@@ -64,7 +70,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
64
70
[ `${ WAIT_EXPRESSION_QUERY } > FunctionExpression > BlockStatement` ] : reportSideEffects ,
65
71
ImportDeclaration ( node : TSESTree . ImportDeclaration ) {
66
72
isImportingTestingLibrary = hasTestingLibraryImportModule ( node ) ;
67
- }
73
+ } ,
68
74
} ;
69
- }
70
- } )
75
+ } ,
76
+ } ) ;
0 commit comments