Skip to content

Commit 875eb3e

Browse files
committed
feat(require-hyphen-before-param-description): when always is set, disallow hyphen at end of line; fixes #1435
1 parent 8132392 commit 875eb3e

File tree

4 files changed

+128
-22
lines changed

4 files changed

+128
-22
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Requires (or disallows) a hyphen before the `@param` description.
66

77
## Fixer
88

9-
(Todo)
9+
Adds a hyphen for "always" and removes a hyphen for "never".
1010

1111
## Options
1212

@@ -16,6 +16,11 @@ If the string is `"always"` then a problem is raised when there is no hyphen
1616
before the description. If it is `"never"` then a problem is raised when there
1717
is a hyphen before the description. The default value is `"always"`.
1818

19+
Even if hyphens are set to "always" appear after the tag name, they will
20+
actually be forbidden in the event that they are followed immediately by
21+
the end of a line (this will otherwise cause Visual Studio Code to display
22+
incorrectly).
23+
1924
The options object may have the following properties to indicate behavior for
2025
other tags besides the `@param` tag (or the `@arg` tag if so set):
2126

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Requires (or disallows) a hyphen before the `@param` description.
1515
<a name="require-hyphen-before-param-description-fixer"></a>
1616
## Fixer
1717

18-
(Todo)
18+
Adds a hyphen for "always" and removes a hyphen for "never".
1919

2020
<a name="user-content-require-hyphen-before-param-description-options"></a>
2121
<a name="require-hyphen-before-param-description-options"></a>
@@ -27,6 +27,11 @@ If the string is `"always"` then a problem is raised when there is no hyphen
2727
before the description. If it is `"never"` then a problem is raised when there
2828
is a hyphen before the description. The default value is `"always"`.
2929

30+
Even if hyphens are set to "always" appear after the tag name, they will
31+
actually be forbidden in the event that they are followed immediately by
32+
the end of a line (this will otherwise cause Visual Studio Code to display
33+
incorrectly).
34+
3035
The options object may have the following properties to indicate behavior for
3136
other tags besides the `@param` tag (or the `@arg` tag if so set):
3237

@@ -202,6 +207,14 @@ function quux () {
202207
*/
203208
function test(input) {}
204209
// Message: There must be a hyphen before @param description.
210+
211+
/**
212+
* @param foo -
213+
* The possible values for `foo` are as follows.
214+
* - `"option1"`: Description of option 1.
215+
* - `"option2"`: Description of option 2.
216+
*/
217+
// Message: There must be no hyphen followed by newline after the @param name.
205218
````
206219

207220

@@ -294,5 +307,24 @@ function main(argv) {
294307
* @typedef {any} Test
295308
*/
296309
// "jsdoc/require-hyphen-before-param-description": ["error"|"warn", "always",{"tags":{"template":"always"}}]
310+
311+
/**
312+
* @param foo - The possible values for `foo` are as follows.
313+
* - `"option1"`: Description of option 1.
314+
* - `"option2"`: Description of option 2.
315+
*/
316+
317+
/**
318+
* @param foo
319+
* The possible values for `foo` are as follows.
320+
* - `"option1"`: Description of option 1.
321+
* - `"option2"`: Description of option 2.
322+
*/
323+
324+
/**
325+
* @param foo
326+
* - `"option1"`: Description of option 1.
327+
* - `"option2"`: Description of option 2.
328+
*/
297329
````
298330

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export default iterateJsdoc(({
3030
}
3131

3232
const startsWithHyphen = (/^\s*-/v).test(desc);
33+
const hyphenNewline = (/^\s*-\n/v).test(desc);
34+
3335
let lines = 0;
3436
for (const {
3537
tokens,
@@ -41,30 +43,41 @@ export default iterateJsdoc(({
4143
lines++;
4244
}
4345

44-
if (always) {
46+
if (always && !hyphenNewline) {
4547
if (!startsWithHyphen) {
46-
utils.reportJSDoc(
47-
`There must be a hyphen before @${targetTagName} description.`,
48-
{
49-
line: jsdocTag.source[0].number + lines,
50-
},
51-
() => {
52-
for (const {
53-
tokens,
54-
} of jsdocTag.source) {
55-
if (tokens.description) {
56-
tokens.description = tokens.description.replace(
57-
/^(\s*)/v, '$1- ',
58-
);
59-
break;
60-
}
61-
}
62-
},
63-
);
48+
let fixIt = true;
49+
for (const {
50+
tokens,
51+
} of jsdocTag.source) {
52+
if (tokens.description) {
53+
tokens.description = tokens.description.replace(
54+
/^(\s*)/v, '$1- ',
55+
);
56+
break;
57+
}
58+
59+
// Linebreak after name since has no description
60+
if (tokens.name) {
61+
fixIt = false;
62+
break;
63+
}
64+
}
65+
66+
if (fixIt) {
67+
utils.reportJSDoc(
68+
`There must be a hyphen before @${targetTagName} description.`,
69+
{
70+
line: jsdocTag.source[0].number + lines,
71+
},
72+
() => {},
73+
);
74+
}
6475
}
6576
} else if (startsWithHyphen) {
6677
utils.reportJSDoc(
67-
`There must be no hyphen before @${targetTagName} description.`,
78+
always ?
79+
`There must be no hyphen followed by newline after the @${targetTagName} name.` :
80+
`There must be no hyphen before @${targetTagName} description.`,
6881
{
6982
line: jsdocTag.source[0].number + lines,
7083
},
@@ -76,6 +89,10 @@ export default iterateJsdoc(({
7689
tokens.description = tokens.description.replace(
7790
/^\s*-\s*/v, '',
7891
);
92+
if (hyphenNewline) {
93+
tokens.postName = '';
94+
}
95+
7996
break;
8097
}
8198
}

test/rules/assertions/requireHyphenBeforeParamDescription.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,30 @@ export default /** @type {import('../index.js').TestCases} */ ({
496496
function test(name) {}
497497
`,
498498
},
499+
{
500+
code: `
501+
/**
502+
* @param foo -
503+
* The possible values for \`foo\` are as follows.
504+
* - \`"option1"\`: Description of option 1.
505+
* - \`"option2"\`: Description of option 2.
506+
*/
507+
`,
508+
errors: [
509+
{
510+
line: 3,
511+
message: 'There must be no hyphen followed by newline after the @param name.',
512+
},
513+
],
514+
output: `
515+
/**
516+
* @param foo
517+
* The possible values for \`foo\` are as follows.
518+
* - \`"option1"\`: Description of option 1.
519+
* - \`"option2"\`: Description of option 2.
520+
*/
521+
`,
522+
},
499523
],
500524
valid: [
501525
{
@@ -658,5 +682,33 @@ export default /** @type {import('../index.js').TestCases} */ ({
658682
},
659683
],
660684
},
685+
{
686+
code: `
687+
/**
688+
* @param foo - The possible values for \`foo\` are as follows.
689+
* - \`"option1"\`: Description of option 1.
690+
* - \`"option2"\`: Description of option 2.
691+
*/
692+
`,
693+
},
694+
{
695+
code: `
696+
/**
697+
* @param foo
698+
* The possible values for \`foo\` are as follows.
699+
* - \`"option1"\`: Description of option 1.
700+
* - \`"option2"\`: Description of option 2.
701+
*/
702+
`,
703+
},
704+
{
705+
code: `
706+
/**
707+
* @param foo
708+
* - \`"option1"\`: Description of option 1.
709+
* - \`"option2"\`: Description of option 2.
710+
*/
711+
`,
712+
},
661713
],
662714
});

0 commit comments

Comments
 (0)