prefer-comparison-matcher.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("@typescript-eslint/utils");
  7. var _utils2 = require("./utils");
  8. const isString = node => {
  9. return (0, _utils2.isStringNode)(node) || node.type === _utils.AST_NODE_TYPES.TemplateLiteral;
  10. };
  11. const isComparingToString = expression => {
  12. return isString(expression.left) || isString(expression.right);
  13. };
  14. const invertOperator = operator => {
  15. switch (operator) {
  16. case '>':
  17. return '<=';
  18. case '<':
  19. return '>=';
  20. case '>=':
  21. return '<';
  22. case '<=':
  23. return '>';
  24. }
  25. return null;
  26. };
  27. const determineMatcher = (operator, negated) => {
  28. const op = negated ? invertOperator(operator) : operator;
  29. switch (op) {
  30. case '>':
  31. return 'toBeGreaterThan';
  32. case '<':
  33. return 'toBeLessThan';
  34. case '>=':
  35. return 'toBeGreaterThanOrEqual';
  36. case '<=':
  37. return 'toBeLessThanOrEqual';
  38. }
  39. return null;
  40. };
  41. var _default = (0, _utils2.createRule)({
  42. name: __filename,
  43. meta: {
  44. docs: {
  45. category: 'Best Practices',
  46. description: 'Suggest using the built-in comparison matchers',
  47. recommended: false
  48. },
  49. messages: {
  50. useToBeComparison: 'Prefer using `{{ preferredMatcher }}` instead'
  51. },
  52. fixable: 'code',
  53. type: 'suggestion',
  54. schema: []
  55. },
  56. defaultOptions: [],
  57. create(context) {
  58. return {
  59. CallExpression(node) {
  60. const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
  61. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect' || jestFnCall.args.length === 0) {
  62. return;
  63. }
  64. const {
  65. parent: expect
  66. } = jestFnCall.head.node;
  67. if ((expect === null || expect === void 0 ? void 0 : expect.type) !== _utils.AST_NODE_TYPES.CallExpression) {
  68. return;
  69. }
  70. const {
  71. arguments: [comparison],
  72. range: [, expectCallEnd]
  73. } = expect;
  74. const {
  75. matcher
  76. } = jestFnCall;
  77. const matcherArg = (0, _utils2.getFirstMatcherArg)(jestFnCall);
  78. 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)) {
  79. return;
  80. }
  81. const [modifier] = jestFnCall.modifiers;
  82. const hasNot = jestFnCall.modifiers.some(nod => (0, _utils2.getAccessorValue)(nod) === 'not');
  83. const preferredMatcher = determineMatcher(comparison.operator, matcherArg.value === hasNot);
  84. if (!preferredMatcher) {
  85. return;
  86. }
  87. context.report({
  88. fix(fixer) {
  89. const sourceCode = context.getSourceCode(); // preserve the existing modifier if it's not a negation
  90. const modifierText = modifier && (0, _utils2.getAccessorValue)(modifier) !== 'not' ? `.${(0, _utils2.getAccessorValue)(modifier)}` : '';
  91. return [// replace the comparison argument with the left-hand side of the comparison
  92. fixer.replaceText(comparison, sourceCode.getText(comparison.left)), // replace the current matcher & modifier with the preferred matcher
  93. fixer.replaceTextRange([expectCallEnd, matcher.parent.range[1]], `${modifierText}.${preferredMatcher}`), // replace the matcher argument with the right-hand side of the comparison
  94. fixer.replaceText(matcherArg, sourceCode.getText(comparison.right))];
  95. },
  96. messageId: 'useToBeComparison',
  97. data: {
  98. preferredMatcher
  99. },
  100. node: matcher
  101. });
  102. }
  103. };
  104. }
  105. });
  106. exports.default = _default;