Skip to content

Commit

Permalink
feat(eslint-plugin): [max-params] add function overload and function …
Browse files Browse the repository at this point in the history
…type support (#10312)

* feat: add function overload support

* fix: type error

* test: add valid test case

---------

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
  • Loading branch information
developer-bandi and JoshuaKGoldberg authored Nov 22, 2024
1 parent 0b90e8e commit cdbc669
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/eslint-plugin/src/rules/max-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { getESLintCoreRule } from '../util/getESLintCoreRule';
type FunctionLike =
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression;
| TSESTree.FunctionExpression
| TSESTree.TSDeclareFunction
| TSESTree.TSFunctionType;

type FunctionRuleListener<T extends FunctionLike> = (node: T) => void;

Expand Down Expand Up @@ -97,6 +99,8 @@ export default createRule<Options, MessageIds>({
ArrowFunctionExpression: wrapListener(baseRules.ArrowFunctionExpression),
FunctionDeclaration: wrapListener(baseRules.FunctionDeclaration),
FunctionExpression: wrapListener(baseRules.FunctionExpression),
TSDeclareFunction: wrapListener(baseRules.FunctionDeclaration),
TSFunctionType: wrapListener(baseRules.FunctionDeclaration),
};
},
});
26 changes: 26 additions & 0 deletions packages/eslint-plugin/tests/rules/max-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ class Foo {
`,
options: [{ countVoidThis: true, max: 2 }],
},
{
code: `
declare function makeDate(m: number, d: number, y: number): Date;
`,
options: [{ max: 3 }],
},
{
code: `
type sum = (a: number, b: number) => number;
`,
options: [{ max: 2 }],
},
],
invalid: [
{ code: 'function foo(a, b, c, d) {}', errors: [{ messageId: 'exceed' }] },
Expand Down Expand Up @@ -98,5 +110,19 @@ class Foo {
`,
errors: [{ messageId: 'exceed' }],
},
{
code: `
declare function makeDate(m: number, d: number, y: number): Date;
`,
errors: [{ messageId: 'exceed' }],
options: [{ max: 1 }],
},
{
code: `
type sum = (a: number, b: number) => number;
`,
errors: [{ messageId: 'exceed' }],
options: [{ max: 1 }],
},
],
});
7 changes: 6 additions & 1 deletion packages/eslint-plugin/typings/eslint-rules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ declare module 'eslint/lib/rules/max-params' {
unknown,
{
ArrowFunctionExpression(node: TSESTree.ArrowFunctionExpression): void;
FunctionDeclaration(node: TSESTree.FunctionDeclaration): void;
FunctionDeclaration(
node:
| TSESTree.FunctionDeclaration
| TSESTree.TSDeclareFunction
| TSESTree.TSFunctionType,
): void;
FunctionExpression(node: TSESTree.FunctionExpression): void;
}
>;
Expand Down

0 comments on commit cdbc669

Please sign in to comment.