diff --git a/docs/rules/require-hyphen-before-param-description.md b/docs/rules/require-hyphen-before-param-description.md index 67c90611..6022aa17 100644 --- a/docs/rules/require-hyphen-before-param-description.md +++ b/docs/rules/require-hyphen-before-param-description.md @@ -193,6 +193,15 @@ function quux () { */ // "jsdoc/require-hyphen-before-param-description": ["error"|"warn", "always",{"tags":{"*":"never","property":"always"}}] // Message: There must be no hyphen before @returns description. + +/** + * @param {( + * | string + * | number + * )} input The input value + */ +function test(input) {} +// Message: There must be a hyphen before @param description. ```` diff --git a/src/rules/requireHyphenBeforeParamDescription.js b/src/rules/requireHyphenBeforeParamDescription.js index 791e842f..327e8857 100644 --- a/src/rules/requireHyphenBeforeParamDescription.js +++ b/src/rules/requireHyphenBeforeParamDescription.js @@ -33,38 +33,39 @@ export default iterateJsdoc(({ } const startsWithHyphen = (/^\s*-/u).test(desc); - if (always) { - if (!startsWithHyphen) { - report(`There must be a hyphen before @${targetTagName} description.`, (fixer) => { - const lineIndex = /** @type {import('../iterateJsdoc.js').Integer} */ ( - jsdocTag.line - ); - const sourceLines = sourceCode.getText(jsdocNode).split('\n'); - - // Get start index of description, accounting for multi-line descriptions - const description = desc.split('\n')[0]; - const descriptionIndex = sourceLines[lineIndex].lastIndexOf(description); + let lines = 0; + for (const { + tokens, + } of jsdocTag.source) { + if (tokens.description) { + break; + } - const replacementLine = sourceLines[lineIndex] - .slice(0, descriptionIndex) + '- ' + description; - sourceLines.splice(lineIndex, 1, replacementLine); - const replacement = sourceLines.join('\n'); + lines++; + } - return fixer.replaceText(jsdocNode, replacement); - }, jsdocTag); + if (always) { + if (!startsWithHyphen) { + utils.reportJSDoc( + `There must be a hyphen before @${targetTagName} description.`, + { + line: jsdocTag.source[0].number + lines, + }, + () => { + for (const { + tokens, + } of jsdocTag.source) { + if (tokens.description) { + tokens.description = tokens.description.replace( + /^(\s*)/u, '$1- ', + ); + break; + } + } + } + ); } } else if (startsWithHyphen) { - let lines = 0; - for (const { - tokens, - } of jsdocTag.source) { - if (tokens.description) { - break; - } - - lines++; - } - utils.reportJSDoc( `There must be no hyphen before @${targetTagName} description.`, { diff --git a/test/rules/assertions/requireHyphenBeforeParamDescription.js b/test/rules/assertions/requireHyphenBeforeParamDescription.js index 919b6b55..9b2ab23c 100644 --- a/test/rules/assertions/requireHyphenBeforeParamDescription.js +++ b/test/rules/assertions/requireHyphenBeforeParamDescription.js @@ -447,6 +447,32 @@ export default { */ `, }, + { + code: ` + /** + * @param {( + * | string + * | number + * )} input The input value + */ + function test(input) {} + `, + errors: [ + { + line: 6, + message: 'There must be a hyphen before @param description.', + }, + ], + output: ` + /** + * @param {( + * | string + * | number + * )} input - The input value + */ + function test(input) {} + `, + }, ], valid: [ {