Skip to content

Commit

Permalink
feat(eslint-plugin): [explicit-function-return-type] add allowFunctio…
Browse files Browse the repository at this point in the history
…nsWithoutTypeParameters option (#6105)
  • Loading branch information
JoshuaKGoldberg committed Feb 10, 2023
1 parent 2f948df commit 113640e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
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

0 comments on commit 113640e

Please sign in to comment.