|
| 1 | +import { Rule } from 'eslint'; |
| 2 | +import * as ESTree from 'estree'; |
| 3 | +import { |
| 4 | + getParent, |
| 5 | + getRawValue, |
| 6 | + getStringValue, |
| 7 | + isBooleanLiteral, |
| 8 | + isStringLiteral, |
| 9 | +} from '../utils/ast'; |
| 10 | +import { parseExpectCall } from '../utils/parseExpectCall'; |
| 11 | + |
| 12 | +const equalityMatchers = new Set(['toBe', 'toEqual', 'toStrictEqual']); |
| 13 | + |
| 14 | +const isString = (node: ESTree.Node) => { |
| 15 | + return isStringLiteral(node) || node.type === 'TemplateLiteral'; |
| 16 | +}; |
| 17 | + |
| 18 | +const isComparingToString = (expression: ESTree.BinaryExpression) => { |
| 19 | + return isString(expression.left) || isString(expression.right); |
| 20 | +}; |
| 21 | + |
| 22 | +const invertedOperators: Record<string, string | undefined> = { |
| 23 | + '<': '>=', |
| 24 | + '<=': '>', |
| 25 | + '>': '<=', |
| 26 | + '>=': '<', |
| 27 | +}; |
| 28 | + |
| 29 | +const operatorMatcher: Record<string, string | undefined> = { |
| 30 | + '<': 'toBeLessThan', |
| 31 | + '<=': 'toBeLessThanOrEqual', |
| 32 | + '>': 'toBeGreaterThan', |
| 33 | + '>=': 'toBeGreaterThanOrEqual', |
| 34 | +}; |
| 35 | + |
| 36 | +const determineMatcher = ( |
| 37 | + operator: string, |
| 38 | + negated: boolean, |
| 39 | +): string | null => { |
| 40 | + const op = negated ? invertedOperators[operator] : operator; |
| 41 | + return operatorMatcher[op!] ?? null; |
| 42 | +}; |
| 43 | + |
| 44 | +export default { |
| 45 | + create(context) { |
| 46 | + return { |
| 47 | + CallExpression(node) { |
| 48 | + const expectCall = parseExpectCall(context, node); |
| 49 | + if (!expectCall || expectCall.args.length === 0) return; |
| 50 | + |
| 51 | + const { args, matcher } = expectCall; |
| 52 | + const [comparison] = node.arguments; |
| 53 | + const expectCallEnd = node.range![1]; |
| 54 | + const [matcherArg] = args; |
| 55 | + |
| 56 | + if ( |
| 57 | + comparison?.type !== 'BinaryExpression' || |
| 58 | + isComparingToString(comparison) || |
| 59 | + !equalityMatchers.has(getStringValue(matcher)) || |
| 60 | + !isBooleanLiteral(matcherArg) |
| 61 | + ) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const hasNot = expectCall.modifiers.some( |
| 66 | + (node) => getStringValue(node) === 'not', |
| 67 | + ); |
| 68 | + |
| 69 | + const preferredMatcher = determineMatcher( |
| 70 | + comparison.operator, |
| 71 | + getRawValue(matcherArg) === hasNot.toString(), |
| 72 | + ); |
| 73 | + |
| 74 | + if (!preferredMatcher) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + context.report({ |
| 79 | + data: { preferredMatcher }, |
| 80 | + fix(fixer) { |
| 81 | + // Preserve the existing modifier if it's not a negation |
| 82 | + const [modifier] = expectCall.modifiers; |
| 83 | + const modifierText = |
| 84 | + modifier && getStringValue(modifier) !== 'not' |
| 85 | + ? `.${getStringValue(modifier)}` |
| 86 | + : ''; |
| 87 | + |
| 88 | + return [ |
| 89 | + // Replace the comparison argument with the left-hand side of the comparison |
| 90 | + fixer.replaceText( |
| 91 | + comparison, |
| 92 | + context.sourceCode.getText(comparison.left), |
| 93 | + ), |
| 94 | + // Replace the current matcher & modifier with the preferred matcher |
| 95 | + fixer.replaceTextRange( |
| 96 | + [expectCallEnd, getParent(matcher)!.range![1]], |
| 97 | + `${modifierText}.${preferredMatcher}`, |
| 98 | + ), |
| 99 | + // Replace the matcher argument with the right-hand side of the comparison |
| 100 | + fixer.replaceText( |
| 101 | + matcherArg, |
| 102 | + context.sourceCode.getText(comparison.right), |
| 103 | + ), |
| 104 | + ]; |
| 105 | + }, |
| 106 | + messageId: 'useToBeComparison', |
| 107 | + node: matcher, |
| 108 | + }); |
| 109 | + }, |
| 110 | + }; |
| 111 | + }, |
| 112 | + meta: { |
| 113 | + docs: { |
| 114 | + category: 'Best Practices', |
| 115 | + description: 'Suggest using the built-in comparison matchers', |
| 116 | + recommended: false, |
| 117 | + }, |
| 118 | + fixable: 'code', |
| 119 | + messages: { |
| 120 | + useToBeComparison: 'Prefer using `{{ preferredMatcher }}` instead', |
| 121 | + }, |
| 122 | + type: 'suggestion', |
| 123 | + }, |
| 124 | +} as Rule.RuleModule; |
0 commit comments