feat(eslint-plugin-jest): add prefer-to-have-been-called rule#1019
feat(eslint-plugin-jest): add prefer-to-have-been-called rule#1019eryue0220 wants to merge 3 commits into
prefer-to-have-been-called rule#1019Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the jest/prefer-to-have-been-called rule and refactors existing Jest rules to use new shared utility functions for type assertion unwrapping and member access. Feedback on the new rule suggests consolidating the fix logic into a single range replacement to improve robustness and ensure alignment with ESLint semantics regarding edge cases like comments or trailing commas.
| rule.RuleFixReplaceRange( | ||
| core.NewTextRange(matcherCall.Arguments.Pos(), matcherCall.Arguments.End()), | ||
| "", | ||
| ), | ||
| rule.RuleFixReplaceRange( | ||
| core.NewTextRange(replaceStart, matcherParent.End()), | ||
| replacementMatcher, | ||
| ), |
There was a problem hiding this comment.
The current implementation uses two separate fixes to replace the matcher name and clear the arguments. This can be simplified into a single fix that replaces the entire range from the receiver's end to the end of the call expression. This approach is more robust as it correctly handles potential edge cases like trailing commas or comments within the parentheses, ensuring exact alignment with ESLint's semantics for this rule.
| rule.RuleFixReplaceRange( | |
| core.NewTextRange(matcherCall.Arguments.Pos(), matcherCall.Arguments.End()), | |
| "", | |
| ), | |
| rule.RuleFixReplaceRange( | |
| core.NewTextRange(replaceStart, matcherParent.End()), | |
| replacementMatcher, | |
| ), | |
| rule.RuleFixReplaceRange( | |
| core.NewTextRange(replaceStart, node.End()), | |
| replacementMatcher+"()", | |
| ), |
References
- For linter rules ported from or inspired by ESLint, maintain exact alignment with ESLint's semantics, even for edge cases where a simpler implementation might seem practically equivalent.
| } | ||
|
|
||
| func isZeroLiteral(node *ast.Node) bool { | ||
| node = jestUtils.UnwrapTypeAssertions(node) |
There was a problem hiding this comment.
Nit worth fixing: this should be UnwrapBasicTypeAssertions. Upstream's getFirstMatcherArg → followTypeAssertionChain only unwraps as and <T> assertions — not ! or satisfies — so these stay unflagged upstream but get reported (and autofixed) here:
expect(method).toBeCalledTimes(0!);
expect(method).toBeCalledTimes(0 satisfies number);prefer_to_be uses the same upstream helper and already calls UnwrapBasicTypeAssertions, so this keeps the two rules consistent.
Summary
prefer-to-have-been-calledfrom eslint-plugin-jest to rslint.Related Links
Tracking issue: #476
eslint-plugin-jest/prefer-to-have-been-called doc code
Checklist