Skip to content

Commit

Permalink
fix(require-hyphen-before-param-description): inject hyphen at prop…
Browse files Browse the repository at this point in the history
…er place with multiline type
  • Loading branch information
brettz9 committed Oct 11, 2024
1 parent 34866bc commit 8b5b7f7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
9 changes: 9 additions & 0 deletions docs/rules/require-hyphen-before-param-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
````


Expand Down
57 changes: 29 additions & 28 deletions src/rules/requireHyphenBeforeParamDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
{
Expand Down
26 changes: 26 additions & 0 deletions test/rules/assertions/requireHyphenBeforeParamDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down

0 comments on commit 8b5b7f7

Please sign in to comment.