Skip to content

Commit 9c89b9e

Browse files
feat(functional-parameters): add option to ignore getters and setters
this is true by default
1 parent 2055d28 commit 9c89b9e

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

docs/rules/functional-parameters.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Options = {
6363
count: "atLeastOne" | "exactlyOne";
6464
ignoreLambdaExpression: boolean;
6565
ignoreIIFE: boolean;
66+
ignoreGettersAndSetters: boolean;
6667
};
6768
ignorePattern?: string[] | string;
6869
ignorePrefixSelector?: string[] | string;
@@ -79,6 +80,7 @@ const defaults = {
7980
count: "atLeastOne",
8081
ignoreLambdaExpression: false,
8182
ignoreIIFE: true,
83+
ignoreGettersAndSetters: true,
8284
},
8385
};
8486
```
@@ -92,6 +94,7 @@ const recommendedOptions = {
9294
enforceParameterCount: {
9395
ignoreLambdaExpression: true,
9496
ignoreIIFE: true,
97+
ignoreGettersAndSetters: true,
9598
},
9699
};
97100
```
@@ -163,6 +166,10 @@ Here, a lambda function expression refers to any function being defined in place
163166

164167
If true, this option allows for the use of [IIFEs](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) that do not have any parameters.
165168

169+
#### `enforceParameterCount.ignoreGettersAndSetters`
170+
171+
Getters should always take zero parameters, and setter one. If for some reason you want to treat these function like any other function, then you can set this option to `false`.
172+
166173
### `ignorePrefixSelector`
167174

168175
This allows for ignore functions where one of the given selectors matches the parent node in the AST of the function node.\

src/configs/recommended.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const overrides: Linter.Config = {
2222
enforceParameterCount: {
2323
ignoreLambdaExpression: true,
2424
ignoreIIFE: true,
25+
ignoreGettersAndSetters: true,
2526
},
2627
},
2728
],

src/rules/functional-parameters.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
import { createRuleUsingFunction } from "#eslint-plugin-functional/utils/rule";
2424
import {
2525
isArgument,
26+
isGetter,
27+
isSetter,
2628
isIIFE,
2729
isPropertyAccess,
2830
isPropertyName,
@@ -54,6 +56,7 @@ type Options = [
5456
count: ParameterCountOptions;
5557
ignoreLambdaExpression: boolean;
5658
ignoreIIFE: boolean;
59+
ignoreGettersAndSetters: boolean;
5760
};
5861
},
5962
];
@@ -91,6 +94,9 @@ const schema: JSONSchema4[] = [
9194
type: "string",
9295
enum: ["atLeastOne", "exactlyOne"],
9396
},
97+
ignoreGettersAndSetters: {
98+
type: "boolean",
99+
},
94100
ignoreLambdaExpression: {
95101
type: "boolean",
96102
},
@@ -119,6 +125,7 @@ const defaultOptions: Options = [
119125
count: "atLeastOne",
120126
ignoreLambdaExpression: false,
121127
ignoreIIFE: true,
128+
ignoreGettersAndSetters: true,
122129
},
123130
},
124131
];
@@ -179,7 +186,9 @@ function getParamCountViolations(
179186
(node.params.length === 0 &&
180187
typeof enforceParameterCount === "object" &&
181188
((enforceParameterCount.ignoreIIFE && isIIFE(node)) ||
182-
(enforceParameterCount.ignoreLambdaExpression && isArgument(node))))
189+
(enforceParameterCount.ignoreLambdaExpression && isArgument(node)) ||
190+
(enforceParameterCount.ignoreGettersAndSetters &&
191+
(isGetter(node) || isSetter(node)))))
183192
) {
184193
return [];
185194
}

src/utils/tree.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,28 @@ export function isArgument(node: TSESTree.Node): boolean {
207207
);
208208
}
209209

210+
/**
211+
* Is the given node a getter function?
212+
*/
213+
export function isGetter(node: TSESTree.Node): boolean {
214+
return (
215+
node.parent !== undefined &&
216+
isProperty(node.parent) &&
217+
node.parent.kind === "get"
218+
);
219+
}
220+
221+
/**
222+
* Is the given node a setter function?
223+
*/
224+
export function isSetter(node: TSESTree.Node): boolean {
225+
return (
226+
node.parent !== undefined &&
227+
isProperty(node.parent) &&
228+
node.parent.kind === "set"
229+
);
230+
}
231+
210232
/**
211233
* Get the key the given node is assigned to in its parent ObjectExpression.
212234
*/

tests/rules/functional-parameters/es2015/valid.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ const tests: Array<ValidTestCaseSet<OptionsOf<typeof rule>>> = [
102102
`,
103103
optionsSet: [[{ enforceParameterCount: { ignoreLambdaExpression: true } }]],
104104
},
105+
{
106+
code: dedent`
107+
const foo = {
108+
get bar() {
109+
return "baz";
110+
}
111+
}
112+
`,
113+
optionsSet: [[{ enforceParameterCount: {} }]],
114+
},
105115
];
106116

107117
export default tests;

0 commit comments

Comments
 (0)