1
- import _ from 'lodash' ;
2
1
import iterateJsdoc from '../iterateJsdoc' ;
3
2
4
3
export default iterateJsdoc ( ( {
@@ -7,29 +6,52 @@ export default iterateJsdoc(({
7
6
functionNode,
8
7
utils
9
8
} ) => {
9
+ // Implicit return like `() => foo` is ok
10
+ if ( functionNode . type === 'ArrowFunctionExpression' && functionNode . expression ) {
11
+ return ;
12
+ }
13
+
14
+ // Async function always returns a promise
15
+ if ( functionNode . async ) {
16
+ return ;
17
+ }
18
+
10
19
const targetTagName = utils . getPreferredTagName ( 'returns' ) ;
11
20
12
- const jsdocTags = _ . filter ( jsdoc . tags , {
13
- tag : targetTagName
21
+ // We can skip in case there are no tags defined...
22
+ if ( typeof jsdoc . tags === 'undefined' ) {
23
+ return ;
24
+ }
25
+
26
+ const jsdocTags = jsdoc . tags . filter ( ( item ) => {
27
+ return item . tag === targetTagName ;
14
28
} ) ;
15
29
16
- const sourcecode = utils . getFunctionSourceCode ( ) ;
30
+ if ( jsdocTags . length === 0 ) {
31
+ return ;
32
+ }
17
33
18
- const voidReturn = jsdocTags . findIndex ( ( vundef ) => {
19
- return [ 'undefined' , 'void' ] . indexOf ( vundef . type ) !== - 1 ;
20
- } ) === - 1 ;
34
+ if ( jsdocTags . length > 1 ) {
35
+ report ( 'Found more than one @' + targetTagName + ' declaration.' ) ;
21
36
22
- // Implicit return like `() => foo` is ok
23
- if ( functionNode . type === 'ArrowFunctionExpression' && functionNode . expression ) {
24
37
return ;
25
38
}
26
39
27
- // Async function always returns a promise
28
- if ( functionNode . async ) {
40
+ // An abstract function is by definition incomplete
41
+ // so it is perfectly fine if the return is missing
42
+ // a subclass may inherits the doc an implements the
43
+ // missing return.
44
+ const isAbstract = jsdoc . tags . some ( ( item ) => {
45
+ return item . tag === utils . getPreferredTagName ( 'abstract' ) ;
46
+ } ) ;
47
+
48
+ if ( isAbstract ) {
29
49
return ;
30
50
}
31
51
32
- if ( JSON . stringify ( jsdocTags ) !== '[]' && voidReturn && sourcecode . indexOf ( 'return' ) < 1 ) {
52
+ const sourcecode = utils . getFunctionSourceCode ( ) ;
53
+
54
+ if ( sourcecode . indexOf ( 'return' ) === - 1 ) {
33
55
report ( 'Present JSDoc @' + targetTagName + ' declaration but not available return expression in function.' ) ;
34
56
}
35
57
} ) ;
0 commit comments