Skip to content

Commit 7c47afa

Browse files
brettz9golopot
authored andcommitted
Switch "main description" on tags to its own option as mainDescription
1 parent da3d139 commit 7c47afa

File tree

4 files changed

+67
-42
lines changed

4 files changed

+67
-42
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

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,19 +2300,21 @@ tag should be linted with the `matchDescription` value (or the default).
23002300
```
23012301

23022302
If you wish to override the main function description without changing the
2303-
default `mainDescription`, you may use `tags` with `main description`:
2303+
default `match-description`, you may use `mainDescription`:
23042304

23052305
```js
23062306
{
2307-
'jsdoc/match-description': ['error', {tags: {
2308-
'main description': '[A-Z].*\\.',
2309-
param: true,
2310-
returns: true
2311-
}}]
2307+
'jsdoc/match-description': ['error', {
2308+
mainDescription: '[A-Z].*\\.',
2309+
tags: {
2310+
param: true,
2311+
returns: true
2312+
}
2313+
}]
23122314
}
23132315
```
23142316

2315-
There is no need to add `"main description": true`, as by default, the main
2317+
There is no need to add `mainDescription: true`, as by default, the main
23162318
function (and only the main function) is linted, though you may disable checking
23172319
it by setting it to `false`.
23182320

@@ -2372,8 +2374,12 @@ function quux () {
23722374
function quux () {
23732375

23742376
}
2377+
<<<<<<< HEAD
23752378
// Options: [{"tags":{"main description":"[А-Я][А-я]+\\.","param":true}}]
23762379
>>>>>>> feat(match-description): allow `main description: string|boolean` to override or disable main description separate from default
2380+
=======
2381+
// Options: [{"mainDescription":"[А-Я][А-я]+\\.","tags":{"param":true}}]
2382+
>>>>>>> Switch "main description" on `tags` to its own option as `mainDescription`
23772383
// Message: JSDoc description does not satisfy the regex pattern.
23782384

23792385
/**
@@ -2403,7 +2409,7 @@ function quux (foo) {
24032409
function quux (foo) {
24042410

24052411
}
2406-
// Options: [{"tags":{"main description":"^[a-zA-Z]*$","param":true}}]
2412+
// Options: [{"mainDescription":"^[a-zA-Z]*$","tags":{"param":true}}]
24072413
// Message: JSDoc description does not satisfy the regex pattern.
24082414

24092415
/**
@@ -2414,7 +2420,7 @@ function quux (foo) {
24142420
function quux (foo) {
24152421

24162422
}
2417-
// Options: [{"tags":{"main description":false,"param":true}}]
2423+
// Options: [{"mainDescription":false,"tags":{"param":true}}]
24182424
// Message: JSDoc description does not satisfy the regex pattern.
24192425

24202426
/**
@@ -2692,7 +2698,7 @@ function quux () {
26922698
function quux () {
26932699

26942700
}
2695-
// Options: [{"tags":{"main description":false}}]
2701+
// Options: [{"mainDescription":false}]
26962702

26972703
/**
26982704
* foo.
@@ -2708,7 +2714,7 @@ class quux {
27082714
class quux {
27092715

27102716
}
2711-
// Options: [{"tags":{"main description":true}}]
2717+
// Options: [{"mainDescription":true}]
27122718

27132719
class MyClass {
27142720
/**

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: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export default {
6767
}
6868
],
6969
options: [{
70+
mainDescription: '[\u0410-\u042F][\u0410-\u044F]+\\.',
7071
tags: {
71-
'main description': '[\u0410-\u042F][\u0410-\u044F]+\\.',
7272
param: true
7373
}
7474
}]
@@ -133,8 +133,8 @@ export default {
133133
],
134134
options: [
135135
{
136+
mainDescription: '^[a-zA-Z]*$',
136137
tags: {
137-
'main description': '^[a-zA-Z]*$',
138138
param: true
139139
}
140140
}
@@ -159,8 +159,8 @@ export default {
159159
],
160160
options: [
161161
{
162+
mainDescription: false,
162163
tags: {
163-
'main description': false,
164164
param: true
165165
}
166166
}
@@ -691,9 +691,7 @@ export default {
691691
}
692692
`,
693693
options: [
694-
{tags: {
695-
'main description': false
696-
}}
694+
{mainDescription: false}
697695
]
698696
},
699697
{
@@ -722,9 +720,7 @@ export default {
722720
}
723721
`,
724722
options: [
725-
{tags: {
726-
'main description': true
727-
}}
723+
{mainDescription: true}
728724
]
729725
},
730726
{

0 commit comments

Comments
 (0)