Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ module.exports = [
| :------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------ | :-- |
| [await-async-events](docs/rules/await-async-events.md) | Enforce promises from async event methods are handled | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-svelte][] ![badge-vue][] | | 🔧 |
| [await-async-queries](docs/rules/await-async-queries.md) | Enforce promises from async queries to be handled | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-svelte][] ![badge-vue][] | | 🔧 |
| [await-async-utils](docs/rules/await-async-utils.md) | Enforce promises from async utils to be awaited properly | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-svelte][] ![badge-vue][] | | |
| [await-async-utils](docs/rules/await-async-utils.md) | Enforce promises from async utils to be awaited properly | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-svelte][] ![badge-vue][] | | 🔧 |
| [consistent-data-testid](docs/rules/consistent-data-testid.md) | Ensures consistent usage of `data-testid` | | | |
| [no-await-sync-events](docs/rules/no-await-sync-events.md) | Disallow unnecessary `await` for sync events | ![badge-angular][] ![badge-dom][] ![badge-react][] | | |
| [no-await-sync-queries](docs/rules/no-await-sync-queries.md) | Disallow unnecessary `await` for sync queries | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-svelte][] ![badge-vue][] | | 🔧 |
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/await-async-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `svelte`, `vue`.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

Ensure that promises returned by async utils are handled properly.
Expand Down
20 changes: 6 additions & 14 deletions lib/rules/await-async-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isMemberExpression,
isPromiseHandled,
} from '../node-utils';
import { addAsyncToFunctionFix } from '../utils/add-async-to-function-fix';

import type { TSESTree } from '@typescript-eslint/utils';

Expand Down Expand Up @@ -164,20 +165,11 @@ export default createTestingLibraryRule<Options, MessageIds>({
);
}

const ruleFixes = [IdentifierNodeFixer];
if (!functionExpression.async) {
/**
* 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;

ruleFixes.push(
fixer.insertTextBefore(functionExpression, 'async ')
);
}

return ruleFixes;
return addAsyncToFunctionFix(
fixer,
IdentifierNodeFixer,
functionExpression
);
},
});
}
Expand Down
33 changes: 32 additions & 1 deletion lib/rules/await-async-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { ASTUtils } from '@typescript-eslint/utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
findClosestCallExpressionNode,
findClosestFunctionExpressionNode,
getDeepestIdentifierNode,
getFunctionName,
getInnermostReturningFunction,
getReferenceNode,
getVariableReferences,
isCallExpression,
isObjectPattern,
isPromiseHandled,
isProperty,
} from '../node-utils';
import { addAsyncToFunctionFix } from '../utils/add-async-to-function-fix';

import type { TSESTree } from '@typescript-eslint/utils';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';

export const RULE_NAME = 'await-async-utils';
export type MessageIds = 'asyncUtilWrapper' | 'awaitAsyncUtil';
Expand All @@ -40,6 +43,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
'Promise returned from {{ name }} wrapper over async util must be handled',
},
schema: [],
fixable: 'code',
},
defaultOptions: [],

Expand Down Expand Up @@ -91,6 +95,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

function insertAwaitBeforeNode(
fixer: TSESLint.RuleFixer,
node: TSESTree.Node
) {
return fixer.insertTextBefore(node, 'await ');
}

/*
Either we report a direct usage of an async util or a usage of a wrapper
around an async util
Expand Down Expand Up @@ -155,6 +166,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
context,
closestCallExpression.parent
);
const functionExpression = findClosestFunctionExpressionNode(node);

if (references.length === 0) {
if (!isPromiseHandled(callExpressionIdentifier)) {
Expand All @@ -164,6 +176,17 @@ export default createTestingLibraryRule<Options, MessageIds>({
data: {
name: callExpressionIdentifier.name,
},
fix: (fixer) => {
const referenceNode = getReferenceNode(
callExpressionIdentifier
);
const awaitFix = insertAwaitBeforeNode(fixer, referenceNode);
return addAsyncToFunctionFix(
fixer,
awaitFix,
functionExpression
);
},
});
}
} else {
Expand All @@ -176,6 +199,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
data: {
name: callExpressionIdentifier.name,
},
fix: (fixer) => {
const awaitFix = insertAwaitBeforeNode(fixer, referenceNode);
return addAsyncToFunctionFix(
fixer,
awaitFix,
functionExpression
);
},
});
return;
}
Expand Down
22 changes: 22 additions & 0 deletions lib/utils/add-async-to-function-fix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';

export const addAsyncToFunctionFix = (
fixer: TSESLint.RuleFixer,
ruleFix: TSESLint.RuleFix,
functionExpression:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| null
) => {
if (functionExpression && !functionExpression.async) {
/**
* 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 [ruleFix, fixer.insertTextBefore(functionExpression, 'async ')];
}
return ruleFix;
};
Loading