Skip to content

Commit 11cf9f5

Browse files
committed
feat(match-description): allow mainDescription: string|false to override or disable main description separate from default
1 parent 299b095 commit 11cf9f5

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

.README/rules/match-description.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,21 @@ tag should be linted with the `matchDescription` value (or the default).
5151
```
5252

5353
If you wish to override the main function description without changing the
54-
default `mainDescription`, you may use `tags` with `main description`:
54+
default `match-description`, you may use `mainDescription`:
5555

5656
```js
5757
{
58-
'jsdoc/match-description': ['error', {tags: {
59-
'main description': '[A-Z].*\\.',
60-
param: true,
61-
returns: true
62-
}}]
58+
'jsdoc/match-description': ['error', {
59+
mainDescription: '[A-Z].*\\.',
60+
tags: {
61+
param: true,
62+
returns: true
63+
}
64+
}]
6365
}
6466
```
6567

66-
There is no need to add `"main description": true`, as by default, the main
68+
There is no need to add `mainDescription: true`, as by default, the main
6769
function (and only the main function) is linted, though you may disable checking
6870
it by setting it to `false`.
6971

src/rules/matchDescription.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import iterateJsdoc from '../iterateJsdoc';
33

44
const tagsWithDescriptions = ['param', 'arg', 'argument', 'returns', 'return'];
55

6+
// If supporting Node >= 10, we could loosen the default to this for the
7+
// initial letter: \\p{Upper}
8+
const matchDescriptionDefault = '^[A-Z`\\d_][\\s\\S]*[.?!`]$';
9+
10+
const stringOrDefault = (value, userDefault) => {
11+
return typeof value === 'string' ?
12+
value :
13+
userDefault || matchDescriptionDefault;
14+
};
15+
616
export default iterateJsdoc(({
717
jsdoc,
818
report,
@@ -12,32 +22,31 @@ export default iterateJsdoc(({
1222
const options = context.options[0] || {};
1323

1424
const validateDescription = (description, tag) => {
15-
const tagName = tag.tag;
16-
const tagOptions = options.tags || {};
17-
if (tagOptions[tagName] === false) {
25+
if (!tag && options.mainDescription === false) {
1826
return;
1927
}
20-
const regex = new RegExp(
21-
(typeof tagOptions[tagName] === 'string' ? tagOptions[tagName] :
22-
options.matchDescription
2328

24-
// If supporting Node >= 10, we could loosen to this for the
25-
// initial letter: \\p{Upper}
26-
) || '^[A-Z`\\d_][\\s\\S]*[.?!`]$',
29+
let tagValue = options.mainDescription;
30+
if (tag) {
31+
const tagName = tag.tag;
32+
tagValue = options.tags[tagName];
33+
}
34+
35+
const regex = new RegExp(
36+
stringOrDefault(tagValue, options.matchDescription),
2737
'u'
2838
);
2939

3040
if (!regex.test(description)) {
31-
report('JSDoc description does not satisfy the regex pattern.', null, tag);
41+
report('JSDoc description does not satisfy the regex pattern.', null, tag || {
42+
// Add one as description would typically be into block
43+
line: jsdoc.line + 1
44+
});
3245
}
3346
};
3447

3548
if (jsdoc.description) {
36-
validateDescription(jsdoc.description, {
37-
// Add one as description would typically be into block
38-
line: jsdoc.line + 1,
39-
tag: 'main description'
40-
});
49+
validateDescription(jsdoc.description);
4150
}
4251

4352
if (!options.tags || !Object.keys(options.tags).length) {
@@ -74,6 +83,17 @@ export default iterateJsdoc(({
7483
}
7584
]
7685
},
86+
mainDescription: {
87+
oneOf: [
88+
{
89+
format: 'regex',
90+
type: 'string'
91+
},
92+
{
93+
type: 'boolean'
94+
}
95+
]
96+
},
7797
matchDescription: {
7898
format: 'regex',
7999
type: 'string'
@@ -91,6 +111,7 @@ export default iterateJsdoc(({
91111
type: 'string'
92112
},
93113
{
114+
enum: [true],
94115
type: 'boolean'
95116
}
96117
]

test/rules/assertions/matchDescription.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ export default {
5151
matchDescription: '[\u0410-\u042F][\u0410-\u044F]+\\.'
5252
}]
5353
},
54+
{
55+
code: `
56+
/**
57+
* Abc.
58+
*/
59+
function quux () {
60+
61+
}
62+
`,
63+
errors: [
64+
{
65+
line: 3,
66+
message: 'JSDoc description does not satisfy the regex pattern.'
67+
}
68+
],
69+
options: [{
70+
mainDescription: '[\u0410-\u042F][\u0410-\u044F]+\\.',
71+
tags: {
72+
param: true
73+
}
74+
}]
75+
},
5476
{
5577
code: `
5678
/**
@@ -111,8 +133,8 @@ export default {
111133
],
112134
options: [
113135
{
136+
mainDescription: '^[a-zA-Z]*$',
114137
tags: {
115-
'main description': '^[a-zA-Z]*$',
116138
param: true
117139
}
118140
}
@@ -137,8 +159,8 @@ export default {
137159
],
138160
options: [
139161
{
162+
mainDescription: false,
140163
tags: {
141-
'main description': false,
142164
param: true
143165
}
144166
}
@@ -669,9 +691,7 @@ export default {
669691
}
670692
`,
671693
options: [
672-
{tags: {
673-
'main description': false
674-
}}
694+
{mainDescription: false}
675695
]
676696
},
677697
{
@@ -694,9 +714,7 @@ export default {
694714
}
695715
`,
696716
options: [
697-
{tags: {
698-
'main description': true
699-
}}
717+
{mainDescription: true}
700718
]
701719
},
702720
{

0 commit comments

Comments
 (0)