Skip to content

Commit

Permalink
fix(await-async-events): improve fixer (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
skovy committed Oct 14, 2022
1 parent 7238f76 commit 9d5554c
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 20 deletions.
24 changes: 24 additions & 0 deletions lib/node-utils/index.ts
Expand Up @@ -13,6 +13,8 @@ import {
isBlockStatement,
isCallExpression,
isExpressionStatement,
isFunctionExpression,
isFunctionDeclaration,
isImportDeclaration,
isImportNamespaceSpecifier,
isImportSpecifier,
Expand Down Expand Up @@ -95,6 +97,28 @@ export function findClosestVariableDeclaratorNode(
return findClosestVariableDeclaratorNode(node.parent);
}

export function findClosestFunctionExpressionNode(
node: TSESTree.Node | undefined
):
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression
| TSESTree.FunctionDeclaration
| null {
if (!node) {
return null;
}

if (
isArrowFunctionExpression(node) ||
isFunctionExpression(node) ||
isFunctionDeclaration(node)
) {
return node;
}

return findClosestFunctionExpressionNode(node.parent);
}

/**
* TODO: remove this one in favor of {@link findClosestCallExpressionNode}
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/node-utils/is-node-of-type.ts
Expand Up @@ -59,3 +59,6 @@ export const isReturnStatement = ASTUtils.isNodeOfType(
export const isFunctionExpression = ASTUtils.isNodeOfType(
AST_NODE_TYPES.FunctionExpression
);
export const isFunctionDeclaration = ASTUtils.isNodeOfType(
AST_NODE_TYPES.FunctionDeclaration
);
46 changes: 44 additions & 2 deletions lib/rules/await-async-events.ts
Expand Up @@ -3,6 +3,7 @@ import { ASTUtils, TSESLint, TSESTree } from '@typescript-eslint/utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
findClosestCallExpressionNode,
findClosestFunctionExpressionNode,
getFunctionName,
getInnermostReturningFunction,
getVariableReferences,
Expand Down Expand Up @@ -142,7 +143,28 @@ export default createTestingLibraryRule<Options, MessageIds>({
closestCallExpression,
fix: (fixer) => {
if (isMemberExpression(node.parent)) {
return fixer.insertTextBefore(node.parent, 'await ');
const functionExpression =
findClosestFunctionExpressionNode(node);

if (functionExpression) {
const memberExpressionFixer = fixer.insertTextBefore(
node.parent,
'await '
);

if (functionExpression.async) {
return memberExpressionFixer;
} else {
// Mutate the actual node so if other nodes exist in this
// function expression body they don't also try to fix it.
functionExpression.async = true;

return [
memberExpressionFixer,
fixer.insertTextBefore(functionExpression, 'async '),
];
}
}
}

return null;
Expand Down Expand Up @@ -175,7 +197,27 @@ export default createTestingLibraryRule<Options, MessageIds>({
closestCallExpression,
messageId: 'awaitAsyncEventWrapper',
fix: (fixer) => {
return fixer.insertTextBefore(node, 'await ');
const functionExpression =
findClosestFunctionExpressionNode(node);

if (functionExpression) {
const nodeFixer = fixer.insertTextBefore(node, 'await ');

if (functionExpression.async) {
return nodeFixer;
} else {
// Mutate the actual node so if other nodes exist in this
// function expression body they don't also try to fix it.
functionExpression.async = true;

return [
nodeFixer,
fixer.insertTextBefore(functionExpression, 'async '),
];
}
}

return null;
},
});
}
Expand Down

0 comments on commit 9d5554c

Please sign in to comment.