From f6b96ce623cfd406287a0825b0e0cc9559e3ff5a Mon Sep 17 00:00:00 2001 From: lonyele Date: Thu, 22 Aug 2019 14:08:33 +0900 Subject: [PATCH] fix: when there are multiple arguments in BlockStatement --- .../rules/explicit-function-return-type.ts | 29 +++++++++---------- .../explicit-function-return-type.test.ts | 11 +++++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/eslint-plugin/src/rules/explicit-function-return-type.ts b/packages/eslint-plugin/src/rules/explicit-function-return-type.ts index da3e7334b6ab..df52383a8c26 100644 --- a/packages/eslint-plugin/src/rules/explicit-function-return-type.ts +++ b/packages/eslint-plugin/src/rules/explicit-function-return-type.ts @@ -169,21 +169,20 @@ export default util.createRule({ return false; } - // Check if body is a block with a single statement - if ( - body.type === AST_NODE_TYPES.BlockStatement && - body.body.length === 1 - ) { - const [statement] = body.body; - - // Check if that statement is a return statement with an argument - if ( - statement.type === AST_NODE_TYPES.ReturnStatement && - !!statement.argument - ) { - // If so, check that returned argument as body - body = statement.argument; - } + // Check if body is a block with a return statement + if (body.type === AST_NODE_TYPES.BlockStatement) { + // Check if ReturnStatement exist among body and use that argument + // for checking whether it is a function expression + body.body.some(statement => { + if ( + statement.type === AST_NODE_TYPES.ReturnStatement && + !!statement.argument + ) { + // If so, check that returned argument as body + body = statement.argument; + return; + } + }); } // Check if the body being returned is a function expression diff --git a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts index 942ed5ea2e89..589d665dc94b 100644 --- a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts @@ -246,6 +246,17 @@ function FunctionDeclaration() { `, options: [{ allowHigherOrderFunctions: true }], }, + { + filename: 'test.ts', + code: ` +const x = (arg1: string) => { + const tempVar1 = 'temporary value1'; + const tempVar2 = 'temporary value2'; + return (arg2: number): string => 'foo'; +} + `, + options: [{ allowHigherOrderFunctions: true }], + }, // https://github.com/typescript-eslint/typescript-eslint/issues/679 { filename: 'test.ts',