Skip to content

fix(await-async-events): improve fixer #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/node-utils/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading