Skip to content

Commit

Permalink
refactor: improve comments readability
Browse files Browse the repository at this point in the history
  • Loading branch information
neriyarden authored and MichaelDeBoey committed Oct 17, 2024
1 parent 2c5cb31 commit 1facfc3
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions lib/rules/await-async-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
closestCallExpressionNode.parent
);

// check direct usage of async query:
// const element = await findByRole('button')
/**
* Check direct usage of async query:
* const element = await findByRole('button');
*/
if (references.length === 0) {
if (!isPromiseHandled(identifierNode)) {
context.report({
Expand All @@ -103,9 +105,11 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

// check references usages of async query:
// const promise = findByRole('button')
// const element = await promise
/**
* Check references usages of async query:
* const promise = findByRole('button');
* const element = await promise;
*/
for (const reference of references) {
if (
ASTUtils.isIdentifier(reference.identifier) &&
Expand All @@ -129,7 +133,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
functionWrappersNames.includes(identifierNode.name) &&
!isPromiseHandled(identifierNode)
) {
// check async queries used within a wrapper previously detected
// Check async queries used within a wrapper previously detected
context.report({
node: identifierNode,
messageId: 'asyncQueryWrapper',
Expand All @@ -141,19 +145,23 @@ export default createTestingLibraryRule<Options, MessageIds>({
if (!functionExpression) return null;

let IdentifierNodeFixer;
// If the wrapper is a property of an object,
// add 'await' before the object, e.g.:
// const obj = { wrapper: () => screen.findByText(/foo/i) };
// await obj.wrapper();
if (isMemberExpression(identifierNode.parent)) {
/**
* If the wrapper is a property of an object,
* add 'await' before the object, e.g.:
* const obj = { wrapper: () => screen.findByText(/foo/i) };
* await obj.wrapper();
*/
IdentifierNodeFixer = fixer.insertTextBefore(
identifierNode.parent,
'await '
);
// Otherwise, add 'await' before the wrapper function, e.g.:
// const wrapper = () => screen.findByText(/foo/i);
// await wrapper();
} else {
/**
* Add 'await' before the wrapper function, e.g.:
* const wrapper = () => screen.findByText(/foo/i);
* await wrapper();
*/
IdentifierNodeFixer = fixer.insertTextBefore(
identifierNode,
'await '
Expand All @@ -163,8 +171,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
if (functionExpression.async) {
return IdentifierNodeFixer;
} else {
// Mutate the actual node so if other nodes exist in this
// function expression body they don't also try to fix it.
/**
* 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 [
Expand Down

0 comments on commit 1facfc3

Please sign in to comment.