Skip to content

fix(eslint-plugin): [explicit-module-boundary-types] handle bodyless arrow functions with explicit return types that return functions #2169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export default util.createRule<Options, MessageIds>({
if (
isAllowedName(node.parent) ||
isTypedFunctionExpression(node, options) ||
ancestorHasReturnType(node.parent, options)
ancestorHasReturnType(node.parent)
) {
return;
}
Expand All @@ -424,10 +424,7 @@ export default util.createRule<Options, MessageIds>({
}
checkedFunctions.add(node);

if (
isAllowedName(node.parent) ||
ancestorHasReturnType(node.parent, options)
) {
if (isAllowedName(node.parent) || ancestorHasReturnType(node.parent)) {
return;
}

Expand Down
25 changes: 8 additions & 17 deletions packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,8 @@ function isValidFunctionExpressionReturnType(
function isValidFunctionReturnType(
node: FunctionNode,
options: Options,
isParentCheck = false,
): boolean {
if (
!isParentCheck &&
options.allowHigherOrderFunctions &&
doesImmediatelyReturnFunctionExpression(node)
) {
Expand Down Expand Up @@ -353,29 +351,22 @@ function checkFunctionExpressionReturnType(
* Check whether any ancestor of the provided node has a valid return type, with
* the given options.
*/
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) {
function ancestorHasReturnType(ancestor: TSESTree.Node | undefined): boolean {
// if the ancestor is not a return, then this function was not returned at all, so we can exit early
const isReturnStatne = ancestor?.type === AST_NODE_TYPES.ReturnStatement;
const isBodylessArrow =
ancestor?.type === AST_NODE_TYPES.ArrowFunctionExpression &&
ancestor.body.type !== AST_NODE_TYPES.BlockStatement;
if (!isReturnStatne && !isBodylessArrow) {
return false;
}

// This boolean tells the `isValidFunctionReturnType` that it is being called
// by an ancestor check.
const isParentCheck = true;

while (ancestor) {
switch (ancestor.type) {
case AST_NODE_TYPES.ArrowFunctionExpression:
case AST_NODE_TYPES.FunctionExpression:
return (
isValidFunctionExpressionReturnType(ancestor, options) ||
isValidFunctionReturnType(ancestor, options, isParentCheck)
);
case AST_NODE_TYPES.FunctionDeclaration:
return isValidFunctionReturnType(ancestor, options, isParentCheck);
return ancestor.returnType != null;
}

ancestor = ancestor.parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,12 @@ export function foo(...[a]: any): void {}
`
export function foo(arg = 1): void {}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/2161
{
code: `
export const foo = (): ((n: number) => string) => n => String(n);
`,
},
],
invalid: [
{
Expand Down