@@ -3,6 +3,7 @@ import { ASTUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
33import { createTestingLibraryRule } from '../create-testing-library-rule' ;
44import {
55 findClosestCallExpressionNode ,
6+ findClosestFunctionExpressionNode ,
67 getFunctionName ,
78 getInnermostReturningFunction ,
89 getVariableReferences ,
@@ -142,7 +143,28 @@ export default createTestingLibraryRule<Options, MessageIds>({
142143 closestCallExpression,
143144 fix : ( fixer ) => {
144145 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+ }
146168 }
147169
148170 return null ;
@@ -175,7 +197,27 @@ export default createTestingLibraryRule<Options, MessageIds>({
175197 closestCallExpression,
176198 messageId : 'awaitAsyncEventWrapper' ,
177199 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 ;
179221 } ,
180222 } ) ;
181223 }
0 commit comments