From 9b37ff01366aa897a000e4f9e8a020b8c133599c Mon Sep 17 00:00:00 2001 From: Ifiok Jr Date: Tue, 26 May 2020 09:37:27 +0200 Subject: [PATCH] fix(eslint-plugin): explicit-module-boundary-types Exit early when the type of the ancestor of the node being checked is not a return statement. --- .../src/util/explicitReturnTypeUtils.ts | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts index 2e26f69b1cab..56d4a267a2d1 100644 --- a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts +++ b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts @@ -366,34 +366,31 @@ function ancestorHasReturnType( ancestor: TSESTree.Node | undefined, options: Options, ): boolean { + // Exit early if this ancestor is not a ReturnStatement. + if (ancestor?.type !== AST_NODE_TYPES.ReturnStatement) { + return false; + } + // This boolean tells the `isValidFunctionReturnType` that it is being called // by an ancestor check. const isParentCheck = true; - // Has a valid type been found in any of the ancestors. - let hasType = false; - while (ancestor) { switch (ancestor.type) { case AST_NODE_TYPES.ArrowFunctionExpression: case AST_NODE_TYPES.FunctionExpression: - hasType = + return ( isValidFunctionExpressionReturnType(ancestor, options) || - isValidFunctionReturnType(ancestor, options, isParentCheck); - break; + isValidFunctionReturnType(ancestor, options, isParentCheck) + ); case AST_NODE_TYPES.FunctionDeclaration: - hasType = isValidFunctionReturnType(ancestor, options, isParentCheck); - break; - } - - if (hasType) { - return hasType; + return isValidFunctionReturnType(ancestor, options, isParentCheck); } ancestor = ancestor.parent; } - return hasType; + return false; } export {