Skip to content

Commit c13e095

Browse files
Add ESLint rules and documentation for conditional best practices
1 parent 5479169 commit c13e095

32 files changed

+3850
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
/docs
3+
.prettierrc
4+
.github

.prettierrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
tabWidth: 2,
3+
semi: true,
4+
singleQuote: true,
5+
printWidth: 120,
6+
arrowParens: 'always',
7+
proseWrap: 'preserve',
8+
trailingComma: 'es5',
9+
};

docs/no-constant-conditionals.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# no-constant-conditionals
2+
3+
4+
## Description
5+
6+
This rule disallows conditionals that always evaluate to true or false.
7+
8+
## Options
9+
This rule does not accept any options.
10+
11+
## Severity
12+
- Type: Problem
13+
- Recommended: Yes
14+
15+
## Examples
16+
17+
### **Invalid** 👎
18+
19+
```js
20+
if (true) { sayHi(); }
21+
```
22+
23+
```js
24+
if (false) { sayHi(); }
25+
```
26+
27+
### **Valid** 👍
28+
29+
```js
30+
const name = "andreas"; const x = "hello"; if (x === name) { sayHi(); }
31+
```

docs/no-duplicated-conditions.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# no-duplicated-conditions
2+
3+
4+
## Description
5+
6+
This rule disallows duplicate conditions in if-else chains.
7+
8+
## Options
9+
This rule does not accept any options.
10+
11+
## Severity
12+
- Type: Problem
13+
- Recommended: Yes
14+
15+
## Examples
16+
17+
### **Invalid** 👎
18+
19+
```js
20+
if (a) {} else if (a) {}
21+
```
22+
23+
### **Valid** 👍
24+
25+
```js
26+
if (a) {} else if (b) {}
27+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# no-excessive-nested-conditionals
2+
3+
4+
## Description
5+
6+
This rule disallows excessive nesting of conditionals in order to improve code readability.
7+
8+
## Options
9+
10+
```json
11+
{
12+
"rules": {
13+
"@andreasnicolaou/conditional-best-practices/no-excessive-nested-conditionals": ["warn", { "max": 2 }]
14+
}
15+
}
16+
```
17+
18+
- max (optional): The maximum depth of nested conditionals. The default value is 3.
19+
20+
## Severity
21+
- Type: Suggestion
22+
- Recommended: Yes
23+
24+
## Examples
25+
26+
### **Invalid** 👎
27+
28+
```js
29+
if (a) {
30+
if (b) {
31+
if (c) {
32+
if (d) { doSomething(); }
33+
}
34+
}
35+
} else if (b) {} else if (c) {} else if (d) {};
36+
```
37+
38+
```js
39+
if (a) {
40+
if (a) {
41+
if (b) {
42+
if (c) {
43+
if (d) { doSomething(); }
44+
}
45+
}
46+
}
47+
} else if (b) {} else if (c) {} else if (d) {};
48+
```
49+
50+
### **Valid** 👍
51+
52+
```js
53+
if (a) { if (b) { if (c) { doSomething(); } } }
54+
```
55+
56+
```js
57+
if (a) {} else if (b) {} else if (c) {} else if (d) {}
58+
```

docs/no-long-else-if-chains.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# no-long-else-if-chains
2+
3+
4+
## Description
5+
6+
This rule limits the number of consecutive else-if statements.
7+
8+
## Options
9+
10+
```json
11+
{
12+
"rules": {
13+
"@andreasnicolaou/conditional-best-practices/no-long-else-if-chains":
14+
["warn", { "max": 2 }]
15+
}
16+
}
17+
```
18+
19+
- max (optional): The maximum number of consecutive else-if statements. The default value is 3.
20+
21+
## Severity
22+
- Type: Suggestion
23+
- Recommended: Yes
24+
25+
## Examples
26+
27+
### **Invalid** 👎
28+
29+
```js
30+
if (a) {}
31+
else if (b) {}
32+
else if (c) {}
33+
else if (d) {}
34+
else if (e) {}
35+
```
36+
37+
```js
38+
if (a) {}
39+
else if (b) {}
40+
else if (c) {}
41+
else if (d) {}
42+
else if (e) {}
43+
else if (f) {}
44+
```
45+
46+
### **Valid** 👍
47+
48+
```js
49+
if (a) {}
50+
```
51+
52+
```js
53+
if (a) {} else if (b) {}
54+
```
55+
56+
```js
57+
if (a) {} else if (b) {} else if (c) {}
58+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# no-nested-ternary-operators
2+
3+
4+
## Description
5+
6+
This rule disallows nested ternary operators.
7+
8+
## Options
9+
This rule does not accept any options.
10+
11+
## Severity
12+
- Type: Problem
13+
- Recommended: Yes
14+
15+
## Examples
16+
17+
### **Invalid** 👎
18+
19+
```js
20+
const result = condition1 ? "a" : condition2 ? "b" : "c";
21+
```
22+
23+
```js
24+
const result = condition1 ? (condition2 ? "a" : "b") : "c";
25+
```
26+
27+
```js
28+
const result = condition1 ? (condition2 ? "a" : "b") : (condition3 ? "x" : "y");
29+
```
30+
31+
### **Valid** 👍
32+
33+
```js
34+
const result = condition ? "a" : "b";
35+
```

docs/no-useless-ternary.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# no-useless-ternary
2+
3+
4+
## Description
5+
6+
This rule disallows unnecessary ternary expressions.
7+
8+
## Options
9+
This rule does not accept any options.
10+
11+
## Severity
12+
- Type: Suggestion
13+
- Recommended: Yes
14+
15+
## Examples
16+
17+
### **Invalid** 👎
18+
19+
```js
20+
const isActive = condition2 ? true : false;
21+
```
22+
23+
```js
24+
const isInactive = condition3 ? false : true;
25+
```
26+
27+
### **Valid** 👍
28+
29+
```js
30+
const isEnabled = condition1 ? "yes" : "no";
31+
```

docs/prefer-early-return.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# prefer-early-return
2+
3+
4+
## Description
5+
6+
This rule encourages the use of early returns instead of deeply nested if-else blocks.
7+
8+
## Options
9+
This rule does not accept any options.
10+
11+
## Severity
12+
- Type: Problem
13+
- Recommended: Yes
14+
15+
## Examples
16+
17+
### **Invalid** 👎
18+
19+
```js
20+
function check(x) {
21+
if (x) { helloWorld(); }
22+
else { return; }
23+
}
24+
```
25+
26+
### **Valid** 👍
27+
28+
```js
29+
function check(x) { if (!x) return; helloWorld(); }
30+
```

0 commit comments

Comments
 (0)