requireThrows.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  9. * We can skip checking for a throws value, in case the documentation is inherited
  10. * or the method is either a constructor or an abstract method.
  11. * @param {import('../iterateJsdoc.js').Utils} utils a reference to the utils which are used to probe if a tag is present or not.
  12. * @returns {boolean} true in case deep checking can be skipped; otherwise false.
  13. */
  14. const canSkip = utils => {
  15. return utils.hasATag([
  16. // inheritdoc implies that all documentation is inherited
  17. // see https://jsdoc.app/tags-inheritdoc.html
  18. //
  19. // Abstract methods are by definition incomplete,
  20. // so it is not necessary to document that they throw an error.
  21. 'abstract', 'virtual',
  22. // The designated type can itself document `@throws`
  23. 'type']) || utils.avoidDocs();
  24. };
  25. var _default = exports.default = (0, _iterateJsdoc.default)(({
  26. report,
  27. utils
  28. }) => {
  29. // A preflight check. We do not need to run a deep check for abstract
  30. // functions.
  31. if (canSkip(utils)) {
  32. return;
  33. }
  34. const tagName = /** @type {string} */utils.getPreferredTagName({
  35. tagName: 'throws'
  36. });
  37. if (!tagName) {
  38. return;
  39. }
  40. const tags = utils.getTags(tagName);
  41. const iteratingFunction = utils.isIteratingFunction();
  42. // In case the code returns something, we expect a return value in JSDoc.
  43. const [tag] = tags;
  44. const missingThrowsTag = typeof tag === 'undefined' || tag === null;
  45. const shouldReport = () => {
  46. if (!missingThrowsTag) {
  47. if (tag.type.trim() === 'never' && iteratingFunction && utils.hasThrowValue()) {
  48. report(`JSDoc @${tagName} declaration set to "never" but throw value found.`);
  49. }
  50. return false;
  51. }
  52. return iteratingFunction && utils.hasThrowValue();
  53. };
  54. if (shouldReport()) {
  55. report(`Missing JSDoc @${tagName} declaration.`);
  56. }
  57. }, {
  58. contextDefaults: true,
  59. meta: {
  60. docs: {
  61. description: 'Requires that throw statements are documented.',
  62. url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/require-throws.md#repos-sticky-header'
  63. },
  64. schema: [{
  65. additionalProperties: false,
  66. properties: {
  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. exemptedBy: {
  87. items: {
  88. type: 'string'
  89. },
  90. type: 'array'
  91. }
  92. },
  93. type: 'object'
  94. }],
  95. type: 'suggestion'
  96. }
  97. });
  98. module.exports = exports.default;
  99. //# sourceMappingURL=requireThrows.js.map