Skip to content

Commit

Permalink
fix: recognize implicit return in require-returns-check (fixes #156)
Browse files Browse the repository at this point in the history
fix: recognize implicit return in require-returns-check (fixes #156)
  • Loading branch information
gajus committed Mar 15, 2019
2 parents 897f205 + 3483159 commit b8b9692
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -2257,6 +2257,12 @@ function quux (foo) {
}
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
// Message: Present JSDoc @return declaration but not available return expression in function.

/**
* @returns
*/
const quux = () => {}
// Message: Present JSDoc @returns declaration but not available return expression in function.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -2291,6 +2297,11 @@ function quux () {
*/
function quux () {
}

/**
* @returns {*} Foo.
*/
const quux = () => foo;
````


Expand Down
6 changes: 6 additions & 0 deletions src/rules/requireReturnsCheck.js
100644 → 100755
Expand Up @@ -4,6 +4,7 @@ import iterateJsdoc from '../iterateJsdoc';
export default iterateJsdoc(({
jsdoc,
report,
functionNode,
utils
}) => {
const targetTagName = utils.getPreferredTagName('returns');
Expand All @@ -18,6 +19,11 @@ export default iterateJsdoc(({
return ['undefined', 'void'].indexOf(vundef.type) !== -1;
}) === -1;

// Implicit return like `() => foo` is ok
if (functionNode.type === 'ArrowFunctionExpression' && functionNode.expression) {
return;
}

if (JSON.stringify(jsdocTags) !== '[]' && voidReturn && sourcecode.indexOf('return') < 1) {
report('Present JSDoc @' + targetTagName + ' declaration but not available return expression in function.');
}
Expand Down
22 changes: 22 additions & 0 deletions test/rules/assertions/requireReturnsCheck.js
100644 → 100755
Expand Up @@ -38,6 +38,20 @@ export default {
}
}
}
},
{
code: `
/**
* @returns
*/
const quux = () => {}
`,
errors: [
{
line: 2,
message: 'Present JSDoc @returns declaration but not available return expression in function.'
}
]
}
],
valid: [
Expand Down Expand Up @@ -82,6 +96,14 @@ export default {
function quux () {
}
`
},
{
code: `
/**
* @returns {*} Foo.
*/
const quux = () => foo;
`
}
]
};

0 comments on commit b8b9692

Please sign in to comment.