-
Notifications
You must be signed in to change notification settings - Fork 248
fix(unbound-method): fix issue 1800 #1862
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { | |||||||||||||||||||||||||
| findTopMostCallExpression, | ||||||||||||||||||||||||||
| getAccessorValue, | ||||||||||||||||||||||||||
| isIdentifier, | ||||||||||||||||||||||||||
| isSupportedAccessor, | ||||||||||||||||||||||||||
| parseJestFnCall, | ||||||||||||||||||||||||||
| } from './utils'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -73,9 +74,58 @@ export default createRule<Options, MessageIds>({ | |||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Checks if a MemberExpression is an argument to a `jest.mocked()` call. | ||||||||||||||||||||||||||
| * This handles cases like `jest.mocked(service.method)` where `service.method` | ||||||||||||||||||||||||||
| * should not be flagged as an unbound method. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| const isArgumentToJestMocked = ( | ||||||||||||||||||||||||||
| node: TSESTree.MemberExpression, | ||||||||||||||||||||||||||
| ): boolean => { | ||||||||||||||||||||||||||
| // Check if the immediate parent is a CallExpression | ||||||||||||||||||||||||||
| if (node.parent?.type !== AST_NODE_TYPES.CallExpression) { | ||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const parentCall = node.parent; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Check if the call is jest.mocked() by examining the callee | ||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||
| parentCall.callee.type === AST_NODE_TYPES.MemberExpression && | ||||||||||||||||||||||||||
| isSupportedAccessor(parentCall.callee.object) && | ||||||||||||||||||||||||||
|
Collaborator
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.
that in turn means you should be able to refactor this all to be a |
||||||||||||||||||||||||||
| isSupportedAccessor(parentCall.callee.property) | ||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||
| const objectName = getAccessorValue(parentCall.callee.object); | ||||||||||||||||||||||||||
| const propertyName = getAccessorValue(parentCall.callee.property); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (objectName === 'jest' && propertyName === 'mocked') { | ||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Also try using parseJestFnCall as a fallback | ||||||||||||||||||||||||||
| // const jestFnCall = parseJestFnCall( | ||||||||||||||||||||||||||
| // findTopMostCallExpression(parentCall), | ||||||||||||||||||||||||||
| // context, | ||||||||||||||||||||||||||
| // ); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // return ( | ||||||||||||||||||||||||||
| // jestFnCall?.type === 'jest' && | ||||||||||||||||||||||||||
| // jestFnCall.members.length >= 1 && | ||||||||||||||||||||||||||
| // isIdentifier(jestFnCall.members[0], 'mocked') | ||||||||||||||||||||||||||
| // ); | ||||||||||||||||||||||||||
|
Comment on lines
+107
to
+118
Collaborator
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.
Suggested change
|
||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| ...baseSelectors, | ||||||||||||||||||||||||||
| MemberExpression(node: TSESTree.MemberExpression): void { | ||||||||||||||||||||||||||
| // Check if this MemberExpression is an argument to jest.mocked() | ||||||||||||||||||||||||||
| if (isArgumentToJestMocked(node)) { | ||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (node.parent?.type === AST_NODE_TYPES.CallExpression) { | ||||||||||||||||||||||||||
| const jestFnCall = parseJestFnCall( | ||||||||||||||||||||||||||
| findTopMostCallExpression(node.parent), | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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.
this is the opposite of the check we do later on, so ideally we can merge the two but that might be messy due to the optional chaining and require an
elsewhich I try to avoid so not too fussed to keep it like this for now