Skip to content

Commit 75b4b6f

Browse files
authored
Merge pull request #225 from brettz9/matchDescription-107
- Add matchDescription for #107
2 parents 80eddcf + 133a9cf commit 75b4b6f

File tree

4 files changed

+515
-0
lines changed

4 files changed

+515
-0
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import checkParamNames from './rules/checkParamNames';
66
import checkSyntax from './rules/checkSyntax';
77
import checkTagNames from './rules/checkTagNames';
88
import checkTypes from './rules/checkTypes';
9+
import matchDescription from './rules/matchDescription';
910
import newlineAfterDescription from './rules/newlineAfterDescription';
1011
import noUndefinedTypes from './rules/noUndefinedTypes';
1112
import requireDescriptionCompleteSentence from './rules/requireDescriptionCompleteSentence';
@@ -61,6 +62,7 @@ export default {
6162
'check-syntax': checkSyntax,
6263
'check-tag-names': checkTagNames,
6364
'check-types': checkTypes,
65+
'match-description': matchDescription,
6466
'newline-after-description': newlineAfterDescription,
6567
'no-undefined-types': noUndefinedTypes,
6668
'require-description': requireDescription,

src/rules/matchDescription.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import _ from 'lodash';
2+
import iterateJsdoc from '../iterateJsdoc';
3+
4+
const tagsWithDescriptions = ['param', 'arg', 'argument', 'returns', 'return'];
5+
6+
export default iterateJsdoc(({
7+
jsdoc,
8+
report,
9+
context
10+
}) => {
11+
const options = context.options[0] || {};
12+
13+
const validateDescription = (description, tag) => {
14+
const regex = new RegExp(
15+
(tag && typeof options.tags[tag] === 'string' ? options.tags[tag] :
16+
options.matchDescription,
17+
18+
// If supporting Node >= 10, we could loosen to this for the
19+
// initial letter: \\p{Upper}
20+
options.matchDescription) || '^([A-Z]|[`\\d_])([\\s\\S]*[.?!`])?$',
21+
'u'
22+
);
23+
24+
if (!regex.test(description)) {
25+
report('JSDoc description does not satisfy the regex pattern.');
26+
27+
return true;
28+
}
29+
30+
return false;
31+
};
32+
33+
if (jsdoc.description && validateDescription(jsdoc.description)) {
34+
return;
35+
}
36+
37+
if (!options.tags || !Object.keys(options.tags).length) {
38+
return;
39+
}
40+
41+
const tags = _.filter(jsdoc.tags, (tag) => {
42+
return _.includes(tagsWithDescriptions, tag.tag) &&
43+
{}.hasOwnProperty.call(options.tags, tag.tag) && options.tags[tag.tag];
44+
});
45+
46+
_.some(tags, (tag) => {
47+
const description = _.trimStart(tag.description, '- ');
48+
49+
return validateDescription(description, tag.tag);
50+
});
51+
});

0 commit comments

Comments
 (0)