123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _utils = require("@typescript-eslint/utils");
- var _utils2 = require("./utils");
- const isString = node => {
- return (0, _utils2.isStringNode)(node) || node.type === _utils.AST_NODE_TYPES.TemplateLiteral;
- };
- const isComparingToString = expression => {
- return isString(expression.left) || isString(expression.right);
- };
- const invertOperator = operator => {
- switch (operator) {
- case '>':
- return '<=';
- case '<':
- return '>=';
- case '>=':
- return '<';
- case '<=':
- return '>';
- }
- return null;
- };
- const determineMatcher = (operator, negated) => {
- const op = negated ? invertOperator(operator) : operator;
- switch (op) {
- case '>':
- return 'toBeGreaterThan';
- case '<':
- return 'toBeLessThan';
- case '>=':
- return 'toBeGreaterThanOrEqual';
- case '<=':
- return 'toBeLessThanOrEqual';
- }
- return null;
- };
- var _default = (0, _utils2.createRule)({
- name: __filename,
- meta: {
- docs: {
- category: 'Best Practices',
- description: 'Suggest using the built-in comparison matchers',
- recommended: false
- },
- messages: {
- useToBeComparison: 'Prefer using `{{ preferredMatcher }}` instead'
- },
- fixable: 'code',
- type: 'suggestion',
- schema: []
- },
- defaultOptions: [],
- create(context) {
- return {
- CallExpression(node) {
- const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
- if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect' || jestFnCall.args.length === 0) {
- return;
- }
- const {
- parent: expect
- } = jestFnCall.head.node;
- if ((expect === null || expect === void 0 ? void 0 : expect.type) !== _utils.AST_NODE_TYPES.CallExpression) {
- return;
- }
- const {
- arguments: [comparison],
- range: [, expectCallEnd]
- } = expect;
- const {
- matcher
- } = jestFnCall;
- const matcherArg = (0, _utils2.getFirstMatcherArg)(jestFnCall);
- if ((comparison === null || comparison === void 0 ? void 0 : comparison.type) !== _utils.AST_NODE_TYPES.BinaryExpression || isComparingToString(comparison) || !_utils2.EqualityMatcher.hasOwnProperty((0, _utils2.getAccessorValue)(matcher)) || !(0, _utils2.isBooleanLiteral)(matcherArg)) {
- return;
- }
- const [modifier] = jestFnCall.modifiers;
- const hasNot = jestFnCall.modifiers.some(nod => (0, _utils2.getAccessorValue)(nod) === 'not');
- const preferredMatcher = determineMatcher(comparison.operator, matcherArg.value === hasNot);
- if (!preferredMatcher) {
- return;
- }
- context.report({
- fix(fixer) {
- const sourceCode = context.getSourceCode(); // preserve the existing modifier if it's not a negation
- const modifierText = modifier && (0, _utils2.getAccessorValue)(modifier) !== 'not' ? `.${(0, _utils2.getAccessorValue)(modifier)}` : '';
- return [// replace the comparison argument with the left-hand side of the comparison
- fixer.replaceText(comparison, sourceCode.getText(comparison.left)), // replace the current matcher & modifier with the preferred matcher
- fixer.replaceTextRange([expectCallEnd, matcher.parent.range[1]], `${modifierText}.${preferredMatcher}`), // replace the matcher argument with the right-hand side of the comparison
- fixer.replaceText(matcherArg, sourceCode.getText(comparison.right))];
- },
- messageId: 'useToBeComparison',
- data: {
- preferredMatcher
- },
- node: matcher
- });
- }
- };
- }
- });
- exports.default = _default;
|