|
| 1 | +import { Rule } from 'eslint'; |
| 2 | +import { |
| 3 | + equalityMatchers, |
| 4 | + getParent, |
| 5 | + getRawValue, |
| 6 | + getStringValue, |
| 7 | + isBooleanLiteral, |
| 8 | +} from '../utils/ast'; |
| 9 | +import { parseExpectCall } from '../utils/parseExpectCall'; |
| 10 | + |
| 11 | +export default { |
| 12 | + create(context) { |
| 13 | + return { |
| 14 | + CallExpression(node) { |
| 15 | + const expectCall = parseExpectCall(context, node); |
| 16 | + if (!expectCall || expectCall.args.length === 0) return; |
| 17 | + |
| 18 | + const { args, matcher } = expectCall; |
| 19 | + const [comparison] = node.arguments; |
| 20 | + const expectCallEnd = node.range![1]; |
| 21 | + const [matcherArg] = args; |
| 22 | + |
| 23 | + if ( |
| 24 | + comparison?.type !== 'BinaryExpression' || |
| 25 | + (comparison.operator !== '===' && comparison.operator !== '!==') || |
| 26 | + !equalityMatchers.has(getStringValue(matcher)) || |
| 27 | + !isBooleanLiteral(matcherArg) |
| 28 | + ) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const matcherValue = getRawValue(matcherArg) === 'true'; |
| 33 | + const [modifier] = expectCall.modifiers; |
| 34 | + const hasNot = expectCall.modifiers.some( |
| 35 | + (node) => getStringValue(node) === 'not', |
| 36 | + ); |
| 37 | + |
| 38 | + // we need to negate the expectation if the current expected |
| 39 | + // value is itself negated by the "not" modifier |
| 40 | + const addNotModifier = |
| 41 | + (comparison.operator === '!==' ? !matcherValue : matcherValue) === |
| 42 | + hasNot; |
| 43 | + |
| 44 | + context.report({ |
| 45 | + messageId: 'useEqualityMatcher', |
| 46 | + node: matcher, |
| 47 | + suggest: [...equalityMatchers.keys()].map((equalityMatcher) => ({ |
| 48 | + data: { matcher: equalityMatcher }, |
| 49 | + fix(fixer) { |
| 50 | + // preserve the existing modifier if it's not a negation |
| 51 | + let modifierText = |
| 52 | + modifier && getStringValue(modifier) !== 'not' |
| 53 | + ? `.${getStringValue(modifier)}` |
| 54 | + : ''; |
| 55 | + |
| 56 | + if (addNotModifier) { |
| 57 | + modifierText += `.not`; |
| 58 | + } |
| 59 | + |
| 60 | + return [ |
| 61 | + // replace the comparison argument with the left-hand side of the comparison |
| 62 | + fixer.replaceText( |
| 63 | + comparison, |
| 64 | + context.sourceCode.getText(comparison.left), |
| 65 | + ), |
| 66 | + // replace the current matcher & modifier with the preferred matcher |
| 67 | + fixer.replaceTextRange( |
| 68 | + [expectCallEnd, getParent(matcher)!.range![1]], |
| 69 | + `${modifierText}.${equalityMatcher}`, |
| 70 | + ), |
| 71 | + // replace the matcher argument with the right-hand side of the comparison |
| 72 | + fixer.replaceText( |
| 73 | + matcherArg, |
| 74 | + context.sourceCode.getText(comparison.right), |
| 75 | + ), |
| 76 | + ]; |
| 77 | + }, |
| 78 | + messageId: 'suggestEqualityMatcher', |
| 79 | + })), |
| 80 | + }); |
| 81 | + }, |
| 82 | + }; |
| 83 | + }, |
| 84 | + meta: { |
| 85 | + docs: { |
| 86 | + category: 'Best Practices', |
| 87 | + description: 'Suggest using the built-in equality matchers', |
| 88 | + recommended: false, |
| 89 | + url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-equality-matcher.md', |
| 90 | + }, |
| 91 | + hasSuggestions: true, |
| 92 | + messages: { |
| 93 | + suggestEqualityMatcher: 'Use `{{ matcher }}`', |
| 94 | + useEqualityMatcher: 'Prefer using one of the equality matchers instead', |
| 95 | + }, |
| 96 | + type: 'suggestion', |
| 97 | + }, |
| 98 | +} as Rule.RuleModule; |
0 commit comments