warning
jsdoc/require-returns-checkRequires a return statement in function body if a @returns
tag is specified in jsdoc comment.
Will also report if multiple @returns
tags are present.
Examples
❌ Incorrect
/**
* @returns
*/
function quux1 (foo) {
}
// Message: JSDoc @returns declaration present but return expression not available in function.
/**
* @returns
*/
const quux2 = () => {}
// Message: JSDoc @returns declaration present but return expression not available in function.
/**
* @returns {undefined} Foo.
* @returns {String} Foo.
*/
function quux3 () {
return foo;
}
// Message: Found more than one @returns declaration.
const language = {
/**
* @param {string} name
* @returns {string}
*/
get name() {
this._name = name;
}
}
// Message: JSDoc @returns declaration present but return expression not available in function.
class Foo {
/**
* @returns {string}
*/
bar () {
}
}
// Message: JSDoc @returns declaration present but return expression not available in function.
/**
* @returns {string}
*/
function f () {
function g() {
return 'foo'
}
() => {
return 5
}
}
// Message: JSDoc @returns declaration present but return expression not available in function.
✅ Correct
/**
* @returns Foo.
*/
function quux1 () {
return foo;
}
/**
* @returns {string} Foo.
*/
function quux2 () {
return foo;
}
/**
* @returns {string} Foo.
*/
function quux3 () {
return foo;
}
/**
*
*/
function quux4 () {
}
/**
* @returns {*} Foo.
*/
const quux5 = () => foo;
/**
* @returns {undefined} Foo.
*/
function quux6 () {}
/**
* @returns { void } Foo.
*/
function quux7 () {}
/**
* @returns {Promise<void>}
*/
async function quux8() {}
/**
* @returns {Promise<void>}
*/
const quux9 = async function () {}
/**
* @returns {Promise<void>}
*/
const quux10 = async () => {}
/**
* @returns Foo.
* @abstract
*/
function quux11 () {
throw new Error('must be implemented by subclass!');
}
/**
* @returns Foo.
* @virtual
*/
function quux12 () {
throw new Error('must be implemented by subclass!');
}
/**
* @returns Foo.
* @constructor
*/
function quux13 () {
}
/**
* @interface
*/
class Foo {
/**
* @returns {string}
*/
bar () {
}
}
/**
* @returns {undefined} Foo.
*/
function quux14 () {
}
/**
* @returns {void} Foo.
*/
function quux15 () {
}
/**
*
*/
function quux16 () {
return undefined;
}
/**
* @returns {true|undefined}
*/
function quux17 () {
try {
return true;
} catch (err) {
}
return;
}