Skip to content

Commit 8b5b7f7

Browse files
committed
fix(require-hyphen-before-param-description): inject hyphen at proper place with multiline type
1 parent 34866bc commit 8b5b7f7

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

docs/rules/require-hyphen-before-param-description.md

+9
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ function quux () {
193193
*/
194194
// "jsdoc/require-hyphen-before-param-description": ["error"|"warn", "always",{"tags":{"*":"never","property":"always"}}]
195195
// Message: There must be no hyphen before @returns description.
196+
197+
/**
198+
* @param {(
199+
* | string
200+
* | number
201+
* )} input The input value
202+
*/
203+
function test(input) {}
204+
// Message: There must be a hyphen before @param description.
196205
````
197206

198207

src/rules/requireHyphenBeforeParamDescription.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,39 @@ export default iterateJsdoc(({
3333
}
3434

3535
const startsWithHyphen = (/^\s*-/u).test(desc);
36-
if (always) {
37-
if (!startsWithHyphen) {
38-
report(`There must be a hyphen before @${targetTagName} description.`, (fixer) => {
39-
const lineIndex = /** @type {import('../iterateJsdoc.js').Integer} */ (
40-
jsdocTag.line
41-
);
42-
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
43-
44-
// Get start index of description, accounting for multi-line descriptions
45-
const description = desc.split('\n')[0];
46-
const descriptionIndex = sourceLines[lineIndex].lastIndexOf(description);
36+
let lines = 0;
37+
for (const {
38+
tokens,
39+
} of jsdocTag.source) {
40+
if (tokens.description) {
41+
break;
42+
}
4743

48-
const replacementLine = sourceLines[lineIndex]
49-
.slice(0, descriptionIndex) + '- ' + description;
50-
sourceLines.splice(lineIndex, 1, replacementLine);
51-
const replacement = sourceLines.join('\n');
44+
lines++;
45+
}
5246

53-
return fixer.replaceText(jsdocNode, replacement);
54-
}, jsdocTag);
47+
if (always) {
48+
if (!startsWithHyphen) {
49+
utils.reportJSDoc(
50+
`There must be a hyphen before @${targetTagName} description.`,
51+
{
52+
line: jsdocTag.source[0].number + lines,
53+
},
54+
() => {
55+
for (const {
56+
tokens,
57+
} of jsdocTag.source) {
58+
if (tokens.description) {
59+
tokens.description = tokens.description.replace(
60+
/^(\s*)/u, '$1- ',
61+
);
62+
break;
63+
}
64+
}
65+
}
66+
);
5567
}
5668
} else if (startsWithHyphen) {
57-
let lines = 0;
58-
for (const {
59-
tokens,
60-
} of jsdocTag.source) {
61-
if (tokens.description) {
62-
break;
63-
}
64-
65-
lines++;
66-
}
67-
6869
utils.reportJSDoc(
6970
`There must be no hyphen before @${targetTagName} description.`,
7071
{

test/rules/assertions/requireHyphenBeforeParamDescription.js

+26
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,32 @@ export default {
447447
*/
448448
`,
449449
},
450+
{
451+
code: `
452+
/**
453+
* @param {(
454+
* | string
455+
* | number
456+
* )} input The input value
457+
*/
458+
function test(input) {}
459+
`,
460+
errors: [
461+
{
462+
line: 6,
463+
message: 'There must be a hyphen before @param description.',
464+
},
465+
],
466+
output: `
467+
/**
468+
* @param {(
469+
* | string
470+
* | number
471+
* )} input - The input value
472+
*/
473+
function test(input) {}
474+
`,
475+
},
450476
],
451477
valid: [
452478
{

0 commit comments

Comments
 (0)