-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathno-positional-boolean-parameters.ts
47 lines (43 loc) · 1.32 KB
/
no-positional-boolean-parameters.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { type Parameter } from "../ast";
import { type Rule } from "../eslint";
const message = `Avoid boolean positional parameters: use named parameters or even separate functions. Instead of onEdit(true), use onEdit({ isNew: true }), or, even better, onEdit() and onNew().`;
const rule: Rule = {
create: function (context) {
const isBoolean = (name: string): boolean => {
return Boolean(
name.match(/^enabled|disabled|hidden$/) ||
name.match(/^(can|has|hide|is|no|show|will)[A-Z]/),
);
};
const validate = (parameters: Parameter[]) => {
for (const param of parameters) {
if (param.type === "Identifier") {
if (param.typeAnnotation) {
if (
param.typeAnnotation.typeAnnotation.type === "TSBooleanKeyword"
) {
context.report({ message, node: param });
}
return;
}
const name = param.name;
if (isBoolean(name)) {
context.report({ message, node: param });
}
}
}
};
return {
ArrowFunctionExpression(node) {
validate(node.params);
},
FunctionDeclaration(node) {
validate(node.params);
},
FunctionExpression(node) {
validate(node.params);
},
};
},
};
export default rule;