Skip to content

Commit a4d83cd

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

File tree

4 files changed

+123
-21
lines changed

4 files changed

+123
-21
lines changed

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

Lines changed: 5 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,10 @@ 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, they will actually be forbidden
20+
in the event that they are at the end of a line (this will otherwise cause
21+
Visual Studio Code to display incorrectly).
22+
1923
The options object may have the following properties to indicate behavior for
2024
other tags besides the `@param` tag (or the `@arg` tag if so set):
2125

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

Lines changed: 32 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,10 @@ 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, they will actually be forbidden
31+
in the event that they are at the end of a line (this will otherwise cause
32+
Visual Studio Code to display incorrectly).
33+
3034
The options object may have the following properties to indicate behavior for
3135
other tags besides the `@param` tag (or the `@arg` tag if so set):
3236

@@ -202,6 +206,14 @@ function quux () {
202206
*/
203207
function test(input) {}
204208
// Message: There must be a hyphen before @param description.
209+
210+
/**
211+
* @param foo -
212+
* The possible values for `foo` are as follows.
213+
* - `"option1"`: Description of option 1.
214+
* - `"option2"`: Description of option 2.
215+
*/
216+
// Message: There must be no hyphen before @param description.
205217
````
206218

207219

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

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 34 additions & 19 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,26 +43,35 @@ 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(
@@ -76,6 +87,10 @@ export default iterateJsdoc(({
7687
tokens.description = tokens.description.replace(
7788
/^\s*-\s*/v, '',
7889
);
90+
if (hyphenNewline) {
91+
tokens.postName = '';
92+
}
93+
7994
break;
8095
}
8196
}

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 before @param description.',
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)