Skip to content

Commit 3e4ae0f

Browse files
committed
feat: add always/never options to require-hyphen-before-param-description
1 parent d2dbfa1 commit 3e4ae0f

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Requires a hyphen before the `@param` description.
44

5+
This rule takes one argument. If it is `"always"` then a problem is raised when there is no hyphen before the description. If it is `"never"` then a problem is raised when there is a hyphen before the description. The default value is `"always"`.
6+
57
|||
68
|---|---|
79
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ function quux (foo) {
843843
}
844844
// Settings: {"jsdoc":{"additionalTagNames":{"customTags":["baz","bar"]}}}
845845

846-
/**
846+
/**
847847
* @abstract
848848
* @access
849849
* @alias
@@ -931,9 +931,9 @@ RegExp
931931
<a name="eslint-plugin-jsdoc-rules-check-types-why-not-capital-case-everything"></a>
932932
#### Why not capital case everything?
933933

934-
Why are `boolean`, `number` and `string` exempt from starting with a capital letter? Let's take `string` as an example. In Javascript, everything is an object. The string Object has prototypes for string functions such as `.toUpperCase()`.
934+
Why are `boolean`, `number` and `string` exempt from starting with a capital letter? Let's take `string` as an example. In Javascript, everything is an object. The string Object has prototypes for string functions such as `.toUpperCase()`.
935935

936-
Fortunately we don't have to write `new String()` everywhere in our code. Javascript will automatically wrap string primitives into string Objects when we're applying a string function to a string primitive. This way the memory footprint is a tiny little bit smaller, and the [GC](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)) has less work to do.
936+
Fortunately we don't have to write `new String()` everywhere in our code. Javascript will automatically wrap string primitives into string Objects when we're applying a string function to a string primitive. This way the memory footprint is a tiny little bit smaller, and the [GC](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)) has less work to do.
937937

938938
So in a sense, there two types of strings in Javascript; `{string}` literals, also called primitives and `{String}` Objects. We use the primitives because it's easier to write and uses less memory. `{String}` and `{string}` are technically both valid, but they are not the same.
939939

@@ -1617,6 +1617,8 @@ function quux () {
16171617

16181618
Requires a hyphen before the `@param` description.
16191619

1620+
This rule takes one argument. If it is `"always"` then a problem is raised when there is no hyphen before the description. If it is `"never"` then a problem is raised when there is a hyphen before the description. The default value is `"always"`.
1621+
16201622
|||
16211623
|---|---|
16221624
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
@@ -1631,7 +1633,17 @@ The following patterns are considered problems:
16311633
function quux () {
16321634

16331635
}
1636+
// Options: ["always"]
16341637
// Message: There must be a hyphen before @param description.
1638+
1639+
/**
1640+
* @param foo - Foo.
1641+
*/
1642+
function quux () {
1643+
1644+
}
1645+
// Options: ["never"]
1646+
// Message: There must be no hyphen before @param description.
16351647
````
16361648

16371649
The following patterns are not considered problems:
@@ -1643,6 +1655,15 @@ The following patterns are not considered problems:
16431655
function quux () {
16441656

16451657
}
1658+
// Options: ["always"]
1659+
1660+
/**
1661+
* @param foo Foo.
1662+
*/
1663+
function quux () {
1664+
1665+
}
1666+
// Options: ["never"]
16461667
````
16471668

16481669

@@ -2303,12 +2324,12 @@ function quux () {
23032324
*/
23042325
const quux = () => foo;
23052326

2306-
/**
2327+
/**
23072328
* @returns {Promise<void>}
23082329
*/
23092330
async function quux() {}
23102331

2311-
/**
2332+
/**
23122333
* @returns {Promise<void>}
23132334
*/
23142335
async () => {}

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,38 @@ export default iterateJsdoc(({
55
sourceCode,
66
jsdoc,
77
report,
8+
context,
89
jsdocNode
910
}) => {
11+
let always;
12+
1013
const jsdocTags = _.filter(jsdoc.tags, {
1114
tag: 'param'
1215
});
1316

17+
if (_.has(context.options, 0)) {
18+
always = context.options[0] === 'always';
19+
} else {
20+
always = true;
21+
}
22+
1423
_.forEach(jsdocTags, (jsdocTag) => {
15-
if (jsdocTag.description && !_.startsWith(jsdocTag.description, '-')) {
16-
report('There must be a hyphen before @param description.', (fixer) => {
17-
const replacement = sourceCode.getText(jsdocNode).replace(jsdocTag.description, '- ' + jsdocTag.description);
24+
if (!jsdocTag.description) {
25+
return;
26+
}
27+
28+
if (always) {
29+
if (!_.startsWith(jsdocTag.description, '-')) {
30+
report('There must be a hyphen before @param description.', (fixer) => {
31+
const replacement = sourceCode.getText(jsdocNode).replace(jsdocTag.description, '- ' + jsdocTag.description);
32+
33+
return fixer.replaceText(jsdocNode, replacement);
34+
}, jsdocTag);
35+
}
36+
} else if (_.startsWith(jsdocTag.description, '-')) {
37+
report('There must be no hyphen before @param description.', (fixer) => {
38+
const reg = new RegExp(/(?<=-\s*)\w.*/);
39+
const replacement = sourceCode.getText(jsdocNode).replace(jsdocTag.description, jsdocTag.description.match(reg));
1840

1941
return fixer.replaceText(jsdocNode, replacement);
2042
}, jsdocTag);

test/rules/assertions/requireHyphenBeforeParamDescription.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,42 @@ export default {
1515
message: 'There must be a hyphen before @param description.'
1616
}
1717
],
18+
options: [
19+
'always'
20+
],
1821
output: `
1922
/**
2023
* @param foo - Foo.
2124
*/
2225
function quux () {
2326
27+
}
28+
`
29+
},
30+
{
31+
code: `
32+
/**
33+
* @param foo - Foo.
34+
*/
35+
function quux () {
36+
37+
}
38+
`,
39+
errors: [
40+
{
41+
line: 3,
42+
message: 'There must be no hyphen before @param description.'
43+
}
44+
],
45+
options: [
46+
'never'
47+
],
48+
output: `
49+
/**
50+
* @param foo Foo.
51+
*/
52+
function quux () {
53+
2454
}
2555
`
2656
}
@@ -34,7 +64,23 @@ export default {
3464
function quux () {
3565
3666
}
37-
`
67+
`,
68+
options: [
69+
'always'
70+
]
71+
},
72+
{
73+
code: `
74+
/**
75+
* @param foo Foo.
76+
*/
77+
function quux () {
78+
79+
}
80+
`,
81+
options: [
82+
'never'
83+
]
3884
}
3985
]
4086
};

0 commit comments

Comments
 (0)