Skip to content

Commit 9d5554c

Browse files
authored
fix(await-async-events): improve fixer (#675)
1 parent 7238f76 commit 9d5554c

File tree

4 files changed

+240
-20
lines changed

4 files changed

+240
-20
lines changed

lib/node-utils/index.ts

+24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
isBlockStatement,
1414
isCallExpression,
1515
isExpressionStatement,
16+
isFunctionExpression,
17+
isFunctionDeclaration,
1618
isImportDeclaration,
1719
isImportNamespaceSpecifier,
1820
isImportSpecifier,
@@ -95,6 +97,28 @@ export function findClosestVariableDeclaratorNode(
9597
return findClosestVariableDeclaratorNode(node.parent);
9698
}
9799

100+
export function findClosestFunctionExpressionNode(
101+
node: TSESTree.Node | undefined
102+
):
103+
| TSESTree.ArrowFunctionExpression
104+
| TSESTree.FunctionExpression
105+
| TSESTree.FunctionDeclaration
106+
| null {
107+
if (!node) {
108+
return null;
109+
}
110+
111+
if (
112+
isArrowFunctionExpression(node) ||
113+
isFunctionExpression(node) ||
114+
isFunctionDeclaration(node)
115+
) {
116+
return node;
117+
}
118+
119+
return findClosestFunctionExpressionNode(node.parent);
120+
}
121+
98122
/**
99123
* TODO: remove this one in favor of {@link findClosestCallExpressionNode}
100124
*/

lib/node-utils/is-node-of-type.ts

+3
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ export const isReturnStatement = ASTUtils.isNodeOfType(
5959
export const isFunctionExpression = ASTUtils.isNodeOfType(
6060
AST_NODE_TYPES.FunctionExpression
6161
);
62+
export const isFunctionDeclaration = ASTUtils.isNodeOfType(
63+
AST_NODE_TYPES.FunctionDeclaration
64+
);

lib/rules/await-async-events.ts

+44-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ASTUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
33
import { createTestingLibraryRule } from '../create-testing-library-rule';
44
import {
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

Comments
 (0)