From 2c1e47c7dac67171d56ce334a68f861ed536cfad Mon Sep 17 00:00:00 2001 From: Gilbert Gilb's Date: Thu, 28 Mar 2019 17:47:18 +0100 Subject: [PATCH 1/2] fix(eslint-plugin): fix allowExpressions option Fixes #387 --- .../rules/explicit-function-return-type.ts | 22 +++++++++---------- .../explicit-function-return-type.test.ts | 19 ++++++++++++++++ 2 files changed, 30 insertions(+), 11 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 9cb9c9831046..0d9f9304d2a4 100644 --- a/packages/eslint-plugin/src/rules/explicit-function-return-type.ts +++ b/packages/eslint-plugin/src/rules/explicit-function-return-type.ts @@ -39,7 +39,7 @@ export default util.createRule({ }, defaultOptions: [ { - allowExpressions: true, + allowExpressions: false, allowTypedFunctionExpressions: false, }, ], @@ -88,6 +88,16 @@ export default util.createRule({ | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression, ): void { + if ( + options.allowExpressions && + node.type !== AST_NODE_TYPES.FunctionDeclaration && + node.parent && + node.parent.type !== AST_NODE_TYPES.VariableDeclarator && + node.parent.type !== AST_NODE_TYPES.MethodDefinition + ) { + return; + } + if ( !node.returnType && node.parent && @@ -112,16 +122,6 @@ export default util.createRule({ | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression, ): void { - if ( - options.allowExpressions && - node.type !== AST_NODE_TYPES.ArrowFunctionExpression && - node.parent && - node.parent.type !== AST_NODE_TYPES.VariableDeclarator && - node.parent.type !== AST_NODE_TYPES.MethodDefinition - ) { - return; - } - if ( options.allowTypedFunctionExpressions && node.parent && 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 6786b92f4b14..45ff620d6169 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 @@ -54,6 +54,7 @@ function test() { `, }, { + filename: 'test.ts', code: `fn(() => {});`, options: [ { @@ -62,6 +63,7 @@ function test() { ], }, { + filename: 'test.ts', code: `fn(function() {});`, options: [ { @@ -70,6 +72,7 @@ function test() { ], }, { + filename: 'test.ts', code: `[function() {}, () => {}]`, options: [ { @@ -78,6 +81,7 @@ function test() { ], }, { + filename: 'test.ts', code: `(function() {});`, options: [ { @@ -86,6 +90,7 @@ function test() { ], }, { + filename: 'test.ts', code: `(() => {})();`, options: [ { @@ -193,6 +198,20 @@ class Test { }, ], }, + { + filename: 'test.ts', + code: `function test() { + return; + }`, + options: [{ allowExpressions: true }], + errors: [ + { + messageId: 'missingReturnType', + line: 1, + column: 1, + }, + ], + }, { filename: 'test.ts', code: `const foo = () => {};`, From 21516f3440c3e613afd19d2c52e59a131953f5c8 Mon Sep 17 00:00:00 2001 From: Gilbert Gilb's Date: Thu, 28 Mar 2019 17:57:17 +0100 Subject: [PATCH 2/2] docs(eslint-plugin): document allowTypedFunctionExpressions option --- .../docs/rules/explicit-function-return-type.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md index e63d3d351a25..b6f37038aba1 100644 --- a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md +++ b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md @@ -62,9 +62,11 @@ class Test { The rule accepts an options object with the following properties: - `allowExpressions` if true, only functions which are part of a declaration will be checked +- `allowTypedFunctionExpressions` if true, type annotations are also allowed on the variable + of a function expression rather than on the function directly. -By default, `allowExpressions: false` is used, meaning all declarations and -expressions _must_ have a return type. +By default, `allowExpressions: false` and `allowTypedFunctionExpressions: false` are used, +meaning all declarations and expressions _must_ have a return type. ### allowExpressions