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 b1ac9af14ff..f31d6f42b8b 100644 --- a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md +++ b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md @@ -84,6 +84,10 @@ Examples of **incorrect** code for this rule with `{ allowExpressions: true }`: ```ts function test() {} + +const fn = () => {}; + +export default () => {}; ``` Examples of **correct** code for this rule with `{ allowExpressions: true }`: @@ -155,24 +159,20 @@ functionWithObjectArg({ Examples of **incorrect** code for this rule with `{ allowHigherOrderFunctions: true }`: ```ts -var arrowFn = (x: number) => (y: number) => x + y; +var arrowFn = () => () => {}; -function fn(x: number) { - return function(y: number) { - return x + y; - }; +function fn() { + return function() {}; } ``` Examples of **correct** code for this rule with `{ allowHigherOrderFunctions: true }`: ```ts -var arrowFn = (x: number) => (y: number): number => x + y; +var arrowFn = () => (): void => {}; -function fn(x: number) { - return function(y: number): number { - return x + y; - }; +function fn() { + return function(): void {}; } ``` 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 b431fe7675c..44ea2d03c8e 100644 --- a/packages/eslint-plugin/src/rules/explicit-function-return-type.ts +++ b/packages/eslint-plugin/src/rules/explicit-function-return-type.ts @@ -256,7 +256,8 @@ export default util.createRule({ if ( options.allowExpressions && node.parent.type !== AST_NODE_TYPES.VariableDeclarator && - node.parent.type !== AST_NODE_TYPES.MethodDefinition + node.parent.type !== AST_NODE_TYPES.MethodDefinition && + node.parent.type !== AST_NODE_TYPES.ExportDefaultDeclaration ) { return; } 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 72d64d2abe0..32f23792591 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 @@ -90,6 +90,15 @@ class Test { }, ], }, + { + filename: 'test.ts', + code: `export default (): void => {}`, + options: [ + { + allowExpressions: true, + }, + ], + }, { filename: 'test.ts', code: ` @@ -417,6 +426,30 @@ function test() { }, ], }, + { + filename: 'test.ts', + code: 'export default () => {};', + options: [{ allowExpressions: true }], + errors: [ + { + messageId: 'missingReturnType', + line: 1, + column: 16, + }, + ], + }, + { + filename: 'test.ts', + code: 'export default function() {};', + options: [{ allowExpressions: true }], + errors: [ + { + messageId: 'missingReturnType', + line: 1, + column: 16, + }, + ], + }, { filename: 'test.ts', code: "var arrowFn = () => 'test';",