@@ -11,8 +11,9 @@ export const RULE_NAME = 'no-unnecessary-act';
11
11
export type MessageIds =
12
12
| 'noUnnecessaryActEmptyFunction'
13
13
| 'noUnnecessaryActTestingLibraryUtil' ;
14
+ type Options = [ { isStrict : boolean } ] ;
14
15
15
- export default createTestingLibraryRule < [ ] , MessageIds > ( {
16
+ export default createTestingLibraryRule < Options , MessageIds > ( {
16
17
name : RULE_NAME ,
17
18
meta : {
18
19
type : 'problem' ,
@@ -32,37 +33,71 @@ export default createTestingLibraryRule<[], MessageIds>({
32
33
'Avoid wrapping Testing Library util calls in `act`' ,
33
34
noUnnecessaryActEmptyFunction : 'Avoid wrapping empty function in `act`' ,
34
35
} ,
35
- schema : [ ] ,
36
+ schema : [
37
+ {
38
+ type : 'object' ,
39
+ properties : {
40
+ isStrict : { type : 'boolean' } ,
41
+ } ,
42
+ } ,
43
+ ] ,
36
44
} ,
37
- defaultOptions : [ ] ,
45
+ defaultOptions : [
46
+ {
47
+ isStrict : false ,
48
+ } ,
49
+ ] ,
50
+
51
+ create ( context , [ options ] , helpers ) {
52
+ function getStatementIdentifier ( statement : TSESTree . Statement ) {
53
+ const callExpression = getStatementCallExpression ( statement ) ;
54
+
55
+ if ( ! callExpression ) {
56
+ return null ;
57
+ }
58
+
59
+ const identifier = getDeepestIdentifierNode ( callExpression ) ;
60
+
61
+ if ( ! identifier ) {
62
+ return null ;
63
+ }
64
+
65
+ return identifier ;
66
+ }
38
67
39
- create ( context , _ , helpers ) {
40
68
/**
41
69
* Determines whether some call is non Testing Library related for a given list of statements.
42
70
*/
43
71
function hasSomeNonTestingLibraryCall (
44
72
statements : TSESTree . Statement [ ]
45
73
) : boolean {
46
74
return statements . some ( ( statement ) => {
47
- const callExpression = getStatementCallExpression ( statement ) ;
75
+ const identifier = getStatementIdentifier ( statement ) ;
48
76
49
- if ( ! callExpression ) {
77
+ if ( ! identifier ) {
50
78
return false ;
51
79
}
52
80
53
- const identifier = getDeepestIdentifierNode ( callExpression ) ;
81
+ return ! helpers . isTestingLibraryUtil ( identifier ) ;
82
+ } ) ;
83
+ }
84
+
85
+ function hasTestingLibraryCall ( statements : TSESTree . Statement [ ] ) {
86
+ return statements . some ( ( statement ) => {
87
+ const identifier = getStatementIdentifier ( statement ) ;
54
88
55
89
if ( ! identifier ) {
56
90
return false ;
57
91
}
58
92
59
- return ! helpers . isTestingLibraryUtil ( identifier ) ;
93
+ return helpers . isTestingLibraryUtil ( identifier ) ;
60
94
} ) ;
61
95
}
62
96
63
97
function checkNoUnnecessaryActFromBlockStatement (
64
98
blockStatementNode : TSESTree . BlockStatement
65
99
) {
100
+ const { isStrict } = options ;
66
101
const functionNode = blockStatementNode . parent as
67
102
| TSESTree . ArrowFunctionExpression
68
103
| TSESTree . FunctionExpression
@@ -89,7 +124,15 @@ export default createTestingLibraryRule<[], MessageIds>({
89
124
node : identifierNode ,
90
125
messageId : 'noUnnecessaryActEmptyFunction' ,
91
126
} ) ;
92
- } else if ( ! hasSomeNonTestingLibraryCall ( blockStatementNode . body ) ) {
127
+ return ;
128
+ }
129
+
130
+ const shouldBeReported = isStrict
131
+ ? hasSomeNonTestingLibraryCall ( blockStatementNode . body ) &&
132
+ hasTestingLibraryCall ( blockStatementNode . body )
133
+ : ! hasSomeNonTestingLibraryCall ( blockStatementNode . body ) ;
134
+
135
+ if ( shouldBeReported ) {
93
136
context . report ( {
94
137
node : identifierNode ,
95
138
messageId : 'noUnnecessaryActTestingLibraryUtil' ,
0 commit comments