@@ -2,6 +2,10 @@ import { TSESTree } from '@typescript-eslint/experimental-utils';
2
2
import {
3
3
getPropertyIdentifierNode ,
4
4
isExpressionStatement ,
5
+ isVariableDeclaration ,
6
+ isAssignmentExpression ,
7
+ isCallExpression ,
8
+ isSequenceExpression ,
5
9
} from '../node-utils' ;
6
10
import { createTestingLibraryRule } from '../create-testing-library-rule' ;
7
11
@@ -32,7 +36,11 @@ export default createTestingLibraryRule<Options, MessageIds>({
32
36
defaultOptions : [ ] ,
33
37
create : function ( context , _ , helpers ) {
34
38
function isCallerWaitFor (
35
- node : TSESTree . BlockStatement | TSESTree . CallExpression
39
+ node :
40
+ | TSESTree . BlockStatement
41
+ | TSESTree . CallExpression
42
+ | TSESTree . AssignmentExpression
43
+ | TSESTree . SequenceExpression
36
44
) : boolean {
37
45
if ( ! node . parent ) {
38
46
return false ;
@@ -52,18 +60,31 @@ export default createTestingLibraryRule<Options, MessageIds>({
52
60
body : TSESTree . Node [ ]
53
61
) : TSESTree . ExpressionStatement [ ] {
54
62
return body . filter ( ( node ) => {
55
- if ( ! isExpressionStatement ( node ) ) {
63
+ if ( ! isExpressionStatement ( node ) && ! isVariableDeclaration ( node ) ) {
56
64
return false ;
57
65
}
58
66
59
67
const expressionIdentifier = getPropertyIdentifierNode ( node ) ;
60
- if ( ! expressionIdentifier ) {
61
- return false ;
62
- }
68
+
69
+ const isRenderInVariableDeclaration =
70
+ isVariableDeclaration ( node ) &&
71
+ node . declarations . some ( ( declaration ) =>
72
+ helpers . isRenderVariableDeclarator ( declaration )
73
+ ) ;
74
+
75
+ const isRenderInAssignment =
76
+ isExpressionStatement ( node ) &&
77
+ isAssignmentExpression ( node . expression ) &&
78
+ helpers . isRenderUtil (
79
+ getPropertyIdentifierNode ( node . expression . right )
80
+ ) ;
63
81
64
82
return (
65
83
helpers . isFireEventUtil ( expressionIdentifier ) ||
66
- helpers . isUserEventUtil ( expressionIdentifier )
84
+ helpers . isUserEventUtil ( expressionIdentifier ) ||
85
+ helpers . isRenderUtil ( expressionIdentifier ) ||
86
+ isRenderInVariableDeclaration ||
87
+ isRenderInAssignment
67
88
) ;
68
89
} ) as TSESTree . ExpressionStatement [ ] ;
69
90
}
@@ -86,19 +107,35 @@ export default createTestingLibraryRule<Options, MessageIds>({
86
107
}
87
108
}
88
109
89
- function reportImplicitReturnSideEffect ( node : TSESTree . CallExpression ) {
110
+ function reportImplicitReturnSideEffect (
111
+ node :
112
+ | TSESTree . CallExpression
113
+ | TSESTree . AssignmentExpression
114
+ | TSESTree . SequenceExpression
115
+ ) {
90
116
if ( ! isCallerWaitFor ( node ) ) {
91
117
return ;
92
118
}
93
119
94
- const expressionIdentifier = getPropertyIdentifierNode ( node . callee ) ;
95
- if ( ! expressionIdentifier ) {
96
- return ;
97
- }
120
+ const expressionIdentifier =
121
+ isCallExpression ( node ) && getPropertyIdentifierNode ( node . callee ) ;
122
+ const isRenderInAssignment =
123
+ isAssignmentExpression ( node ) &&
124
+ helpers . isRenderUtil ( getPropertyIdentifierNode ( node . right ) ) ;
125
+ const isRenderInSequenceAssignment =
126
+ isSequenceExpression ( node ) &&
127
+ node . expressions . some (
128
+ ( expression ) =>
129
+ isAssignmentExpression ( expression ) &&
130
+ helpers . isRenderUtil ( getPropertyIdentifierNode ( expression . right ) )
131
+ ) ;
98
132
99
133
if (
100
- ! helpers . isFireEventUtil ( expressionIdentifier ) &&
101
- ! helpers . isUserEventUtil ( expressionIdentifier )
134
+ ! helpers . isFireEventUtil ( expressionIdentifier || null ) &&
135
+ ! helpers . isUserEventUtil ( expressionIdentifier || null ) &&
136
+ ! helpers . isRenderUtil ( expressionIdentifier || null ) &&
137
+ ! isRenderInAssignment &&
138
+ ! isRenderInSequenceAssignment
102
139
) {
103
140
return ;
104
141
}
@@ -112,6 +149,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
112
149
return {
113
150
'CallExpression > ArrowFunctionExpression > BlockStatement' : reportSideEffects ,
114
151
'CallExpression > ArrowFunctionExpression > CallExpression' : reportImplicitReturnSideEffect ,
152
+ 'CallExpression > ArrowFunctionExpression > AssignmentExpression' : reportImplicitReturnSideEffect ,
153
+ 'CallExpression > ArrowFunctionExpression > SequenceExpression' : reportImplicitReturnSideEffect ,
115
154
'CallExpression > FunctionExpression > BlockStatement' : reportSideEffects ,
116
155
} ;
117
156
} ,
0 commit comments