prefer-to-have-length.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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')) {
  37. return;
  38. }
  39. context.report({
  40. fix(fixer) {
  41. return [// remove the "length" property accessor
  42. fixer.removeRange([argument.property.range[0] - 1, argument.range[1]]), // replace the current matcher with "toHaveLength"
  43. fixer.replaceTextRange([matcher.node.object.range[1], matcher.node.range[1]], '.toHaveLength')];
  44. },
  45. messageId: 'useToHaveLength',
  46. node: matcher.node.property
  47. });
  48. }
  49. };
  50. }
  51. });
  52. exports.default = _default;