-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
requireReturns.js
51 lines (38 loc) · 1.35 KB
/
requireReturns.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
import _ from 'lodash';
import iterateJsdoc from '../iterateJsdoc';
export default iterateJsdoc(({
jsdoc,
report,
utils
}) => {
// inheritdoc implies that all documentation is inherited
// see http://usejsdoc.org/tags-inheritdoc.html
//
// As we do not know the parent method, we cannot perform any checks.
if (utils.hasTag('inheritdoc')) {
return;
}
const targetTagName = utils.getPreferredTagName('returns');
const jsdocTags = _.filter(jsdoc.tags, {
tag: targetTagName
});
const sourcecode = utils.getFunctionSourceCode();
// build a one-liner to test against
const flattenedSource = sourcecode.replace(/\r?\n|\r|\s/g, '');
const startsWithReturn = '(\\)\\s?\\{return)';
const endsWithReturn = '(return.*\\})';
const implicitReturn = '(\\s?=>\\s?\\b.*)';
const implicitObjectReturn = '(\\s?=>\\s?\\(\\{)';
const matcher = new RegExp([
startsWithReturn,
endsWithReturn,
implicitObjectReturn,
implicitReturn
].join('|'), 'gim');
const positiveTest = (flattenedSource.match(matcher) || []).length > 0;
const negativeTest = (flattenedSource.match(/(\{.*\{.*return)/gim) || []).length > 0 &&
(flattenedSource.match(/(return)/gim) || []).length < 2;
if (JSON.stringify(jsdocTags) === '[]' && positiveTest && !negativeTest) {
report('Missing JSDoc @' + targetTagName + ' declaration.');
}
});