Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [explicit-function-return-type] add allowFunctionsWithoutTypeParameters option #6105

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -15,6 +15,7 @@ type Options = [
allowHigherOrderFunctions?: boolean;
allowDirectConstAssertionInArrowFunctions?: boolean;
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
allowFunctionsWithoutTypeParameters?: boolean;
allowedNames?: string[];
},
];
Expand Down Expand Up @@ -61,6 +62,11 @@ export default util.createRule<Options, MessageIds>({
'Whether to ignore arrow functions immediately returning a `as const` value.',
type: 'boolean',
},
allowFunctionsWithoutTypeParameters: {
description:
"Whether to ignore functions that don't have generic type parameters.",
type: 'boolean',
},
allowedNames: {
description:
'An array of function/method names that will not have their arguments or return values checked.',
Expand All @@ -86,12 +92,16 @@ export default util.createRule<Options, MessageIds>({
],
create(context, [options]) {
const sourceCode = context.getSourceCode();
function isAllowedName(
function isAllowedFunction(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression
| TSESTree.FunctionDeclaration,
): boolean {
if (options.allowFunctionsWithoutTypeParameters && !node.typeParameters) {
return true;
}

if (!options.allowedNames?.length) {
return false;
}
Expand Down Expand Up @@ -153,7 +163,7 @@ export default util.createRule<Options, MessageIds>({
return;
}

if (isAllowedName(node)) {
if (isAllowedFunction(node)) {
return;
}

Expand All @@ -174,7 +184,7 @@ export default util.createRule<Options, MessageIds>({
);
},
FunctionDeclaration(node): void {
if (isAllowedName(node)) {
if (isAllowedFunction(node)) {
return;
}
if (options.allowTypedFunctionExpressions && node.returnType) {
Expand Down
Expand Up @@ -400,6 +400,46 @@ new Foo(1, () => {});
code: 'const log = (message: string) => void console.log(message);',
options: [{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }],
},
{
code: 'const log = (a: string) => a;',
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
code: 'const log = <A>(a: A): A => a;',
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
code: `
function log<A>(a: A): A {
return a;
}
`,
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
code: `
function log(a: string) {
return a;
}
`,
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
code: `
const log = function <A>(a: A): A {
return a;
};
`,
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
code: `
const log = function (a: A): string {
return a;
};
`,
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
filename: 'test.ts',
options: [
Expand Down Expand Up @@ -1319,6 +1359,29 @@ const func = (value: number) => ({ type: 'X', value } as const);
},
],
},
{
code: 'const log = <A>(a: A) => a;',
errors: [{ messageId: 'missingReturnType' }],
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
code: `
function log<A>(a: A) {
return a;
}
`,
errors: [{ messageId: 'missingReturnType' }],
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
code: `
const log = function <A>(a: A) {
return a;
};
`,
errors: [{ messageId: 'missingReturnType' }],
options: [{ allowFunctionsWithoutTypeParameters: true }],
},
{
filename: 'test.ts',
options: [
Expand Down