Skip to content

feat(eslint-plugin): [explicit-module-boundary-types] add an option to ignore overload implementations #10889

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ export const foo: FooType = bar => {};
</TabItem>
</Tabs>

### `allowOverloadFunctions`

{/* insert option description */}

Examples of correct code when `allowOverloadFunctions` is set to `true`:

```ts option='{ "allowOverloadFunctions": true }' showPlaygroundButton
export function test(a: string): string;
export function test(a: number): number;
export function test(a: unknown) {
return a;
}
```

## When Not To Use It

If your project is not used by downstream consumers that are sensitive to API types, you can disable this rule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import type {
FunctionNode,
} from '../util/explicitReturnTypeUtils';

import { createRule, isFunction, isStaticMemberAccessOfValue } from '../util';
import {
createRule,
hasOverloadSignatures,
isFunction,
isStaticMemberAccessOfValue,
} from '../util';
import {
ancestorHasReturnType,
checkFunctionExpressionReturnType,
Expand All @@ -25,6 +30,7 @@ export type Options = [
allowedNames?: string[];
allowHigherOrderFunctions?: boolean;
allowTypedFunctionExpressions?: boolean;
allowOverloadFunctions?: boolean;
},
];
export type MessageIds =
Expand Down Expand Up @@ -82,6 +88,11 @@ export default createRule<Options, MessageIds>({
'You must still type the parameters of the function.',
].join('\n'),
},
allowOverloadFunctions: {
type: 'boolean',
description:
'Whether to ignore return type annotations on functions with overload signatures.',
},
allowTypedFunctionExpressions: {
type: 'boolean',
description:
Expand All @@ -97,6 +108,7 @@ export default createRule<Options, MessageIds>({
allowDirectConstAssertionInArrowFunctions: true,
allowedNames: [],
allowHigherOrderFunctions: true,
allowOverloadFunctions: false,
allowTypedFunctionExpressions: true,
},
],
Expand Down Expand Up @@ -456,6 +468,14 @@ export default createRule<Options, MessageIds>({
return;
}

if (
options.allowOverloadFunctions &&
node.parent.type === AST_NODE_TYPES.MethodDefinition &&
hasOverloadSignatures(node.parent, context)
) {
return;
}

checkFunctionExpressionReturnType(
{ node, returns },
options,
Expand Down Expand Up @@ -485,6 +505,13 @@ export default createRule<Options, MessageIds>({
return;
}

if (
options.allowOverloadFunctions &&
hasOverloadSignatures(node, context)
) {
return;
}

checkFunctionReturnType(
{ node, returns },
options,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,64 @@ export const a: Foo = {
f: (x: boolean) => x,
};
`,
{
code: `
export function test(a: string): string;
export function test(a: number): number;
export function test(a: unknown) {
return a;
}
`,
options: [
{
allowOverloadFunctions: true,
},
],
},
{
code: `
export default function test(a: string): string;
export default function test(a: number): number;
export default function test(a: unknown) {
return a;
}
`,
options: [
{
allowOverloadFunctions: true,
},
],
},
{
code: `
export default function (a: string): string;
export default function (a: number): number;
export default function (a: unknown) {
return a;
}
`,
options: [
{
allowOverloadFunctions: true,
},
],
},
{
code: `
export class Test {
test(a: string): string;
test(a: number): number;
test(a: unknown) {
return a;
}
}
`,
options: [
{
allowOverloadFunctions: true,
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -2077,5 +2135,75 @@ export const foo = {
},
],
},
{
code: `
export function test(a: string): string;
export function test(a: number): number;
export function test(a: unknown) {
return a;
}
`,
errors: [
{
column: 8,
endColumn: 21,
line: 4,
messageId: 'missingReturnType',
},
],
},
{
code: `
export default function test(a: string): string;
export default function test(a: number): number;
export default function test(a: unknown) {
return a;
}
`,
errors: [
{
column: 16,
endColumn: 29,
line: 4,
messageId: 'missingReturnType',
},
],
},
{
code: `
export default function (a: string): string;
export default function (a: number): number;
export default function (a: unknown) {
return a;
}
`,
errors: [
{
column: 16,
endColumn: 25,
line: 4,
messageId: 'missingReturnType',
},
],
},
{
code: `
export class Test {
test(a: string): string;
test(a: number): number;
test(a: unknown) {
return a;
}
}
`,
errors: [
{
column: 3,
endColumn: 7,
line: 5,
messageId: 'missingReturnType',
},
],
},
],
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading