1
+ import { isAwaitExpression } from '@typescript-eslint/utils/ast-utils' ;
2
+
1
3
import { createTestingLibraryRule } from '../create-testing-library-rule' ;
2
4
import {
3
5
getPropertyIdentifierNode ,
@@ -8,6 +10,7 @@ import {
8
10
isSequenceExpression ,
9
11
hasThenProperty ,
10
12
} from '../node-utils' ;
13
+ import { getSourceCode } from '../utils' ;
11
14
12
15
import type { TSESTree } from '@typescript-eslint/utils' ;
13
16
@@ -35,6 +38,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
35
38
'Avoid using side effects within `waitFor` callback' ,
36
39
} ,
37
40
schema : [ ] ,
41
+ fixable : 'code' ,
38
42
} ,
39
43
defaultOptions : [ ] ,
40
44
create ( context , _ , helpers ) {
@@ -209,10 +213,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
209
213
}
210
214
211
215
function reportImplicitReturnSideEffect (
212
- node :
216
+ node : (
213
217
| TSESTree . AssignmentExpression
214
218
| TSESTree . CallExpression
215
219
| TSESTree . SequenceExpression
220
+ ) & {
221
+ parent : TSESTree . ArrowFunctionExpression & {
222
+ parent : TSESTree . CallExpression ;
223
+ } ;
224
+ }
216
225
) {
217
226
if ( ! isCallerWaitFor ( node ) ) {
218
227
return ;
@@ -242,6 +251,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
242
251
context . report ( {
243
252
node,
244
253
messageId : 'noSideEffectsWaitFor' ,
254
+ fix : ( fixer ) => {
255
+ const { parent : callExpressionNode } = node . parent ;
256
+ const targetNode = isAwaitExpression ( callExpressionNode . parent )
257
+ ? callExpressionNode . parent
258
+ : callExpressionNode ;
259
+ const sourceCode = getSourceCode ( context ) ;
260
+ return fixer . replaceText ( targetNode , sourceCode . getText ( node ) ) ;
261
+ } ,
245
262
} ) ;
246
263
}
247
264
0 commit comments