Skip to content

Commit 1383c02

Browse files
committed
feat(require-hyphen-before-param-description): add option to support checking property/prop
1 parent 18f7470 commit 1383c02

File tree

4 files changed

+139
-17
lines changed

4 files changed

+139
-17
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@ Requires a hyphen before the `@param` description.
44

55
#### Options
66

7-
This rule takes one optional string 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"`.
7+
This rule takes one optional string argument and an optional options object.
8+
9+
If the string is `"always"` then a problem is raised when there is no hyphen
10+
before the description. If it is `"never"` then a problem is raised when there
11+
is a hyphen before the description. The default value is `"always"`.
12+
13+
The options object may have the following properties:
14+
15+
- `checkProperties` - Boolean on whether to also apply the rule to `@property`
16+
tags.
817

918
|||
1019
|---|---|
1120
|Context|everywhere|
12-
|Tags|`param`|
13-
|Aliases|`arg`, `argument`|
14-
|Options|(a string matching `"always"|"never"`)|
21+
|Tags|`param` and optionally `property`|
22+
|Aliases|`arg`, `argument`; optionally `prop`|
23+
|Options|(a string matching `"always"|"never"`) and an optional object with a `checkProperties` property|
1524

1625
<!-- assertions requireHyphenBeforeParamDescription -->

README.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7181,14 +7181,23 @@ Requires a hyphen before the `@param` description.
71817181
<a name="eslint-plugin-jsdoc-rules-require-hyphen-before-param-description-options-18"></a>
71827182
#### Options
71837183
7184-
This rule takes one optional string 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"`.
7184+
This rule takes one optional string argument and an optional options object.
7185+
7186+
If the string is `"always"` then a problem is raised when there is no hyphen
7187+
before the description. If it is `"never"` then a problem is raised when there
7188+
is a hyphen before the description. The default value is `"always"`.
7189+
7190+
The options object may have the following properties:
7191+
7192+
- `checkProperties` - Boolean on whether to also apply the rule to `@property`
7193+
tags.
71857194
71867195
|||
71877196
|---|---|
71887197
|Context|everywhere|
7189-
|Tags|`param`|
7190-
|Aliases|`arg`, `argument`|
7191-
|Options|(a string matching `"always"|"never"`)|
7198+
|Tags|`param` and optionally `property`|
7199+
|Aliases|`arg`, `argument`; optionally `prop`|
7200+
|Options|(a string matching `"always"|"never"`) and an optional object with a `checkProperties` property|
71927201
71937202
The following patterns are considered problems:
71947203
@@ -7248,6 +7257,20 @@ function quux (foo) {
72487257
}
72497258
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
72507259
// Message: Unexpected tag `@param`
7260+
7261+
/**
7262+
* @typedef {SomeType} ATypeDefName
7263+
* @property foo Foo.
7264+
*/
7265+
// Options: ["always",{"checkProperties":true}]
7266+
// Message: There must be a hyphen before @property description.
7267+
7268+
/**
7269+
* @typedef {SomeType} ATypeDefName
7270+
* @property foo - Foo.
7271+
*/
7272+
// Options: ["never",{"checkProperties":true}]
7273+
// Message: There must be no hyphen before @property description.
72517274
````
72527275
72537276
The following patterns are not considered problems:
@@ -7275,6 +7298,18 @@ function quux () {
72757298
function quux () {
72767299
72777300
}
7301+
7302+
/**
7303+
* @typedef {SomeType} ATypeDefName
7304+
* @property foo - Foo.
7305+
*/
7306+
// Options: ["always",{"checkProperties":true}]
7307+
7308+
/**
7309+
* @typedef {SomeType} ATypeDefName
7310+
* @property foo Foo.
7311+
*/
7312+
// Options: ["never",{"checkProperties":true}]
72787313
````
72797314
72807315

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ from 'lodash';
21
import iterateJsdoc from '../iterateJsdoc';
32

43
export default iterateJsdoc(({
@@ -8,14 +7,10 @@ export default iterateJsdoc(({
87
context,
98
jsdocNode,
109
}) => {
11-
let always;
12-
if (_.has(context.options, 0)) {
13-
always = context.options[0] === 'always';
14-
} else {
15-
always = true;
16-
}
10+
const [circumstance, {checkProperties} = {}] = context.options;
11+
const always = !circumstance || circumstance === 'always';
1712

18-
utils.forEachPreferredTag('param', (jsdocTag, targetTagName) => {
13+
const checkHyphens = (jsdocTag, targetTagName) => {
1914
if (!jsdocTag.description) {
2015
return;
2116
}
@@ -49,7 +44,12 @@ export default iterateJsdoc(({
4944
return fixer.replaceText(jsdocNode, replacement);
5045
}, jsdocTag);
5146
}
52-
});
47+
};
48+
49+
utils.forEachPreferredTag('param', checkHyphens);
50+
if (checkProperties) {
51+
utils.forEachPreferredTag('property', checkHyphens);
52+
}
5353
}, {
5454
iterateAllJsdocs: true,
5555
meta: {
@@ -59,6 +59,16 @@ export default iterateJsdoc(({
5959
enum: ['always', 'never'],
6060
type: 'string',
6161
},
62+
{
63+
additionalProperties: false,
64+
properties: {
65+
checkProperties: {
66+
default: false,
67+
type: 'boolean',
68+
},
69+
},
70+
type: 'object',
71+
},
6272
],
6373
type: 'layout',
6474
},

test/rules/assertions/requireHyphenBeforeParamDescription.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,52 @@ export default {
160160
},
161161
},
162162
},
163+
{
164+
code: `
165+
/**
166+
* @typedef {SomeType} ATypeDefName
167+
* @property foo Foo.
168+
*/
169+
`,
170+
errors: [
171+
{
172+
line: 4,
173+
message: 'There must be a hyphen before @property description.',
174+
},
175+
],
176+
options: [
177+
'always', {checkProperties: true},
178+
],
179+
output: `
180+
/**
181+
* @typedef {SomeType} ATypeDefName
182+
* @property foo - Foo.
183+
*/
184+
`,
185+
},
186+
{
187+
code: `
188+
/**
189+
* @typedef {SomeType} ATypeDefName
190+
* @property foo - Foo.
191+
*/
192+
`,
193+
errors: [
194+
{
195+
line: 4,
196+
message: 'There must be no hyphen before @property description.',
197+
},
198+
],
199+
options: [
200+
'never', {checkProperties: true},
201+
],
202+
output: `
203+
/**
204+
* @typedef {SomeType} ATypeDefName
205+
* @property foo Foo.
206+
*/
207+
`,
208+
},
163209
],
164210
valid: [
165211
{
@@ -198,5 +244,27 @@ export default {
198244
}
199245
`,
200246
},
247+
{
248+
code: `
249+
/**
250+
* @typedef {SomeType} ATypeDefName
251+
* @property foo - Foo.
252+
*/
253+
`,
254+
options: [
255+
'always', {checkProperties: true},
256+
],
257+
},
258+
{
259+
code: `
260+
/**
261+
* @typedef {SomeType} ATypeDefName
262+
* @property foo Foo.
263+
*/
264+
`,
265+
options: [
266+
'never', {checkProperties: true},
267+
],
268+
},
201269
],
202270
};

0 commit comments

Comments
 (0)