forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
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
parameter
Closes typescript-eslint#7538
- Loading branch information
Showing
7 changed files
with
236 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` parameter so it 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,99 @@ | ||
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils'; | ||
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'; | ||
|
||
import * as util 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 = util.InferOptionsTypeFromRule<typeof baseRule>; | ||
export type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
const schema = Object.values( | ||
util.deepMerge( | ||
{ ...baseRule.meta.schema }, | ||
{ | ||
0: { | ||
oneOf: [ | ||
baseRule.meta.schema[0].oneOf[0], | ||
{ | ||
...baseRule.meta.schema[0].oneOf[1], | ||
properties: { | ||
...baseRule.meta.schema[0].oneOf[1].properties, | ||
countVoidThis: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
), | ||
) as JSONSchema4[]; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'max-params', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Enforce a maximum number of parameters in function definitions', | ||
extendsBaseRule: true, | ||
}, | ||
schema, | ||
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) { | ||
return node; | ||
} | ||
|
||
const params = [...node.params]; | ||
|
||
if ( | ||
params[0] && | ||
params[0].type === AST_NODE_TYPES.Identifier && | ||
params[0].name === 'this' && | ||
params[0].typeAnnotation?.typeAnnotation.type === | ||
AST_NODE_TYPES.TSVoidKeyword | ||
) { | ||
params.shift(); | ||
} | ||
|
||
return { | ||
...node, | ||
params, | ||
}; | ||
}; | ||
|
||
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' }], | ||
}, | ||
], | ||
}); |
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