Skip to content

Commit 00c13ce

Browse files
committed
Switch "main description" on tags to its own option as mainDescription
1 parent 91fda5c commit 00c13ce

File tree

4 files changed

+88
-41
lines changed

4 files changed

+88
-41
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
@@ -2333,19 +2333,21 @@ tag should be linted with the `matchDescription` value (or the default).
23332333
```
23342334

23352335
If you wish to override the main function description without changing the
2336-
default `mainDescription`, you may use `tags` with `main description`:
2336+
default `match-description`, you may use `mainDescription`:
23372337

23382338
```js
23392339
{
2340-
'jsdoc/match-description': ['error', {tags: {
2341-
'main description': '[A-Z].*\\.',
2342-
param: true,
2343-
returns: true
2344-
}}]
2340+
'jsdoc/match-description': ['error', {
2341+
mainDescription: '[A-Z].*\\.',
2342+
tags: {
2343+
param: true,
2344+
returns: true
2345+
}
2346+
}]
23452347
}
23462348
```
23472349

2348-
There is no need to add `"main description": true`, as by default, the main
2350+
There is no need to add `mainDescription: true`, as by default, the main
23492351
function (and only the main function) is linted, though you may disable checking
23502352
it by setting it to `false`.
23512353

@@ -2405,8 +2407,12 @@ function quux () {
24052407
function quux () {
24062408

24072409
}
2410+
<<<<<<< HEAD
24082411
// Options: [{"tags":{"main description":"[А-Я][А-я]+\\.","param":true}}]
24092412
>>>>>>> feat(match-description): allow `main description: string|boolean` to override or disable main description separate from default
2413+
=======
2414+
// Options: [{"mainDescription":"[А-Я][А-я]+\\.","tags":{"param":true}}]
2415+
>>>>>>> Switch "main description" on `tags` to its own option as `mainDescription`
24102416
// Message: JSDoc description does not satisfy the regex pattern.
24112417

24122418
/**
@@ -2436,7 +2442,7 @@ function quux (foo) {
24362442
function quux (foo) {
24372443

24382444
}
2439-
// Options: [{"tags":{"main description":"^[a-zA-Z]*$","param":true}}]
2445+
// Options: [{"mainDescription":"^[a-zA-Z]*$","tags":{"param":true}}]
24402446
// Message: JSDoc description does not satisfy the regex pattern.
24412447

24422448
/**
@@ -2447,7 +2453,7 @@ function quux (foo) {
24472453
function quux (foo) {
24482454

24492455
}
2450-
// Options: [{"tags":{"main description":false,"param":true}}]
2456+
// Options: [{"mainDescription":false,"tags":{"param":true}}]
24512457
// Message: JSDoc description does not satisfy the regex pattern.
24522458

24532459
/**
@@ -2725,7 +2731,7 @@ function quux () {
27252731
function quux () {
27262732

27272733
}
2728-
// Options: [{"tags":{"main description":false}}]
2734+
// Options: [{"mainDescription":false}]
27292735

27302736
/**
27312737
* foo.
@@ -2741,7 +2747,7 @@ class quux {
27412747
class quux {
27422748

27432749
}
2744-
// Options: [{"tags":{"main description":true}}]
2750+
// Options: [{"mainDescription":true}]
27452751

27462752
class MyClass {
27472753
/**

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)