-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
matchDescription.js
132 lines (116 loc) · 3.14 KB
/
matchDescription.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import _ from 'lodash';
import iterateJsdoc from '../iterateJsdoc';
// If supporting Node >= 10, we could loosen the default to this for the
// initial letter: \\p{Upper}
const matchDescriptionDefault = '^[A-Z`\\d_][\\s\\S]*[.?!`]$';
const stringOrDefault = (value, userDefault) => {
return typeof value === 'string' ?
value :
userDefault || matchDescriptionDefault;
};
export default iterateJsdoc(({
jsdoc,
report,
context,
utils
}) => {
const options = context.options[0] || {};
const validateDescription = (description, tag) => {
if (!tag && options.mainDescription === false) {
return;
}
let tagValue = options.mainDescription;
if (tag) {
const tagName = tag.tag;
tagValue = options.tags[tagName];
}
const regex = new RegExp(
stringOrDefault(tagValue, options.matchDescription),
'u'
);
if (!regex.test(description)) {
report('JSDoc description does not satisfy the regex pattern.', null, tag || {
// Add one as description would typically be into block
line: jsdoc.line + 1
});
}
};
if (jsdoc.description) {
validateDescription(jsdoc.description);
}
if (!options.tags || !Object.keys(options.tags).length) {
return;
}
const hasOptionTag = (tagName) => {
return Boolean(options.tags[tagName]);
};
utils.forEachPreferredTag('description', (matchingJsdocTag, targetTagName) => {
const description = (matchingJsdocTag.name + ' ' + matchingJsdocTag.description).trim();
if (hasOptionTag(targetTagName)) {
validateDescription(description, matchingJsdocTag);
}
}, true);
const whitelistedTags = utils.filterTags(({tag: tagName}) => {
return hasOptionTag(tagName);
});
const {tagsWithNames, tagsWithoutNames} = utils.getTagsByType(whitelistedTags);
tagsWithNames.some((tag) => {
const description = _.trimStart(tag.description, '- ');
return validateDescription(description, tag);
});
tagsWithoutNames.some((tag) => {
const description = (tag.name + ' ' + tag.description).trim();
return validateDescription(description, tag);
});
}, {
contextDefaults: true,
meta: {
schema: [
{
additionalProperties: false,
properties: {
contexts: {
items: {
type: 'string'
},
type: 'array'
},
mainDescription: {
oneOf: [
{
format: 'regex',
type: 'string'
},
{
type: 'boolean'
}
]
},
matchDescription: {
format: 'regex',
type: 'string'
},
tags: {
patternProperties: {
'.*': {
oneOf: [
{
format: 'regex',
type: 'string'
},
{
enum: [true],
type: 'boolean'
}
]
}
},
type: 'object'
}
},
type: 'object'
}
],
type: 'suggestion'
}
});