prefer-equality-matcher.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  7. var _utils = require("./utils");
  8. const isBooleanLiteral = node => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
  9. /**
  10. * Checks if the given `ParsedExpectMatcher` is a call to one of the equality matchers,
  11. * with a boolean literal as the sole argument.
  12. *
  13. * @example javascript
  14. * toBe(true);
  15. * toEqual(false);
  16. *
  17. * @param {ParsedExpectMatcher} matcher
  18. *
  19. * @return {matcher is ParsedBooleanEqualityMatcher}
  20. */
  21. const isBooleanEqualityMatcher = matcher => (0, _utils.isParsedEqualityMatcherCall)(matcher) && isBooleanLiteral((0, _utils.followTypeAssertionChain)(matcher.arguments[0]));
  22. var _default = (0, _utils.createRule)({
  23. name: __filename,
  24. meta: {
  25. docs: {
  26. category: 'Best Practices',
  27. description: 'Suggest using the built-in equality matchers',
  28. recommended: false,
  29. suggestion: true
  30. },
  31. messages: {
  32. useEqualityMatcher: 'Prefer using one of the equality matchers instead',
  33. suggestEqualityMatcher: 'Use `{{ equalityMatcher }}`'
  34. },
  35. hasSuggestions: true,
  36. type: 'suggestion',
  37. schema: []
  38. },
  39. defaultOptions: [],
  40. create(context) {
  41. return {
  42. CallExpression(node) {
  43. if (!(0, _utils.isExpectCall)(node)) {
  44. return;
  45. }
  46. const {
  47. expect: {
  48. arguments: [comparison],
  49. range: [, expectCallEnd]
  50. },
  51. matcher,
  52. modifier
  53. } = (0, _utils.parseExpectCall)(node);
  54. if (!matcher || (comparison === null || comparison === void 0 ? void 0 : comparison.type) !== _experimentalUtils.AST_NODE_TYPES.BinaryExpression || comparison.operator !== '===' && comparison.operator !== '!==' || !isBooleanEqualityMatcher(matcher)) {
  55. return;
  56. }
  57. const matcherValue = (0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value; // we need to negate the expectation if the current expected
  58. // value is itself negated by the "not" modifier
  59. const addNotModifier = (comparison.operator === '!==' ? !matcherValue : matcherValue) === !!modifier;
  60. const buildFixer = equalityMatcher => fixer => {
  61. const sourceCode = context.getSourceCode();
  62. return [// replace the comparison argument with the left-hand side of the comparison
  63. fixer.replaceText(comparison, sourceCode.getText(comparison.left)), // replace the current matcher & modifier with the preferred matcher
  64. fixer.replaceTextRange([expectCallEnd, matcher.node.range[1]], addNotModifier ? `.${_utils.ModifierName.not}.${equalityMatcher}` : `.${equalityMatcher}`), // replace the matcher argument with the right-hand side of the comparison
  65. fixer.replaceText(matcher.arguments[0], sourceCode.getText(comparison.right))];
  66. };
  67. context.report({
  68. messageId: 'useEqualityMatcher',
  69. suggest: ['toBe', 'toEqual', 'toStrictEqual'].map(equalityMatcher => ({
  70. messageId: 'suggestEqualityMatcher',
  71. data: {
  72. equalityMatcher
  73. },
  74. fix: buildFixer(equalityMatcher)
  75. })),
  76. node: (modifier || matcher).node.property
  77. });
  78. }
  79. };
  80. }
  81. });
  82. exports.default = _default;