Skip to content

Commit

Permalink
[explicit-function-return-types] default parameters where a type anno…
Browse files Browse the repository at this point in the history
…tation is present should count as typed function expressions

fix typescript-eslint#8950
  • Loading branch information
kirkwaiblinger committed May 4, 2024
1 parent f248e68 commit fd81f5d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts
Expand Up @@ -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:
* ```
Expand Down
Expand Up @@ -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: `
Expand Down Expand Up @@ -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,
},
],
},
],
});

0 comments on commit fd81f5d

Please sign in to comment.