8000 [explicit-function-return-types] default parameters where a type anno… · kirkwaiblinger/typescript-eslint@17c0154 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17c0154

Browse files
[explicit-function-return-types] default parameters where a type annotation is present should count as typed function expressions
fix typescript-eslint#8950
1 parent f248e68 commit 17c0154

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,22 @@ function isTypedParent(
8787
return (
8888
isTypeAssertion(parent) ||
8989
isVariableDeclaratorWithTypeAnnotation(parent) ||
90+
isDefaultFunctionParameterWithTypeAnnotation(parent) ||
9091
isPropertyDefinitionWithTypeAnnotation(parent) ||
9192
isFunctionArgument(parent, callee) ||
9293
isTypedJSX(parent)
9394
);
9495
}
9596

97+
function isDefaultFunctionParameterWithTypeAnnotation(
98+
node: TSESTree.Node,
99+
): boolean {
100+
return (
101+
node.type === AST_NODE_TYPES.AssignmentPattern &&
102+
node.left.typeAnnotation != null
103+
);
104+
}
105+
96106
/**
97107
* Checks if a node belongs to:
98108
* ```

packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,22 @@ class Bar {
770770
}
771771
`,
772772
},
773+
{
774+
code: `
775+
type CallBack = () => void;
776+
777+
function f(gotcha: CallBack = () => {}): void {}
778+
`,
779+
options: [{ allowTypedFunctionExpressions: true }],
780+
},
781+
{
782+
code: `
783+
type CallBack = () => void;
784+
785+
const f = (gotcha: CallBack = () => {}): void => {};
786+
`,
787+
options: [{ allowTypedFunctionExpressions: true }],
788+
},
773789
],
774790
invalid: [
775791
{
@@ -1940,5 +1956,39 @@ let foo = (() => () => {})()();
19401956
},
19411957
],
19421958
},
1959+
{
1960+
code: `
1961+
type CallBack = () => void;
1962+
1963+
function f(gotcha: CallBack = () => {}): void {}
1964+
`,
1965+
options: [{ allowTypedFunctionExpressions: false }],
1966+
errors: [
1967+
{
1968+
messageId: 'missingReturnType',
1969+
line: 4,
1970+
column: 34,
1971+
endLine: 4,
1972+
endColumn: 36,
1973+
},
1974+
],
1975+
},
1976+
{
1977+
code: `
1978+
type CallBack = () => void;
1979+
1980+
const f = (gotcha: CallBack = () => {}): void => {};
1981+
`,
1982+
options: [{ allowTypedFunctionExpressions: false }],
1983+
errors: [
1984+
{
1985+
messageId: 'missingReturnType',
1986+
line: 4,
1987+
column: 34,
1988+
endLine: 4,
1989+
endColumn: 36,
1990+
},
1991+
],
1992+
},
19431993
],
19441994
});

0 commit comments

Comments
 (0)
0