Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): [max-params] don't count
this: void
parameter (#…
…7696) * feat(eslint-plugin): [max-params] don't count `this: void` parameter Closes #7538 * hard-code schema * refactor * lint * snapshot * wip * Update packages/eslint-plugin/src/rules/max-params.ts Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com> * Small PR nits, don't mind me, it's great either way --------- Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
- Loading branch information
1 parent
7e52f27
commit 6398d3f
Showing
8 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
description: 'Enforce a maximum number of parameters in function definitions.' | ||
--- | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/max-params** for documentation. | ||
This rule extends the base [`eslint/max-params`](https://eslint.org/docs/rules/max-params) rule. | ||
This version adds support for TypeScript `this` parameters so they won't be counted as a parameter. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils'; | ||
|
||
import type { | ||
InferMessageIdsTypeFromRule, | ||
InferOptionsTypeFromRule, | ||
} from '../util'; | ||
import { createRule } from '../util'; | ||
import { getESLintCoreRule } from '../util/getESLintCoreRule'; | ||
|
||
type FunctionLike = | ||
| TSESTree.FunctionDeclaration | ||
| TSESTree.FunctionExpression | ||
| TSESTree.ArrowFunctionExpression; | ||
|
||
type FunctionRuleListener<T extends FunctionLike> = (node: T) => void; | ||
|
||
const baseRule = getESLintCoreRule('max-params'); | ||
|
||
export type Options = InferOptionsTypeFromRule<typeof baseRule>; | ||
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'max-params', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Enforce a maximum number of parameters in function definitions', | ||
extendsBaseRule: true, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
maximum: { | ||
type: 'integer', | ||
minimum: 0, | ||
}, | ||
max: { | ||
type: 'integer', | ||
minimum: 0, | ||
}, | ||
countVoidThis: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: baseRule.meta.messages, | ||
}, | ||
defaultOptions: [{ max: 3, countVoidThis: false }], | ||
|
||
create(context, [{ countVoidThis }]) { | ||
const baseRules = baseRule.create(context); | ||
|
||
if (countVoidThis === true) { | ||
return baseRules; | ||
} | ||
|
||
const removeVoidThisParam = <T extends FunctionLike>(node: T): T => { | ||
if ( | ||
node.params.length === 0 || | ||
node.params[0].type !== AST_NODE_TYPES.Identifier || | ||
node.params[0].name !== 'this' || | ||
node.params[0].typeAnnotation?.typeAnnotation.type !== | ||
AST_NODE_TYPES.TSVoidKeyword | ||
) { | ||
return node; | ||
} | ||
|
||
return { | ||
...node, | ||
params: node.params.slice(1), | ||
}; | ||
}; | ||
|
||
const wrapListener = <T extends FunctionLike>( | ||
listener: FunctionRuleListener<T>, | ||
): FunctionRuleListener<T> => { | ||
return (node: T): void => { | ||
listener(removeVoidThisParam(node)); | ||
}; | ||
}; | ||
|
||
return { | ||
ArrowFunctionExpression: wrapListener(baseRules.ArrowFunctionExpression), | ||
FunctionDeclaration: wrapListener(baseRules.FunctionDeclaration), | ||
FunctionExpression: wrapListener(baseRules.FunctionExpression), | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
|
||
import rule from '../../src/rules/max-params'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('max-params', rule, { | ||
valid: [ | ||
'function foo() {}', | ||
'const foo = function () {};', | ||
'const foo = () => {};', | ||
'function foo(a) {}', | ||
` | ||
class Foo { | ||
constructor(a) {} | ||
} | ||
`, | ||
` | ||
class Foo { | ||
method(this: void, a, b, c) {} | ||
} | ||
`, | ||
` | ||
class Foo { | ||
method(this: Foo, a, b) {} | ||
} | ||
`, | ||
{ | ||
code: 'function foo(a, b, c, d) {}', | ||
options: [{ max: 4 }], | ||
}, | ||
{ | ||
code: 'function foo(a, b, c, d) {}', | ||
options: [{ maximum: 4 }], | ||
}, | ||
{ | ||
code: ` | ||
class Foo { | ||
method(this: void) {} | ||
} | ||
`, | ||
options: [{ max: 0 }], | ||
}, | ||
{ | ||
code: ` | ||
class Foo { | ||
method(this: void, a) {} | ||
} | ||
`, | ||
options: [{ max: 1 }], | ||
}, | ||
{ | ||
code: ` | ||
class Foo { | ||
method(this: void, a) {} | ||
} | ||
`, | ||
options: [{ max: 2, countVoidThis: true }], | ||
}, | ||
], | ||
invalid: [ | ||
{ code: 'function foo(a, b, c, d) {}', errors: [{ messageId: 'exceed' }] }, | ||
{ | ||
code: 'const foo = function (a, b, c, d) {};', | ||
errors: [{ messageId: 'exceed' }], | ||
}, | ||
{ | ||
code: 'const foo = (a, b, c, d) => {};', | ||
errors: [{ messageId: 'exceed' }], | ||
}, | ||
{ | ||
code: 'const foo = a => {};', | ||
options: [{ max: 0 }], | ||
errors: [{ messageId: 'exceed' }], | ||
}, | ||
{ | ||
code: ` | ||
class Foo { | ||
method(this: void, a, b, c, d) {} | ||
} | ||
`, | ||
errors: [{ messageId: 'exceed' }], | ||
}, | ||
{ | ||
code: ` | ||
class Foo { | ||
method(this: void, a) {} | ||
} | ||
`, | ||
options: [{ max: 1, countVoidThis: true }], | ||
errors: [{ messageId: 'exceed' }], | ||
}, | ||
{ | ||
code: ` | ||
class Foo { | ||
method(this: Foo, a, b, c) {} | ||
} | ||
`, | ||
errors: [{ messageId: 'exceed' }], | ||
}, | ||
], | ||
}); |
38 changes: 38 additions & 0 deletions
38
packages/eslint-plugin/tests/schema-snapshots/max-params.shot
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters