Skip to content

Commit

Permalink
fix: if return type is missing, only check one path; fixes #949
Browse files Browse the repository at this point in the history
Users can still rely on `require-returns-type` to catch missing types
  • Loading branch information
brettz9 committed Jan 23, 2023
1 parent e0f4862 commit 8b72df4
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 6 deletions.
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17813,7 +17813,7 @@ function quux () {
// Message: JSDoc @returns declaration present but return expression not available in function.

/**
* @returns Baz.
* @returns {SomeType} Baz.
*/
function foo() {
switch (true) {
Expand All @@ -17827,7 +17827,7 @@ function foo() {
// Message: JSDoc @returns declaration present but return expression not available in function.

/**
* @returns Baz.
* @returns {SomeType} Baz.
*/
function foo() {
switch (true) {
Expand Down Expand Up @@ -18243,6 +18243,16 @@ function assertNumber(val) {
*/
export function readFixture(path: string): Promise<Buffer>;

/**
* Reads a test fixture.
*
* @param path The path to resolve relative to the fixture base. It will be normalized for the
* operating system.
*
* @returns {SomeType} The file contents as buffer.
*/
export function readFixture(path: string): Promise<Buffer>;

/**
* Reads a test fixture.
*
Expand Down Expand Up @@ -18430,6 +18440,41 @@ function foo( bar ) {
return functionWithUnknownReturnType();
}
}

/**
* @returns Baz.
*/
function foo() {
switch (true) {
default:
switch (false) {
default: return;
}
return "baz";
}
};

/**
* @returns Baz.
*/
function foo() {
switch (true) {
default:
switch (false) {
default: return;
}
return "baz";
}
};

/**
* @returns
*/
const quux = (someVar) => {
if (someVar) {
return true;
}
};
````


Expand Down
8 changes: 6 additions & 2 deletions src/rules/requireReturnsCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ export default iterateJsdoc(({
reportMissingReturnForUndefinedTypes ||
!utils.mayBeUndefinedTypeTag(tag)
) &&
!utils.hasValueOrExecutorHasNonEmptyResolveValue(
(tag.type === '' && !utils.hasValueOrExecutorHasNonEmptyResolveValue(
exemptAsync,
) ||
tag.type !== '' && !utils.hasValueOrExecutorHasNonEmptyResolveValue(
exemptAsync,
true,
) && (!exemptGenerators || !node.generator)
)) &&
(!exemptGenerators || !node.generator)
) {
report(`JSDoc @${tagName} declaration present but return expression not available in function.`);
}
Expand Down
62 changes: 60 additions & 2 deletions test/rules/assertions/requireReturnsCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ export default {
{
code: `
/**
* @returns Baz.
* @returns {SomeType} Baz.
*/
function foo() {
switch (true) {
Expand All @@ -635,7 +635,7 @@ export default {
{
code: `
/**
* @returns Baz.
* @returns {SomeType} Baz.
*/
function foo() {
switch (true) {
Expand Down Expand Up @@ -1260,6 +1260,20 @@ export default {
`,
parser: require.resolve('@typescript-eslint/parser'),
},
{
code: `
/**
* Reads a test fixture.
*
* @param path The path to resolve relative to the fixture base. It will be normalized for the
* operating system.
*
* @returns {SomeType} The file contents as buffer.
*/
export function readFixture(path: string): Promise<Buffer>;
`,
parser: require.resolve('@typescript-eslint/parser'),
},
{
code: `
/**
Expand Down Expand Up @@ -1518,5 +1532,49 @@ export default {
}
`,
},
{
code: `
/**
* @returns Baz.
*/
function foo() {
switch (true) {
default:
switch (false) {
default: return;
}
return "baz";
}
};
`,
},
{
code: `
/**
* @returns Baz.
*/
function foo() {
switch (true) {
default:
switch (false) {
default: return;
}
return "baz";
}
};
`,
},
{
code: `
/**
* @returns
*/
const quux = (someVar) => {
if (someVar) {
return true;
}
};
`,
},
],
};

0 comments on commit 8b72df4

Please sign in to comment.