1- import _ from 'lodash' ;
21import iterateJsdoc from '../iterateJsdoc' ;
32
43export default iterateJsdoc ( ( {
@@ -7,29 +6,52 @@ export default iterateJsdoc(({
76 functionNode,
87 utils
98} ) => {
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+
1019 const targetTagName = utils . getPreferredTagName ( 'returns' ) ;
1120
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 ;
1428 } ) ;
1529
16- const sourcecode = utils . getFunctionSourceCode ( ) ;
30+ if ( jsdocTags . length === 0 ) {
31+ return ;
32+ }
1733
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.' ) ;
2136
22- // Implicit return like `() => foo` is ok
23- if ( functionNode . type === 'ArrowFunctionExpression' && functionNode . expression ) {
2437 return ;
2538 }
2639
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 ) {
2949 return ;
3050 }
3151
32- if ( JSON . stringify ( jsdocTags ) !== '[]' && voidReturn && sourcecode . indexOf ( 'return' ) < 1 ) {
52+ const sourcecode = utils . getFunctionSourceCode ( ) ;
53+
54+ if ( sourcecode . indexOf ( 'return' ) === - 1 ) {
3355 report ( 'Present JSDoc @' + targetTagName + ' declaration but not available return expression in function.' ) ;
3456 }
3557} ) ;
0 commit comments