requireExample.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _iterateJsdoc = _interopRequireDefault(require("../iterateJsdoc.js"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var _default = exports.default = (0, _iterateJsdoc.default)(({
  9. context,
  10. jsdoc,
  11. report,
  12. utils
  13. }) => {
  14. if (utils.avoidDocs()) {
  15. return;
  16. }
  17. const {
  18. enableFixer = true,
  19. exemptNoArguments = false
  20. } = context.options[0] || {};
  21. const targetTagName = 'example';
  22. const functionExamples = jsdoc.tags.filter(({
  23. tag
  24. }) => {
  25. return tag === targetTagName;
  26. });
  27. if (!functionExamples.length) {
  28. if (exemptNoArguments && utils.isIteratingFunction() && !utils.hasParams()) {
  29. return;
  30. }
  31. utils.reportJSDoc(`Missing JSDoc @${targetTagName} declaration.`, null, () => {
  32. if (enableFixer) {
  33. utils.addTag(targetTagName);
  34. }
  35. });
  36. return;
  37. }
  38. for (const example of functionExamples) {
  39. const exampleContent = `${example.name} ${utils.getTagDescription(example)}`.trim().split('\n').filter(Boolean);
  40. if (!exampleContent.length) {
  41. report(`Missing JSDoc @${targetTagName} description.`, null, example);
  42. }
  43. }
  44. }, {
  45. contextDefaults: true,
  46. meta: {
  47. docs: {
  48. description: 'Requires that all functions have examples.',
  49. url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/require-example.md#repos-sticky-header'
  50. },
  51. fixable: 'code',
  52. schema: [{
  53. additionalProperties: false,
  54. properties: {
  55. checkConstructors: {
  56. default: true,
  57. type: 'boolean'
  58. },
  59. checkGetters: {
  60. default: false,
  61. type: 'boolean'
  62. },
  63. checkSetters: {
  64. default: false,
  65. type: 'boolean'
  66. },
  67. contexts: {
  68. items: {
  69. anyOf: [{
  70. type: 'string'
  71. }, {
  72. additionalProperties: false,
  73. properties: {
  74. comment: {
  75. type: 'string'
  76. },
  77. context: {
  78. type: 'string'
  79. }
  80. },
  81. type: 'object'
  82. }]
  83. },
  84. type: 'array'
  85. },
  86. enableFixer: {
  87. default: true,
  88. type: 'boolean'
  89. },
  90. exemptedBy: {
  91. items: {
  92. type: 'string'
  93. },
  94. type: 'array'
  95. },
  96. exemptNoArguments: {
  97. default: false,
  98. type: 'boolean'
  99. }
  100. },
  101. type: 'object'
  102. }],
  103. type: 'suggestion'
  104. }
  105. });
  106. module.exports = exports.default;
  107. //# sourceMappingURL=requireExample.js.map