Skip to content

Commit

Permalink
fix(eslint-plugin): [explicit-function-return-types] fix false positi…
Browse files Browse the repository at this point in the history
…ve on default parameters (#9045)

[explicit-function-return-types] default parameters where a type annotation is present should count as typed function expressions

fix #8950

Co-authored-by: auvred <61150013+auvred@users.noreply.github.com>
  • Loading branch information
kirkwaiblinger and auvred committed May 13, 2024
1 parent 7ce6acd commit 8acb8d4
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 8acb8d4

Please sign in to comment.