-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
requireReturnsCheck.js
executable file
·57 lines (45 loc) · 1.33 KB
/
requireReturnsCheck.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
import iterateJsdoc from '../iterateJsdoc';
export default iterateJsdoc(({
jsdoc,
report,
functionNode,
utils
}) => {
// Implicit return like `() => foo` is ok
if (functionNode.type === 'ArrowFunctionExpression' && functionNode.expression) {
return;
}
// Async function always returns a promise
if (functionNode.async) {
return;
}
const targetTagName = utils.getPreferredTagName('returns');
// We can skip in case there are no tags defined...
if (typeof jsdoc.tags === 'undefined') {
return;
}
const jsdocTags = jsdoc.tags.filter((item) => {
return item.tag === targetTagName;
});
if (jsdocTags.length === 0) {
return;
}
if (jsdocTags.length > 1) {
report('Found more than one @' + targetTagName + ' declaration.');
return;
}
// An abstract function is by definition incomplete
// so it is perfectly fine if the return is missing
// a subclass may inherits the doc an implements the
// missing return.
const isAbstract = jsdoc.tags.some((item) => {
return item.tag === utils.getPreferredTagName('abstract');
});
if (isAbstract) {
return;
}
const sourcecode = utils.getFunctionSourceCode();
if (sourcecode.indexOf('return') === -1) {
report('Present JSDoc @' + targetTagName + ' declaration but not available return expression in function.');
}
});