diff --git a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts index 2604e6185cc..f65273a502e 100644 --- a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts +++ b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts @@ -87,12 +87,22 @@ function isTypedParent( return ( isTypeAssertion(parent) || isVariableDeclaratorWithTypeAnnotation(parent) || + isDefaultFunctionParameterWithTypeAnnotation(parent) || isPropertyDefinitionWithTypeAnnotation(parent) || isFunctionArgument(parent, callee) || isTypedJSX(parent) ); } +function isDefaultFunctionParameterWithTypeAnnotation( + node: TSESTree.Node, +): boolean { + return ( + node.type === AST_NODE_TYPES.AssignmentPattern && + node.left.typeAnnotation != null + ); +} + /** * Checks if a node belongs to: * ``` 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 7556cee3ce7..0e8c5182b7d 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 @@ -770,7 +770,32 @@ class Bar { } `, }, + { + code: ` +type CallBack = () => void; + +function f(gotcha: CallBack = () => {}): void {} + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + code: ` +type CallBack = () => void; + +const f = (gotcha: CallBack = () => {}): void => {}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + code: ` +type ObjectWithCallback = { callback: () => void }; + +const f = (gotcha: ObjectWithCallback = { callback: () => {} }): void => {}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, ], + invalid: [ { code: ` @@ -1940,5 +1965,54 @@ let foo = (() => () => {})()(); }, ], }, + { + code: ` +type CallBack = () => void; + +function f(gotcha: CallBack = () => {}): void {} + `, + options: [{ allowTypedFunctionExpressions: false }], + errors: [ + { + messageId: 'missingReturnType', + line: 4, + column: 34, + endLine: 4, + endColumn: 36, + }, + ], + }, + { + code: ` +type CallBack = () => void; + +const f = (gotcha: CallBack = () => {}): void => {}; + `, + options: [{ allowTypedFunctionExpressions: false }], + errors: [ + { + messageId: 'missingReturnType', + line: 4, + column: 34, + endLine: 4, + endColumn: 36, + }, + ], + }, + { + code: ` +type ObjectWithCallback = { callback: () => void }; + +const f = (gotcha: ObjectWithCallback = { callback: () => {} }): void => {}; + `, + options: [{ allowTypedFunctionExpressions: false }], + errors: [ + { + messageId: 'missingReturnType', + line: 4, + column: 43, + }, + ], + }, ], });