From 52b424a395eb901e5ab60fb5e9a26b0661a1f291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Sat, 9 Nov 2024 19:23:32 +0900 Subject: [PATCH 1/3] feat: add function overload support --- packages/eslint-plugin/src/rules/max-params.ts | 7 ++++++- .../eslint-plugin/tests/rules/max-params.test.ts | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/max-params.ts b/packages/eslint-plugin/src/rules/max-params.ts index fe566bff824a..1ffdd7c86a32 100644 --- a/packages/eslint-plugin/src/rules/max-params.ts +++ b/packages/eslint-plugin/src/rules/max-params.ts @@ -9,13 +9,16 @@ import type { import { createRule } from '../util'; import { getESLintCoreRule } from '../util/getESLintCoreRule'; +import { TSDeclareFunction, TSFunctionType } from '@babel/types'; type FunctionLike = | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression; -type FunctionRuleListener = (node: T) => void; +type FunctionRuleListener< + T extends FunctionLike | TSDeclareFunction | TSFunctionType, +> = (node: T) => void; const baseRule = getESLintCoreRule('max-params'); @@ -96,6 +99,8 @@ export default createRule({ ArrowFunctionExpression: wrapListener(baseRules.ArrowFunctionExpression), FunctionDeclaration: wrapListener(baseRules.FunctionDeclaration), FunctionExpression: wrapListener(baseRules.FunctionExpression), + TSDeclareFunction: wrapListener(baseRules.FunctionDeclaration), + TSFunctionType: wrapListener(baseRules.FunctionDeclaration), }; }, }); diff --git a/packages/eslint-plugin/tests/rules/max-params.test.ts b/packages/eslint-plugin/tests/rules/max-params.test.ts index 0bbbdcdc92be..1c04ace092e7 100644 --- a/packages/eslint-plugin/tests/rules/max-params.test.ts +++ b/packages/eslint-plugin/tests/rules/max-params.test.ts @@ -98,5 +98,19 @@ class Foo { `, errors: [{ messageId: 'exceed' }], }, + { + code: ` +function makeDate(m: number, d: number, y: number): Date; + `, + options: [{ max: 1 }], + errors: [{ messageId: 'exceed' }], + }, + { + code: ` +type sum = (a: number, b:number) => number + `, + options: [{ max: 1 }], + errors: [{ messageId: 'exceed' }], + }, ], }); From 8556e9167f8a6a475a3cb21fb6342d84f51c00b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Sat, 9 Nov 2024 20:47:03 +0900 Subject: [PATCH 2/3] fix: type error --- packages/eslint-plugin/src/rules/max-params.ts | 9 ++++----- packages/eslint-plugin/typings/eslint-rules.d.ts | 7 ++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/max-params.ts b/packages/eslint-plugin/src/rules/max-params.ts index 1ffdd7c86a32..bc1b2459c050 100644 --- a/packages/eslint-plugin/src/rules/max-params.ts +++ b/packages/eslint-plugin/src/rules/max-params.ts @@ -9,16 +9,15 @@ import type { import { createRule } from '../util'; import { getESLintCoreRule } from '../util/getESLintCoreRule'; -import { TSDeclareFunction, TSFunctionType } from '@babel/types'; type FunctionLike = | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration - | TSESTree.FunctionExpression; + | TSESTree.FunctionExpression + | TSESTree.TSDeclareFunction + | TSESTree.TSFunctionType; -type FunctionRuleListener< - T extends FunctionLike | TSDeclareFunction | TSFunctionType, -> = (node: T) => void; +type FunctionRuleListener = (node: T) => void; const baseRule = getESLintCoreRule('max-params'); diff --git a/packages/eslint-plugin/typings/eslint-rules.d.ts b/packages/eslint-plugin/typings/eslint-rules.d.ts index a946d3365a55..6632125e3b15 100644 --- a/packages/eslint-plugin/typings/eslint-rules.d.ts +++ b/packages/eslint-plugin/typings/eslint-rules.d.ts @@ -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; } >; From 4d7cf1964848dbb7968920310aac3b5219ec239f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Mon, 11 Nov 2024 08:01:24 +0900 Subject: [PATCH 3/3] test: add valid test case --- .../tests/rules/max-params.test.ts | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/max-params.test.ts b/packages/eslint-plugin/tests/rules/max-params.test.ts index 1c04ace092e7..3edbd34ae346 100644 --- a/packages/eslint-plugin/tests/rules/max-params.test.ts +++ b/packages/eslint-plugin/tests/rules/max-params.test.ts @@ -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' }] }, @@ -100,17 +112,17 @@ class Foo { }, { code: ` -function makeDate(m: number, d: number, y: number): Date; +declare function makeDate(m: number, d: number, y: number): Date; `, - options: [{ max: 1 }], errors: [{ messageId: 'exceed' }], + options: [{ max: 1 }], }, { code: ` -type sum = (a: number, b:number) => number +type sum = (a: number, b: number) => number; `, - options: [{ max: 1 }], errors: [{ messageId: 'exceed' }], + options: [{ max: 1 }], }, ], });