Skip to content

Commit

Permalink
fix(require-returns): handle contexts TSFunctionType, TSMethodSigna…
Browse files Browse the repository at this point in the history
…ture, MethodDefinition; fixes #767
  • Loading branch information
brettz9 committed Jul 20, 2021
1 parent 641cdee commit fdca227
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Expand Up @@ -17453,6 +17453,46 @@ async function quux () {
}
// "jsdoc/require-returns": ["error"|"warn", {"forceReturnsWithAsync":true}]
// Message: Missing JSDoc @returns declaration.

export class A {
/**
* Description.
*/
public f(): string {
return "";
}
}

export interface B {
/**
* Description.
*/
f(): string;

/**
* Description.
*/
g: () => string;

/**
* Description.
*/
h(): void;

/**
* Description.
*/
i: () => void;
}

/**
* Description.
*/
export function f(): string {
return "";
}
// "jsdoc/require-returns": ["error"|"warn", {"contexts":[":not(BlockStatement) > FunctionDeclaration","MethodDefinition","TSMethodSignature","TSPropertySignature > TSTypeAnnotation > TSFunctionType"]}]
// Message: Missing JSDoc @returns declaration.
````

The following patterns are not considered problems:
Expand Down
8 changes: 8 additions & 0 deletions src/jsdocUtils.js
Expand Up @@ -527,6 +527,14 @@ const hasReturnValue = (node, promFilter) => {
}

switch (node.type) {
case 'TSFunctionType':
case 'TSMethodSignature':
return ![
'TSVoidKeyword',
'TSUndefinedKeyword',
].includes(node?.returnType?.typeAnnotation?.type);
case 'MethodDefinition':
return hasReturnValue(node.value, promFilter);
case 'FunctionExpression':
case 'FunctionDeclaration':
case 'ArrowFunctionExpression': {
Expand Down
68 changes: 68 additions & 0 deletions test/rules/assertions/requireReturns.js
Expand Up @@ -1543,6 +1543,74 @@ export default {
ecmaVersion: 8,
},
},
{
code: `
export class A {
/**
* Description.
*/
public f(): string {
return "";
}
}
export interface B {
/**
* Description.
*/
f(): string;
/**
* Description.
*/
g: () => string;
/**
* Description.
*/
h(): void;
/**
* Description.
*/
i: () => void;
}
/**
* Description.
*/
export function f(): string {
return "";
}
`,
errors: [
{
line: 3,
message: 'Missing JSDoc @returns declaration.',
},
{
line: 12,
message: 'Missing JSDoc @returns declaration.',
},
{
line: 17,
message: 'Missing JSDoc @returns declaration.',
},
{
line: 33,
message: 'Missing JSDoc @returns declaration.',
},
],
options: [{
contexts: [
':not(BlockStatement) > FunctionDeclaration',
'MethodDefinition',
'TSMethodSignature',
'TSPropertySignature > TSTypeAnnotation > TSFunctionType',
],
}],
parser: require.resolve('@typescript-eslint/parser'),
},
],
valid: [
{
Expand Down

0 comments on commit fdca227

Please sign in to comment.