Skip to content

Commit a764861

Browse files
committed
feat(require-hyphen-before-param-description): add tags option for setting specific tags (or any tags) to follow rules for or against hyphen descriptions; now allows property to be set differently than param; fixes gajus#553
BREAKING CHANGE: `{checkProperties: true}` should be replaced by: `{tags: {'property': 'always|never'}}`
1 parent 3c03616 commit a764861

File tree

4 files changed

+285
-28
lines changed

4 files changed

+285
-28
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ If the string is `"always"` then a problem is raised when there is no hyphen
1010
before the description. If it is `"never"` then a problem is raised when there
1111
is a hyphen before the description. The default value is `"always"`.
1212

13-
The options object may have the following properties:
13+
The options object may have the following properties to indicate behavior for
14+
other tags besides the `@param` tag (or the `@arg` tag if so set):
1415

15-
- `checkProperties` - Boolean on whether to also apply the rule to `@property`
16-
tags.
16+
- `tags` - Object whose keys indicate different tags to check for the
17+
presence or absence of hyphens; the key value should be "always" or "never",
18+
indicating how hyphens are to be applied, e.g., `{property: 'never'}`
19+
to ensure `@property` never uses hyphens. A key can also be set as `*`, e.g.,
20+
`'*': 'always'` to apply hyphen checking to any tag (besides the preferred
21+
`@param` tag which follows the main string option setting and besides any
22+
other `tags` entries).
1723

1824
|||
1925
|---|---|
2026
|Context|everywhere|
21-
|Tags|`param` and optionally `property`|
22-
|Aliases|`arg`, `argument`; optionally `prop`|
23-
|Options|(a string matching `"always"|"never"`) followed by an optional object with a `checkProperties` property|
27+
|Tags|`param` and optionally other tags within `tags`|
28+
|Aliases|`arg`, `argument`; potentially `prop` or other aliases|
29+
|Options|(a string matching `"always"|"never"`) followed by an optional object with a `tags` property|
2430

2531
<!-- assertions requireHyphenBeforeParamDescription -->

README.md

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7930,17 +7930,23 @@ If the string is `"always"` then a problem is raised when there is no hyphen
79307930
before the description. If it is `"never"` then a problem is raised when there
79317931
is a hyphen before the description. The default value is `"always"`.
79327932
7933-
The options object may have the following properties:
7933+
The options object may have the following properties to indicate behavior for
7934+
other tags besides the `@param` tag (or the `@arg` tag if so set):
79347935
7935-
- `checkProperties` - Boolean on whether to also apply the rule to `@property`
7936-
tags.
7936+
- `tags` - Object whose keys indicate different tags to check for the
7937+
presence or absence of hyphens; the key value should be "always" or "never",
7938+
indicating how hyphens are to be applied, e.g., `{property: 'never'}`
7939+
to ensure `@property` never uses hyphens. A key can also be set as `*`, e.g.,
7940+
`'*': 'always'` to apply hyphen checking to any tag (besides the preferred
7941+
`@param` tag which follows the main string option setting and besides any
7942+
other `tags` entries).
79377943
79387944
|||
79397945
|---|---|
79407946
|Context|everywhere|
7941-
|Tags|`param` and optionally `property`|
7942-
|Aliases|`arg`, `argument`; optionally `prop`|
7943-
|Options|(a string matching `"always"|"never"`) followed by an optional object with a `checkProperties` property|
7947+
|Tags|`param` and optionally other tags within `tags`|
7948+
|Aliases|`arg`, `argument`; potentially `prop` or other aliases|
7949+
|Options|(a string matching `"always"|"never"`) followed by an optional object with a `tags` property|
79447950
79457951
The following patterns are considered problems:
79467952
@@ -7959,6 +7965,25 @@ function quux () {
79597965
*/
79607966
function quux () {
79617967
7968+
}
7969+
// Options: ["always",{"tags":{"*":"never"}}]
7970+
// Message: There must be a hyphen before @param description.
7971+
7972+
/**
7973+
* @param foo Foo.
7974+
* @returns {SomeType} - Hyphen here.
7975+
*/
7976+
function quux () {
7977+
7978+
}
7979+
// Options: ["always",{"tags":{"*":"never","returns":"always"}}]
7980+
// Message: There must be a hyphen before @param description.
7981+
7982+
/**
7983+
* @param foo Foo.
7984+
*/
7985+
function quux () {
7986+
79627987
}
79637988
// Message: There must be a hyphen before @param description.
79647989
@@ -8005,15 +8030,25 @@ function quux (foo) {
80058030
* @typedef {SomeType} ATypeDefName
80068031
* @property foo Foo.
80078032
*/
8008-
// Options: ["always",{"checkProperties":true}]
8033+
// Options: ["always",{"tags":{"property":"always"}}]
80098034
// Message: There must be a hyphen before @property description.
80108035
80118036
/**
80128037
* @typedef {SomeType} ATypeDefName
80138038
* @property foo - Foo.
80148039
*/
8015-
// Options: ["never",{"checkProperties":true}]
8040+
// Options: ["never",{"tags":{"property":"never"}}]
80168041
// Message: There must be no hyphen before @property description.
8042+
8043+
/**
8044+
* @param foo Foo.
8045+
* @returns {SomeType} - A description.
8046+
*/
8047+
function quux () {
8048+
8049+
}
8050+
// Options: ["always",{"tags":{"returns":"never"}}]
8051+
// Message: There must be a hyphen before @param description.
80178052
````
80188053
80198054
The following patterns are not considered problems:
@@ -8027,6 +8062,15 @@ function quux () {
80278062
}
80288063
// Options: ["always"]
80298064
8065+
/**
8066+
* @param foo - Foo.
8067+
* @returns {SomeType} A description.
8068+
*/
8069+
function quux () {
8070+
8071+
}
8072+
// Options: ["always",{"tags":{"returns":"never"}}]
8073+
80308074
/**
80318075
* @param foo Foo.
80328076
*/
@@ -8042,17 +8086,31 @@ function quux () {
80428086
80438087
}
80448088
8089+
/**
8090+
*
8091+
*/
8092+
function quux () {
8093+
8094+
}
8095+
// Options: ["always",{"tags":{"*":"always"}}]
8096+
80458097
/**
80468098
* @typedef {SomeType} ATypeDefName
80478099
* @property foo - Foo.
80488100
*/
8049-
// Options: ["always",{"checkProperties":true}]
8101+
// Options: ["always",{"tags":{"property":"always"}}]
80508102
80518103
/**
80528104
* @typedef {SomeType} ATypeDefName
80538105
* @property foo Foo.
80548106
*/
8055-
// Options: ["never",{"checkProperties":true}]
8107+
// Options: ["never",{"tags":{"property":"never"}}]
8108+
8109+
/**
8110+
* @typedef {SomeType} ATypeDefName
8111+
* @property foo - Foo.
8112+
*/
8113+
// Options: ["never",{"tags":{"*":"always"}}]
80568114
````
80578115
80588116

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ export default iterateJsdoc(({
55
utils,
66
report,
77
context,
8+
jsdoc,
89
jsdocNode,
910
}) => {
10-
const [circumstance, {checkProperties} = {}] = context.options;
11-
const always = !circumstance || circumstance === 'always';
11+
const [mainCircumstance, {tags} = {}] = context.options;
1212

13-
const checkHyphens = (jsdocTag, targetTagName) => {
13+
const checkHyphens = (jsdocTag, targetTagName, circumstance = mainCircumstance) => {
14+
const always = !circumstance || circumstance === 'always';
1415
if (!jsdocTag.description) {
1516
return;
1617
}
@@ -47,8 +48,28 @@ export default iterateJsdoc(({
4748
};
4849

4950
utils.forEachPreferredTag('param', checkHyphens);
50-
if (checkProperties) {
51-
utils.forEachPreferredTag('property', checkHyphens);
51+
if (tags) {
52+
const tagEntries = Object.entries(tags);
53+
tagEntries.forEach(([tagName, circumstance]) => {
54+
if (tagName === '*') {
55+
const preferredParamTag = utils.getPreferredTagName({tagName: 'param'});
56+
(jsdoc.tags || []).forEach(({tag}) => {
57+
if (tag === preferredParamTag || tagEntries.some(([tagNme]) => {
58+
return tagNme !== '*' && tagNme === tag;
59+
})) {
60+
return;
61+
}
62+
utils.forEachPreferredTag(tag, (jsdocTag, targetTagName) => {
63+
checkHyphens(jsdocTag, targetTagName, circumstance);
64+
});
65+
});
66+
67+
return;
68+
}
69+
utils.forEachPreferredTag(tagName, (jsdocTag, targetTagName) => {
70+
checkHyphens(jsdocTag, targetTagName, circumstance);
71+
});
72+
});
5273
}
5374
}, {
5475
iterateAllJsdocs: true,
@@ -62,9 +83,22 @@ export default iterateJsdoc(({
6283
{
6384
additionalProperties: false,
6485
properties: {
65-
checkProperties: {
66-
default: false,
67-
type: 'boolean',
86+
tags: {
87+
anyOf: [
88+
{
89+
patternProperties: {
90+
'.*': {
91+
enum: ['always', 'never'],
92+
type: 'string',
93+
},
94+
},
95+
type: 'object',
96+
},
97+
{
98+
enum: ['any'],
99+
type: 'string',
100+
},
101+
],
68102
},
69103
},
70104
type: 'object',

0 commit comments

Comments
 (0)