@@ -11,8 +11,9 @@ export const RULE_NAME = 'no-unnecessary-act';
1111export type MessageIds =
1212 | 'noUnnecessaryActEmptyFunction'
1313 | 'noUnnecessaryActTestingLibraryUtil' ;
14+ type Options = [ { isStrict : boolean } ] ;
1415
15- export default createTestingLibraryRule < [ ] , MessageIds > ( {
16+ export default createTestingLibraryRule < Options , MessageIds > ( {
1617 name : RULE_NAME ,
1718 meta : {
1819 type : 'problem' ,
@@ -32,37 +33,71 @@ export default createTestingLibraryRule<[], MessageIds>({
3233 'Avoid wrapping Testing Library util calls in `act`' ,
3334 noUnnecessaryActEmptyFunction : 'Avoid wrapping empty function in `act`' ,
3435 } ,
35- schema : [ ] ,
36+ schema : [
37+ {
38+ type : 'object' ,
39+ properties : {
40+ isStrict : { type : 'boolean' } ,
41+ } ,
42+ } ,
43+ ] ,
3644 } ,
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+ }
3867
39- create ( context , _ , helpers ) {
4068 /**
4169 * Determines whether some call is non Testing Library related for a given list of statements.
4270 */
4371 function hasSomeNonTestingLibraryCall (
4472 statements : TSESTree . Statement [ ]
4573 ) : boolean {
4674 return statements . some ( ( statement ) => {
47- const callExpression = getStatementCallExpression ( statement ) ;
75+ const identifier = getStatementIdentifier ( statement ) ;
4876
49- if ( ! callExpression ) {
77+ if ( ! identifier ) {
5078 return false ;
5179 }
5280
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 ) ;
5488
5589 if ( ! identifier ) {
5690 return false ;
5791 }
5892
59- return ! helpers . isTestingLibraryUtil ( identifier ) ;
93+ return helpers . isTestingLibraryUtil ( identifier ) ;
6094 } ) ;
6195 }
6296
6397 function checkNoUnnecessaryActFromBlockStatement (
6498 blockStatementNode : TSESTree . BlockStatement
6599 ) {
100+ const { isStrict } = options ;
66101 const functionNode = blockStatementNode . parent as
67102 | TSESTree . ArrowFunctionExpression
68103 | TSESTree . FunctionExpression
@@ -89,7 +124,15 @@ export default createTestingLibraryRule<[], MessageIds>({
89124 node : identifierNode ,
90125 messageId : 'noUnnecessaryActEmptyFunction' ,
91126 } ) ;
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 ) {
93136 context . report ( {
94137 node : identifierNode ,
95138 messageId : 'noUnnecessaryActTestingLibraryUtil' ,
0 commit comments