prefer-to-have-length.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. var _default = (0, _utils.createRule)({
  9. name: __filename,
  10. meta: {
  11. docs: {
  12. category: 'Best Practices',
  13. description: 'Suggest using `toHaveLength()`',
  14. recommended: false
  15. },
  16. messages: {
  17. useToHaveLength: 'Use toHaveLength() instead'
  18. },
  19. fixable: 'code',
  20. type: 'suggestion',
  21. schema: []
  22. },
  23. defaultOptions: [],
  24. create(context) {
  25. return {
  26. CallExpression(node) {
  27. if (!(0, _utils.isExpectCall)(node)) {
  28. return;
  29. }
  30. const {
  31. expect: {
  32. arguments: [argument]
  33. },
  34. matcher
  35. } = (0, _utils.parseExpectCall)(node);
  36. if (!matcher || !(0, _utils.isParsedEqualityMatcherCall)(matcher) || (argument === null || argument === void 0 ? void 0 : argument.type) !== _experimentalUtils.AST_NODE_TYPES.MemberExpression || !(0, _utils.isSupportedAccessor)(argument.property, 'length') || argument.property.type !== _experimentalUtils.AST_NODE_TYPES.Identifier) {
  37. return;
  38. }
  39. context.report({
  40. fix(fixer) {
  41. const propertyDot = context.getSourceCode().getFirstTokenBetween(argument.object, argument.property, token => token.value === '.');
  42. /* istanbul ignore if */
  43. if (propertyDot === null) {
  44. throw new Error(`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
  45. }
  46. return [fixer.remove(propertyDot), fixer.remove(argument.property), fixer.replaceText(matcher.node.property, 'toHaveLength')];
  47. },
  48. messageId: 'useToHaveLength',
  49. node: matcher.node.property
  50. });
  51. }
  52. };
  53. }
  54. });
  55. exports.default = _default;