no-restricted-matchers.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. var _default = (0, _utils.createRule)({
  8. name: __filename,
  9. meta: {
  10. docs: {
  11. category: 'Best Practices',
  12. description: 'Disallow specific matchers & modifiers',
  13. recommended: false
  14. },
  15. type: 'suggestion',
  16. schema: [{
  17. type: 'object',
  18. additionalProperties: {
  19. type: ['string', 'null']
  20. }
  21. }],
  22. messages: {
  23. restrictedChain: 'Use of `{{ chain }}` is disallowed',
  24. restrictedChainWithMessage: '{{ message }}'
  25. }
  26. },
  27. defaultOptions: [{}],
  28. create(context, [restrictedChains]) {
  29. return {
  30. CallExpression(node) {
  31. const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
  32. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
  33. return;
  34. }
  35. const permutations = [jestFnCall.members];
  36. if (jestFnCall.members.length > 2) {
  37. permutations.push([jestFnCall.members[0], jestFnCall.members[1]]);
  38. permutations.push([jestFnCall.members[1], jestFnCall.members[2]]);
  39. }
  40. if (jestFnCall.members.length > 1) {
  41. permutations.push(...jestFnCall.members.map(nod => [nod]));
  42. }
  43. for (const permutation of permutations) {
  44. const chain = permutation.map(nod => (0, _utils.getAccessorValue)(nod)).join('.');
  45. if (chain in restrictedChains) {
  46. const message = restrictedChains[chain];
  47. context.report({
  48. messageId: message ? 'restrictedChainWithMessage' : 'restrictedChain',
  49. data: {
  50. message,
  51. chain
  52. },
  53. loc: {
  54. start: permutation[0].loc.start,
  55. end: permutation[permutation.length - 1].loc.end
  56. }
  57. });
  58. break;
  59. }
  60. }
  61. }
  62. };
  63. }
  64. });
  65. exports.default = _default;