requireHyphenBeforeParamDescription.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. sourceCode,
  10. utils,
  11. report,
  12. context,
  13. jsdoc,
  14. jsdocNode
  15. }) => {
  16. const [mainCircumstance, {
  17. tags = null
  18. } = {}] = context.options;
  19. const tgs =
  20. /**
  21. * @type {null|"any"|{[key: string]: "always"|"never"}}
  22. */
  23. tags;
  24. /**
  25. * @param {import('@es-joy/jsdoccomment').JsdocTagWithInline} jsdocTag
  26. * @param {string} targetTagName
  27. * @param {"always"|"never"} [circumstance]
  28. * @returns {void}
  29. */
  30. const checkHyphens = (jsdocTag, targetTagName, circumstance = mainCircumstance) => {
  31. const always = !circumstance || circumstance === 'always';
  32. const desc = /** @type {string} */utils.getTagDescription(jsdocTag);
  33. if (!desc.trim()) {
  34. return;
  35. }
  36. const startsWithHyphen = /^\s*-/u.test(desc);
  37. if (always) {
  38. if (!startsWithHyphen) {
  39. report(`There must be a hyphen before @${targetTagName} description.`, fixer => {
  40. const lineIndex = /** @type {import('../iterateJsdoc.js').Integer} */
  41. jsdocTag.line;
  42. const sourceLines = sourceCode.getText(jsdocNode).split('\n');
  43. // Get start index of description, accounting for multi-line descriptions
  44. const description = desc.split('\n')[0];
  45. const descriptionIndex = sourceLines[lineIndex].lastIndexOf(description);
  46. const replacementLine = sourceLines[lineIndex].slice(0, descriptionIndex) + '- ' + description;
  47. sourceLines.splice(lineIndex, 1, replacementLine);
  48. const replacement = sourceLines.join('\n');
  49. return fixer.replaceText(jsdocNode, replacement);
  50. }, jsdocTag);
  51. }
  52. } else if (startsWithHyphen) {
  53. let lines = 0;
  54. for (const {
  55. tokens
  56. } of jsdocTag.source) {
  57. if (tokens.description) {
  58. break;
  59. }
  60. lines++;
  61. }
  62. utils.reportJSDoc(`There must be no hyphen before @${targetTagName} description.`, {
  63. line: jsdocTag.source[0].number + lines
  64. }, () => {
  65. for (const {
  66. tokens
  67. } of jsdocTag.source) {
  68. if (tokens.description) {
  69. tokens.description = tokens.description.replace(/^\s*-\s*/u, '');
  70. break;
  71. }
  72. }
  73. }, true);
  74. }
  75. };
  76. utils.forEachPreferredTag('param', checkHyphens);
  77. if (tgs) {
  78. const tagEntries = Object.entries(tgs);
  79. for (const [tagName, circumstance] of tagEntries) {
  80. if (tagName === '*') {
  81. const preferredParamTag = utils.getPreferredTagName({
  82. tagName: 'param'
  83. });
  84. for (const {
  85. tag
  86. } of jsdoc.tags) {
  87. if (tag === preferredParamTag || tagEntries.some(([tagNme]) => {
  88. return tagNme !== '*' && tagNme === tag;
  89. })) {
  90. continue;
  91. }
  92. utils.forEachPreferredTag(tag, (jsdocTag, targetTagName) => {
  93. checkHyphens(jsdocTag, targetTagName, /** @type {"always"|"never"} */circumstance);
  94. });
  95. }
  96. continue;
  97. }
  98. utils.forEachPreferredTag(tagName, (jsdocTag, targetTagName) => {
  99. checkHyphens(jsdocTag, targetTagName, /** @type {"always"|"never"} */circumstance);
  100. });
  101. }
  102. }
  103. }, {
  104. iterateAllJsdocs: true,
  105. meta: {
  106. docs: {
  107. description: 'Requires a hyphen before the `@param` description.',
  108. url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/require-hyphen-before-param-description.md#repos-sticky-header'
  109. },
  110. fixable: 'code',
  111. schema: [{
  112. enum: ['always', 'never'],
  113. type: 'string'
  114. }, {
  115. additionalProperties: false,
  116. properties: {
  117. tags: {
  118. anyOf: [{
  119. patternProperties: {
  120. '.*': {
  121. enum: ['always', 'never'],
  122. type: 'string'
  123. }
  124. },
  125. type: 'object'
  126. }, {
  127. enum: ['any'],
  128. type: 'string'
  129. }]
  130. }
  131. },
  132. type: 'object'
  133. }],
  134. type: 'layout'
  135. }
  136. });
  137. module.exports = exports.default;
  138. //# sourceMappingURL=requireHyphenBeforeParamDescription.js.map