@@ -3,6 +3,7 @@ import { ASTUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
3
3
import { createTestingLibraryRule } from '../create-testing-library-rule' ;
4
4
import {
5
5
findClosestCallExpressionNode ,
6
+ findClosestFunctionExpressionNode ,
6
7
getFunctionName ,
7
8
getInnermostReturningFunction ,
8
9
getVariableReferences ,
@@ -142,7 +143,28 @@ export default createTestingLibraryRule<Options, MessageIds>({
142
143
closestCallExpression,
143
144
fix : ( fixer ) => {
144
145
if ( isMemberExpression ( node . parent ) ) {
145
- return fixer . insertTextBefore ( node . parent , 'await ' ) ;
146
+ const functionExpression =
147
+ findClosestFunctionExpressionNode ( node ) ;
148
+
149
+ if ( functionExpression ) {
150
+ const memberExpressionFixer = fixer . insertTextBefore (
151
+ node . parent ,
152
+ 'await '
153
+ ) ;
154
+
155
+ if ( functionExpression . async ) {
156
+ return memberExpressionFixer ;
157
+ } else {
158
+ // Mutate the actual node so if other nodes exist in this
159
+ // function expression body they don't also try to fix it.
160
+ functionExpression . async = true ;
161
+
162
+ return [
163
+ memberExpressionFixer ,
164
+ fixer . insertTextBefore ( functionExpression , 'async ' ) ,
165
+ ] ;
166
+ }
167
+ }
146
168
}
147
169
148
170
return null ;
@@ -175,7 +197,27 @@ export default createTestingLibraryRule<Options, MessageIds>({
175
197
closestCallExpression,
176
198
messageId : 'awaitAsyncEventWrapper' ,
177
199
fix : ( fixer ) => {
178
- return fixer . insertTextBefore ( node , 'await ' ) ;
200
+ const functionExpression =
201
+ findClosestFunctionExpressionNode ( node ) ;
202
+
203
+ if ( functionExpression ) {
204
+ const nodeFixer = fixer . insertTextBefore ( node , 'await ' ) ;
205
+
206
+ if ( functionExpression . async ) {
207
+ return nodeFixer ;
208
+ } else {
209
+ // Mutate the actual node so if other nodes exist in this
210
+ // function expression body they don't also try to fix it.
211
+ functionExpression . async = true ;
212
+
213
+ return [
214
+ nodeFixer ,
215
+ fixer . insertTextBefore ( functionExpression , 'async ' ) ,
216
+ ] ;
217
+ }
218
+ }
219
+
220
+ return null ;
179
221
} ,
180
222
} ) ;
181
223
}
0 commit comments