Skip to content

feat(await-async-query): add auto-fix #917

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

neriyarden
Copy link
Contributor

@neriyarden neriyarden commented Jul 26, 2024

Checks

Changes

  • Add a fixer for the await-async-query rule (add 'await ' before the async queries)
  • Fix some tests names from "An invalid test" to "A valid test" in valid test cases
  • Update tests
  • Update docs

Context

Fixes #914
also some of #202

Copy link

codecov bot commented Jul 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.30%. Comparing base (26d360e) to head (7ab273d).
Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #917      +/-   ##
==========================================
+ Coverage   96.27%   96.30%   +0.03%     
==========================================
  Files          46       46              
  Lines        2472     2493      +21     
  Branches     1025     1035      +10     
==========================================
+ Hits         2380     2401      +21     
  Misses         92       92              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Belco90 Belco90 self-requested a review August 11, 2024 16:14
MichaelDeBoey
MichaelDeBoey previously approved these changes Oct 17, 2024
@MichaelDeBoey MichaelDeBoey force-pushed the feat--add-a-fixer-for-await-async-queries branch from 102c81a to 1facfc3 Compare October 17, 2024 22:08
@MichaelDeBoey MichaelDeBoey force-pushed the feat--add-a-fixer-for-await-async-queries branch from 1facfc3 to a582606 Compare October 19, 2024 01:23
MichaelDeBoey
MichaelDeBoey previously approved these changes Oct 19, 2024
Signed-off-by: Michaël De Boey <info@michaeldeboey.be>
MichaelDeBoey
MichaelDeBoey previously approved these changes Oct 19, 2024
@neriyarden
Copy link
Contributor Author

@Belco90 @MichaelDeBoey
Any update about this PR? Will it be merged?

if (
isMemberExpression(identifierNode.parent) &&
ASTUtils.isIdentifier(identifierNode.parent.object) &&
identifierNode.parent.object.name === 'screen'
Copy link
Member

@Belco90 Belco90 May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really matter if the parent object is screen? I think it should work no matter what's the name of its parent is.

For example:

test("An example test", async () => {
  const view = render(<Component />)
  const button = view.findByText('submit')
});

In this case, view.findByText wouldn't be autofixed, but it should. Can you add this scenario to the tests, and adjust the rule fix accordingly?

Comment on lines +141 to +184
const functionExpression =
findClosestFunctionExpressionNode(node);

if (!functionExpression) return null;

let IdentifierNodeFixer;
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 '
);
} else {
/**
* Add 'await' before the wrapper function, e.g.:
* const wrapper = () => screen.findByText(/foo/i);
* await wrapper();
*/
IdentifierNodeFixer = fixer.insertTextBefore(
identifierNode,
'await '
);
}

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.
*/
functionExpression.async = true;

return [
IdentifierNodeFixer,
fixer.insertTextBefore(functionExpression, 'async '),
];
}
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make 'await-async-queries' rule auto-fixable
3 participants