diff --git a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts index eeb9d6e39ae..7871035672f 100644 --- a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts +++ b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts @@ -211,6 +211,7 @@ export default util.createRule({ } function isUnexported(node: TSESTree.Node | undefined): boolean { + let isReturnedValue = false; while (node) { if ( node.type === AST_NODE_TYPES.ExportDefaultDeclaration || @@ -220,6 +221,22 @@ export default util.createRule({ return false; } + if (node.type === AST_NODE_TYPES.ReturnStatement) { + isReturnedValue = true; + } + + if ( + node.type === AST_NODE_TYPES.ArrowFunctionExpression || + node.type === AST_NODE_TYPES.FunctionDeclaration || + node.type === AST_NODE_TYPES.FunctionExpression + ) { + isReturnedValue = false; + } + + if (node.type === AST_NODE_TYPES.BlockStatement && !isReturnedValue) { + return true; + } + node = node.parent; } diff --git a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts index f67218588d7..c257d60f715 100644 --- a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts @@ -72,6 +72,39 @@ export class Test { { filename: 'test.ts', code: ` +export function test(): void { + nested(); + return; + + function nested() {} +} + `, + }, + { + filename: 'test.ts', + code: ` +export function test(): string { + const nested = () => 'value'; + return nested(); +} + `, + }, + { + filename: 'test.ts', + code: ` +export function test(): string { + class Nested { + public method() { + return 'value'; + } + } + return new Nested().method(); +} + `, + }, + { + filename: 'test.ts', + code: ` export var arrowFn: Foo = () => 'test'; `, options: [ @@ -452,6 +485,26 @@ export class Foo { }, ], }, + { + filename: 'test.ts', + code: 'export default () => true ? (() => {}) : ((): void => {});', + errors: [ + { + messageId: 'missingReturnType', + line: 1, + endLine: 1, + column: 16, + endColumn: 21, + }, + { + messageId: 'missingReturnType', + line: 1, + endLine: 1, + column: 30, + endColumn: 35, + }, + ], + }, { filename: 'test.ts', code: "export var arrowFn = () => 'test';",