-
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
Open
eryue0220
wants to merge
3
commits into
jest-community:main
Choose a base branch
from
eryue0220:fix/issue-1800
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−0
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,116 @@ 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 this node is an argument to the call | ||
| if (!parentCall.arguments.some(arg => arg === node)) { | ||
| return false; | ||
| } | ||
|
|
||
| // 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; | ||
| } | ||
| } | ||
|
|
||
| // 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') | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Checks if a MemberExpression is accessing `.mock` property on a | ||
| * `jest.mocked()` result. This handles cases like: | ||
| * - `jest.mocked(service.method).mock.calls[0][0]` | ||
| * - `jest.mocked(service.method).mock.calls` | ||
| */ | ||
| const isAccessingMockProperty = ( | ||
| node: TSESTree.MemberExpression, | ||
| ): boolean => { | ||
| // Check if we're accessing `.mock` property | ||
| if (!isSupportedAccessor(node.property)) { | ||
| return false; | ||
| } | ||
|
|
||
| const propertyName = getAccessorValue(node.property); | ||
|
|
||
| if (propertyName !== 'mock') { | ||
| return false; | ||
| } | ||
|
|
||
| // Traverse up the chain to find if the object is a jest.mocked() call | ||
| let current: TSESTree.Node = node.object; | ||
|
|
||
| while (current) { | ||
| if (current.type === AST_NODE_TYPES.CallExpression) { | ||
| const jestFnCall = parseJestFnCall( | ||
| findTopMostCallExpression(current), | ||
| context, | ||
| ); | ||
|
|
||
| if ( | ||
| jestFnCall?.type === 'jest' && | ||
| jestFnCall.members.length >= 1 && | ||
| isIdentifier(jestFnCall.members[0], 'mocked') | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| // Continue traversing up if the current node is part of a member chain | ||
| if (current.type === AST_NODE_TYPES.MemberExpression) { | ||
| current = current.object; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| }; | ||
|
|
||
| return { | ||
| ...baseSelectors, | ||
| MemberExpression(node: TSESTree.MemberExpression): void { | ||
| // Check if this MemberExpression is an argument to jest.mocked() | ||
| if (isArgumentToJestMocked(node)) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if accessing .mock property on jest.mocked() result | ||
| if (isAccessingMockProperty(node)) { | ||
| return; | ||
| } | ||
|
|
||
| if (node.parent?.type === AST_NODE_TYPES.CallExpression) { | ||
| const jestFnCall = parseJestFnCall( | ||
| findTopMostCallExpression(node.parent), | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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