prefer-to-contain.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /**
  9. * Checks if the given `node` is a `CallExpression` representing the calling
  10. * of an `includes`-like method that can be 'fixed' (using `toContain`).
  11. *
  12. * @param {CallExpression} node
  13. *
  14. * @return {node is FixableIncludesCallExpression}
  15. */
  16. const isFixableIncludesCallExpression = node => node.type === _utils.AST_NODE_TYPES.CallExpression && node.callee.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.callee.property, 'includes') && (0, _utils2.hasOnlyOneArgument)(node) && node.arguments[0].type !== _utils.AST_NODE_TYPES.SpreadElement; // expect(array.includes(<value>)[not.]{toBe,toEqual}(<boolean>)
  17. var _default = (0, _utils2.createRule)({
  18. name: __filename,
  19. meta: {
  20. docs: {
  21. category: 'Best Practices',
  22. description: 'Suggest using `toContain()`',
  23. recommended: false
  24. },
  25. messages: {
  26. useToContain: 'Use toContain() instead'
  27. },
  28. fixable: 'code',
  29. type: 'suggestion',
  30. schema: []
  31. },
  32. defaultOptions: [],
  33. create(context) {
  34. return {
  35. CallExpression(node) {
  36. const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
  37. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect' || jestFnCall.args.length === 0) {
  38. return;
  39. }
  40. const {
  41. parent: expect
  42. } = jestFnCall.head.node;
  43. if ((expect === null || expect === void 0 ? void 0 : expect.type) !== _utils.AST_NODE_TYPES.CallExpression) {
  44. return;
  45. }
  46. const {
  47. arguments: [includesCall],
  48. range: [, expectCallEnd]
  49. } = expect;
  50. const {
  51. matcher
  52. } = jestFnCall;
  53. const matcherArg = (0, _utils2.getFirstMatcherArg)(jestFnCall);
  54. if (!includesCall || matcherArg.type === _utils.AST_NODE_TYPES.SpreadElement || !_utils2.EqualityMatcher.hasOwnProperty((0, _utils2.getAccessorValue)(matcher)) || !(0, _utils2.isBooleanLiteral)(matcherArg) || !isFixableIncludesCallExpression(includesCall)) {
  55. return;
  56. }
  57. const hasNot = jestFnCall.modifiers.some(nod => (0, _utils2.getAccessorValue)(nod) === 'not');
  58. context.report({
  59. fix(fixer) {
  60. const sourceCode = context.getSourceCode(); // we need to negate the expectation if the current expected
  61. // value is itself negated by the "not" modifier
  62. const addNotModifier = matcherArg.value === hasNot;
  63. return [// remove the "includes" call entirely
  64. fixer.removeRange([includesCall.callee.property.range[0] - 1, includesCall.range[1]]), // replace the current matcher with "toContain", adding "not" if needed
  65. fixer.replaceTextRange([expectCallEnd, matcher.parent.range[1]], addNotModifier ? `.${_utils2.ModifierName.not}.toContain` : '.toContain'), // replace the matcher argument with the value from the "includes"
  66. fixer.replaceText(jestFnCall.args[0], sourceCode.getText(includesCall.arguments[0]))];
  67. },
  68. messageId: 'useToContain',
  69. node: matcher
  70. });
  71. }
  72. };
  73. }
  74. });
  75. exports.default = _default;