-
Notifications
You must be signed in to change notification settings - Fork 231
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
feat(valid-expect): supporting automatically fixing missing await
in some cases
#1574
Changes from 5 commits
e56ce4d
545e505
74890c4
90028bf
eb7a2fc
ee54e6a
a976141
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,14 @@ | |
💼 This rule is enabled in the ✅ `recommended` | ||
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations). | ||
|
||
🔧 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 --> | ||
|
||
Note: Test function will be fixed if it is async and does not have await in the | ||
async assertion. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could use fancy alerts here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! I made the fix in commit ee54e6a. |
||
|
||
Ensure `expect()` is called with a single argument and there is an actual | ||
expectation made. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import { | |
ModifierName, | ||
createRule, | ||
getAccessorValue, | ||
getSourceCode, | ||
isFunction, | ||
isSupportedAccessor, | ||
parseJestFnCallWithReason, | ||
} from './utils'; | ||
|
@@ -48,6 +50,18 @@ const findPromiseCallExpressionNode = (node: TSESTree.Node) => | |
? getPromiseCallExpressionNode(node.parent) | ||
: null; | ||
|
||
const findFirstAsyncFunction = ({ | ||
parent, | ||
}: TSESTree.Node): TSESTree.Node | null => { | ||
if (!parent) { | ||
return null; | ||
} | ||
|
||
return isFunction(parent) && parent.async | ||
? parent | ||
: findFirstAsyncFunction(parent); | ||
}; | ||
Comment on lines
+53
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the late response; I only saw this after the new release. Doesn't this cause false-positives for non-async functions contained in async functions? It's probably a really rare situation in test files, but I think it would be more correct like this: if (!parent) {
return null;
}
if (isFunction(parent)) {
return parent.async ? parent : null;
}
return findFirstAsyncFunction(parent); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see the logic is changed in #1579 anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have a test case that's failing? If so please share either way so we can ensure that going forward it doesn't fail There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No failing test case, just noticed this while reading the code. |
||
|
||
const getParentIfThenified = (node: TSESTree.Node): TSESTree.Node => { | ||
const grandParentNode = node.parent?.parent; | ||
|
||
|
@@ -127,6 +141,7 @@ export default createRule<[Options], MessageIds>({ | |
promisesWithAsyncAssertionsMustBeAwaited: | ||
'Promises which return async assertions must be awaited{{ orReturned }}', | ||
}, | ||
fixable: 'code', | ||
type: 'suggestion', | ||
schema: [ | ||
{ | ||
|
@@ -339,6 +354,25 @@ export default createRule<[Options], MessageIds>({ | |
? 'asyncMustBeAwaited' | ||
: 'promisesWithAsyncAssertionsMustBeAwaited', | ||
node, | ||
fix(fixer) { | ||
if (!findFirstAsyncFunction(finalNode)) { | ||
return []; | ||
} | ||
const returnStatement = | ||
finalNode.parent?.type === AST_NODE_TYPES.ReturnStatement | ||
? finalNode.parent | ||
: null; | ||
|
||
if (alwaysAwait && returnStatement) { | ||
const sourceCodeText = | ||
getSourceCode(context).getText(returnStatement); | ||
const replacedText = sourceCodeText.replace('return', 'await'); | ||
|
||
return fixer.replaceText(returnStatement, replacedText); | ||
} | ||
|
||
return fixer.insertTextBefore(finalNode, 'await '); | ||
}, | ||
}); | ||
|
||
if (isParentArrayExpression) { | ||
|
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.
can we have it say that "some patterns are fixable" or something like that? specifically now, only if the function is already 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.
I attempted to fix it with commit 90028bf. What do you think?
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.
fwiw it seems currently the docs generator doesn't support customizing this in anyway, so I think what you've done is fine for now - @bmish might be something worth adding; even just the ability to explicitly name particular rules as "partially fixable" in the config which would case this string to include with "in some cases" suffix