-
Notifications
You must be signed in to change notification settings - Fork 149
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
base: main
Are you sure you want to change the base?
feat(await-async-query): add auto-fix #917
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. 🚀 New features to boost your workflow:
|
102c81a
to
1facfc3
Compare
1facfc3
to
a582606
Compare
Signed-off-by: Michaël De Boey <info@michaeldeboey.be>
@Belco90 @MichaelDeBoey |
if ( | ||
isMemberExpression(identifierNode.parent) && | ||
ASTUtils.isIdentifier(identifierNode.parent.object) && | ||
identifierNode.parent.object.name === 'screen' |
There was a problem hiding this comment.
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?
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 '), | ||
]; | ||
} | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one!
Checks
Changes
Context
Fixes #914
also some of #202