Skip to content

Commit e9231ef

Browse files
No eval rule (#3)
* Refactor documentation and tests for ESLint rules; add no-eval rule and update formatting * 1.0.3
1 parent d9d0e76 commit e9231ef

35 files changed

+3676
-3469
lines changed

.github/workflows/build.yaml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
name: CI Workflow Build
22
on:
3-
pull_request:
4-
types:
5-
- opened
6-
branches:
7-
- main
3+
pull_request:
4+
types:
5+
- opened
6+
branches:
7+
- main
88
jobs:
9-
test:
10-
runs-on: ubuntu-latest
11-
steps:
12-
- name: Checkout repository
13-
uses: actions/checkout@v4
14-
- name: Set up Node.js
15-
uses: actions/setup-node@v4
16-
with:
17-
node-version: '22'
18-
- name: Install dependencies
19-
run: npm install
20-
- name: Run tests
21-
run: npm run test
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
- name: Set up Node.js
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version: '22'
18+
- name: Install dependencies
19+
run: npm install
20+
- name: Run tests
21+
run: npm run test

.prettierrc.js

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

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# @andreasnicolaou/eslint-plugin-conditional-best-practices
2+
23
This ESLint plugin includes several opinionated rules aimed at enhancing the readability, performance, and maintainability of conditionals in JavaScript.
34

45
These rules focus on writing clear and efficient conditional statements, so while they encourage consistency and simplicity, they may not suit every coding style. Feel free to adjust the rules based on your team's preferences.
56

67
# Installation
78

89
Install the npm package
10+
911
```bash
1012
# If eslint is installed globally
1113
npm install -g @andreasnicolaou/eslint-plugin-conditional-best-practices
@@ -25,22 +27,24 @@ To use this plugin in your project, add it to your ESLint configuration like thi
2527
}
2628

2729
```
30+
2831
- Alternatively, you can extend individual rules if you prefer more granular control.
2932

3033
# Usage
31-
This plugin enforces the following opinionated best practices:
32-
33-
| Name                                        | Description |
34-
| :------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- |
35-
| [no-constant-conditionals](docs/no-constant-conditionals.md) | This rule disallows conditionals that always evaluate to true or false. |
36-
| [no-duplicated-conditions](docs/no-duplicated-conditions.md) | This rule disallows duplicate conditions in if-else chains. |
37-
| [no-excessive-nested-conditionals](docs/no-excessive-nested-conditionals.md) | This rule disallows excessive nesting of conditionals in order to improve code readability. |
38-
| [no-long-else-if-chains](docs/no-long-else-if-chains.md) | This rule limits the number of consecutive else-if statements. |
39-
| [no-nested-ternary-operators](docs/no-nested-ternary-operators.md) | This rule disallows nested ternary operators. |
40-
| [no-useless-ternary](docs/no-useless-ternary.md) | This rule disallows unnecessary ternary expressions. |
41-
| [prefer-early-return](docs/prefer-early-return.md) | This rule encourages the use of early returns instead of deeply nested if-else blocks. |
42-
| [require-default-in-switch](docs/require-default-in-switch.md) | This rule ensures that switch statements include a default case and/or its not empty. |
4334

35+
This plugin enforces the following opinionated best practices:
36+
37+
| Name                                        | Description |
38+
| :--------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ |
39+
| [no-constant-conditionals](docs/no-constant-conditionals.md) | This rule disallows conditionals that always evaluate to true or false. |
40+
| [no-duplicated-conditions](docs/no-duplicated-conditions.md) | This rule disallows duplicate conditions in if-else chains. |
41+
| [no-excessive-nested-conditionals](docs/no-excessive-nested-conditionals.md) | This rule disallows excessive nesting of conditionals in order to improve code readability. |
42+
| [no-long-else-if-chains](docs/no-long-else-if-chains.md) | This rule limits the number of consecutive else-if statements. |
43+
| [no-nested-ternary-operators](docs/no-nested-ternary-operators.md) | This rule disallows nested ternary operators. |
44+
| [no-useless-ternary](docs/no-useless-ternary.md) | This rule disallows unnecessary ternary expressions. |
45+
| [prefer-early-return](docs/prefer-early-return.md) | This rule encourages the use of early returns instead of deeply nested if-else blocks. |
46+
| [require-default-in-switch](docs/require-default-in-switch.md) | This rule ensures that switch statements include a default case and/or its not empty. |
47+
| [no-eval](docs/no-eval.md) | This rule disallows the use of eval() due to security risks. |
4448

4549
# Autofixing
4650

docs/no-constant-conditionals.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# no-constant-conditionals
22

3-
43
## Description
54

65
This rule disallows conditionals that always evaluate to true or false.
76

87
## Options
8+
99
This rule does not accept any options.
1010

1111
## Severity
12+
1213
- Type: Problem
1314
- Recommended: Yes
1415

@@ -17,15 +18,23 @@ This rule does not accept any options.
1718
### **Invalid** 👎
1819

1920
```js
20-
if (true) { sayHi(); }
21+
if (true) {
22+
sayHi();
23+
}
2124
```
2225

2326
```js
24-
if (false) { sayHi(); }
27+
if (false) {
28+
sayHi();
29+
}
2530
```
2631

2732
### **Valid** 👍
2833

2934
```js
30-
const name = "andreas"; const x = "hello"; if (x === name) { sayHi(); }
31-
```
35+
const name = 'andreas';
36+
const x = 'hello';
37+
if (x === name) {
38+
sayHi();
39+
}
40+
```

docs/no-duplicated-conditions.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# no-duplicated-conditions
22

3-
43
## Description
54

65
This rule disallows duplicate conditions in if-else chains.
76

87
## Options
8+
99
This rule does not accept any options.
1010

1111
## Severity
12+
1213
- Type: Problem
1314
- Recommended: Yes
1415

@@ -17,11 +18,15 @@ This rule does not accept any options.
1718
### **Invalid** 👎
1819

1920
```js
20-
if (a) {} else if (a) {}
21+
if (a) {
22+
} else if (a) {
23+
}
2124
```
2225

2326
### **Valid** 👍
2427

2528
```js
26-
if (a) {} else if (b) {}
27-
```
29+
if (a) {
30+
} else if (b) {
31+
}
32+
```

docs/no-eval.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# no-eval
2+
3+
## Description
4+
5+
Disallow use of eval() due to security risks.
6+
7+
## Options
8+
9+
This rule does not accept any options.
10+
11+
## Severity
12+
13+
- Type: Problem
14+
- Recommended: Yes
15+
16+
## Examples
17+
18+
### **Invalid** 👎
19+
20+
```js
21+
eval(console.log('Hi'));
22+
```
23+
24+
```js
25+
window['eval'](console.log('Not Allowed'));
26+
```
27+
28+
```js
29+
globalThis['eval'](console.log('Not Allowed'));
30+
```
31+
32+
```js
33+
const exec = eval;
34+
exec(console.log('Not Allowed'));
35+
```
36+
37+
```js
38+
const safeEval = eval;
39+
safeEval(console.log('Not Allowed'));
40+
```
Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# no-excessive-nested-conditionals
22

3-
43
## Description
54

65
This rule disallows excessive nesting of conditionals in order to improve code readability.
@@ -9,15 +8,16 @@ This rule disallows excessive nesting of conditionals in order to improve code r
98

109
```json
1110
{
12-
"rules": {
13-
"@andreasnicolaou/conditional-best-practices/no-excessive-nested-conditionals": ["warn", { "max": 2 }]
14-
}
11+
"rules": {
12+
"@andreasnicolaou/conditional-best-practices/no-excessive-nested-conditionals": ["warn", { "max": 2 }]
13+
}
1514
}
1615
```
1716

1817
- max (optional): The maximum depth of nested conditionals. The default value is 3.
1918

2019
## Severity
20+
2121
- Type: Suggestion
2222
- Recommended: Yes
2323

@@ -26,33 +26,53 @@ This rule disallows excessive nesting of conditionals in order to improve code r
2626
### **Invalid** 👎
2727

2828
```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) {};
29+
if (a) {
30+
if (b) {
31+
if (c) {
32+
if (d) {
33+
doSomething();
34+
}
35+
}
36+
}
37+
} else if (b) {
38+
} else if (c) {
39+
} else if (d) {
40+
}
3641
```
3742

3843
```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) {};
44+
if (a) {
45+
if (a) {
46+
if (b) {
47+
if (c) {
48+
if (d) {
49+
doSomething();
50+
}
51+
}
52+
}
53+
}
54+
} else if (b) {
55+
} else if (c) {
56+
} else if (d) {
57+
}
4858
```
4959

5060
### **Valid** 👍
5161

5262
```js
53-
if (a) { if (b) { if (c) { doSomething(); } } }
63+
if (a) {
64+
if (b) {
65+
if (c) {
66+
doSomething();
67+
}
68+
}
69+
}
5470
```
5571

5672
```js
57-
if (a) {} else if (b) {} else if (c) {} else if (d) {}
58-
```
73+
if (a) {
74+
} else if (b) {
75+
} else if (c) {
76+
} else if (d) {
77+
}
78+
```

0 commit comments

Comments
 (0)