eslint-plugin-hex-under 1.11.0 → 1.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +165 -74
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -2,98 +2,162 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/2nd-Labs/eslint-plugin-hex-under/actions/workflows/build-and-test.yml)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
An ESLint plugin that keeps hexadecimal, binary, and octal numeric literals readable by enforcing configurable value limits.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
When a non-decimal numeric literal exceeds its configured limit, the rule reports an error and provides an automatic ESLint fix that converts the value to decimal.
|
|
8
8
|
|
|
9
9
|
## Why?
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Non-decimal numeric literals such as 0xfff or 0b101010101 can be compact, but they are not always easy to read or reason about at a glance.
|
|
12
12
|
|
|
13
13
|
This can lead to:
|
|
14
14
|
|
|
15
15
|
- Reduced code readability
|
|
16
16
|
- Slower code reviews
|
|
17
|
-
-
|
|
17
|
+
- Hard-to-understand magic numbers
|
|
18
|
+
- Unnecessary cognitive overhead when reading bitwise operations
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
`eslint-plugin-hex-under` lets you keep smaller, meaningful non-decimal literals while encouraging decimal notation for larger values.
|
|
20
21
|
|
|
21
|
-
##
|
|
22
|
+
## How it works
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
By default, the limits are:
|
|
24
|
+
The plugin provides three independent rules:
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
-
|
|
26
|
+
| Rule | Format | Default limit |
|
|
27
|
+
| ---------------------- | ----------- | ------------- |
|
|
28
|
+
| hex-under/hex-under | Hexadecimal | 0xff (255) |
|
|
29
|
+
| hex-under/binary-under | Binary | 0b1111 (15) |
|
|
30
|
+
| hex-under/octal-under | Octal | 0o777 (511) |
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
The configured limit is inclusive.
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
For example, with the default hexadecimal limit of 255:
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
```js
|
|
37
|
+
const a = 0xff; // OK: 255
|
|
38
|
+
const b = 0x100; // Error: 256
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
Requires ESLint v9+ with flat config.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install --save-dev eslint-plugin-hex-under
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
35
50
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
Add the plugin to your eslint.config.js:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import eslintPluginHexUnder from 'eslint-plugin-hex-under';
|
|
55
|
+
|
|
56
|
+
export default [
|
|
57
|
+
{
|
|
58
|
+
files: ['**/*.js'],
|
|
59
|
+
plugins: {
|
|
60
|
+
'hex-under': eslintPluginHexUnder,
|
|
61
|
+
},
|
|
62
|
+
rules: {
|
|
63
|
+
'hex-under/hex-under': ['error', { limit: 255, checkBigInt: true }],
|
|
64
|
+
'hex-under/octal-under': ['error', { limit: 511, checkBigInt: true }],
|
|
65
|
+
'hex-under/binary-under': ['error', { limit: 15, checkBigInt: true }],
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
You can enable only the formats you need.
|
|
72
|
+
|
|
73
|
+
For example:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
rules: {
|
|
77
|
+
'hex-under/hex-under': ['error', { limit: 255 }],
|
|
78
|
+
}
|
|
79
|
+
```
|
|
40
80
|
|
|
41
81
|
### Examples
|
|
42
82
|
|
|
43
|
-
|
|
83
|
+
Valid with default limits
|
|
44
84
|
|
|
45
85
|
```js
|
|
46
|
-
const signal = 0xef; //
|
|
86
|
+
const signal = 0xef; // 239
|
|
87
|
+
|
|
88
|
+
const func = () => 0xab; // 171
|
|
89
|
+
|
|
90
|
+
function add(a, b) {
|
|
91
|
+
return a + b + 0x1f; // 31
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const binary = 0b1111; // 15
|
|
47
95
|
|
|
48
|
-
|
|
96
|
+
const octal = 0o377; // 255
|
|
97
|
+
|
|
98
|
+
Invalid with default limits
|
|
99
|
+
const signal = 0x21b; // 539
|
|
100
|
+
|
|
101
|
+
const func = () => 0xabc; // 2748
|
|
49
102
|
|
|
50
103
|
function add(a, b) {
|
|
51
|
-
return a + b +
|
|
104
|
+
return a + b + 0x100; // 256
|
|
52
105
|
}
|
|
53
106
|
|
|
54
|
-
const
|
|
107
|
+
const d = 0xaa_ffn;
|
|
108
|
+
|
|
109
|
+
const binary = 0b1_0000_0000; // 256
|
|
55
110
|
|
|
56
|
-
const octal =
|
|
111
|
+
const octal = 0o1000; // 512
|
|
57
112
|
```
|
|
58
113
|
|
|
59
|
-
|
|
114
|
+
### Auto-fix
|
|
115
|
+
|
|
116
|
+
The rules are automatically fixable with ESLint's --fix option.
|
|
117
|
+
|
|
118
|
+
For example:
|
|
60
119
|
|
|
61
120
|
```js
|
|
62
121
|
const signal = 0x21b;
|
|
63
122
|
|
|
64
|
-
|
|
123
|
+
const func = () => 0xabc;
|
|
65
124
|
|
|
66
125
|
function add(a, b) {
|
|
67
126
|
return a + b + 0x100;
|
|
68
127
|
}
|
|
69
128
|
|
|
70
|
-
let d = 0xaa_ffn;
|
|
71
|
-
|
|
72
129
|
const binary = 0b1_0000_0000;
|
|
73
130
|
|
|
74
131
|
const octal = 0o1000;
|
|
75
132
|
```
|
|
76
133
|
|
|
77
|
-
|
|
134
|
+
Running:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
eslint . --fix
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
converts the values to decimal:
|
|
78
141
|
|
|
79
142
|
```js
|
|
80
|
-
// This can be transformed to:
|
|
81
143
|
const signal = 539;
|
|
82
144
|
|
|
83
|
-
|
|
145
|
+
const func = () => 2748;
|
|
84
146
|
|
|
85
147
|
function add(a, b) {
|
|
86
148
|
return a + b + 256;
|
|
87
149
|
}
|
|
88
150
|
|
|
89
|
-
let d = 43775;
|
|
90
|
-
|
|
91
151
|
const binary = 256;
|
|
92
152
|
|
|
93
153
|
const octal = 512;
|
|
94
154
|
```
|
|
95
155
|
|
|
96
|
-
|
|
156
|
+
The source code is not modified during normal linting. Conversion only happens when ESLint's auto-fix functionality is used.
|
|
157
|
+
|
|
158
|
+
### Ignoring individual literals
|
|
159
|
+
|
|
160
|
+
You can disable a rule for a specific line using ESLint's standard inline comments:
|
|
97
161
|
|
|
98
162
|
```js
|
|
99
163
|
// eslint-disable-next-line hex-under/hex-under
|
|
@@ -106,76 +170,103 @@ const binTooBig = 0b1000_0000_0000;
|
|
|
106
170
|
const octalTooBig = 0o777777;
|
|
107
171
|
```
|
|
108
172
|
|
|
109
|
-
|
|
173
|
+
### BigInt
|
|
174
|
+
|
|
175
|
+
BigInt literals can optionally be checked using the checkBigInt option.
|
|
176
|
+
|
|
177
|
+
By default:
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
checkBigInt: true;
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
For example:
|
|
110
184
|
|
|
111
185
|
```js
|
|
112
|
-
// valid with { limit: 255, checkBigInt: false }
|
|
113
186
|
const mask = 0xdead_beefn;
|
|
114
187
|
```
|
|
115
188
|
|
|
116
|
-
|
|
189
|
+
With checkBigInt: true, this literal is checked against the configured limit.
|
|
117
190
|
|
|
118
|
-
|
|
191
|
+
If you don't want BigInt literals to be checked, set:
|
|
119
192
|
|
|
120
|
-
```
|
|
121
|
-
|
|
193
|
+
```js
|
|
194
|
+
checkBigInt: false;
|
|
122
195
|
```
|
|
123
196
|
|
|
197
|
+
For example:
|
|
198
|
+
|
|
124
199
|
```js
|
|
125
|
-
|
|
200
|
+
rules: {
|
|
201
|
+
'hex-under/hex-under': [
|
|
202
|
+
'error',
|
|
203
|
+
{
|
|
204
|
+
limit: 255,
|
|
205
|
+
checkBigInt: false,
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
}
|
|
209
|
+
```
|
|
126
210
|
|
|
127
|
-
|
|
211
|
+
This allows:
|
|
128
212
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
files: ['*.js'],
|
|
132
|
-
plugins: {
|
|
133
|
-
'hex-under': eslintPluginHexUnder,
|
|
134
|
-
},
|
|
135
|
-
rules: {
|
|
136
|
-
'hex-under/hex-under': ['error', { limit: 255, checkBigInt: true }],
|
|
137
|
-
'hex-under/octal-under': ['error', { limit: 511, checkBigInt: true }],
|
|
138
|
-
'hex-under/binary-under': ['error', { limit: 15, checkBigInt: true }],
|
|
139
|
-
},
|
|
140
|
-
},
|
|
141
|
-
];
|
|
213
|
+
```js
|
|
214
|
+
const mask = 0xdead_beefn;
|
|
142
215
|
```
|
|
143
216
|
|
|
144
217
|
## Rules
|
|
145
218
|
|
|
146
|
-
| Rule
|
|
147
|
-
|
|
|
148
|
-
|
|
|
149
|
-
|
|
|
150
|
-
|
|
|
219
|
+
| Rule | Description |
|
|
220
|
+
| ---------------------- | ----------------------------------- |
|
|
221
|
+
| hex-under/hex-under | Limits hexadecimal numeric literals |
|
|
222
|
+
| hex-under/binary-under | Limits binary numeric literals |
|
|
223
|
+
| hex-under/octal-under | Limits octal numeric literals |
|
|
151
224
|
|
|
152
|
-
|
|
225
|
+
Each rule can be configured independently.
|
|
226
|
+
|
|
227
|
+
## Options
|
|
153
228
|
|
|
154
|
-
| Option
|
|
155
|
-
|
|
|
156
|
-
|
|
|
157
|
-
|
|
|
229
|
+
| Option | Type | Default | Description |
|
|
230
|
+
| ----------- | ------- | --------------- | ----------------------------------------- |
|
|
231
|
+
| limit | number | Format-specific | Maximum allowed numeric value |
|
|
232
|
+
| checkBigInt | boolean | true | Whether BigInt literals should be checked |
|
|
233
|
+
|
|
234
|
+
The limit is inclusive. A literal equal to the limit is valid; a literal greater than the limit is reported.
|
|
158
235
|
|
|
159
236
|
## Testing & Code Coverage
|
|
160
237
|
|
|
161
|
-
This project uses
|
|
162
|
-
|
|
238
|
+
This project uses Vitest as its test runner and ESLint's RuleTester for validating rule behavior.
|
|
239
|
+
|
|
240
|
+
The project also uses bats to test the output of ESLint's --fix command.
|
|
241
|
+
|
|
242
|
+
## Running tests
|
|
163
243
|
|
|
164
|
-
###
|
|
244
|
+
### Run all tests:
|
|
165
245
|
|
|
166
|
-
```
|
|
167
|
-
# Run all tests once
|
|
246
|
+
```bash
|
|
168
247
|
npm run test:all
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Run Vitest:
|
|
169
251
|
|
|
170
|
-
|
|
252
|
+
```bash
|
|
171
253
|
npm run test
|
|
254
|
+
```
|
|
172
255
|
|
|
173
|
-
|
|
256
|
+
### Run Vitest in watch mode:
|
|
257
|
+
|
|
258
|
+
```bash
|
|
174
259
|
npm run test:watch
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Run tests with coverage:
|
|
175
263
|
|
|
176
|
-
|
|
264
|
+
```bash
|
|
177
265
|
npm run coverage
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Run bats tests:
|
|
178
269
|
|
|
179
|
-
|
|
270
|
+
```bash
|
|
180
271
|
npm run test:bats
|
|
181
272
|
```
|
package/package.json
CHANGED
|
@@ -2,18 +2,18 @@
|
|
|
2
2
|
"author": "0xflotus",
|
|
3
3
|
"description": "This ESLint rule proves that hex numbers are less than a specified value.",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "1.11.
|
|
5
|
+
"version": "1.11.2",
|
|
6
6
|
"devDependencies": {
|
|
7
7
|
"@eslint/js": "10.0.1",
|
|
8
8
|
"@eslint/json": "2.1.0",
|
|
9
|
-
"@vitest/coverage-istanbul": "
|
|
9
|
+
"@vitest/coverage-istanbul": "5.0.0",
|
|
10
10
|
"@vitest/eslint-plugin": "1.6.27",
|
|
11
11
|
"bats": "1.13.0",
|
|
12
|
-
"eslint": "10.
|
|
12
|
+
"eslint": "10.10.0",
|
|
13
13
|
"eslint-plugin-eslint-plugin": "7.6.2",
|
|
14
|
-
"eslint-vitest-rule-tester": "3.
|
|
14
|
+
"eslint-vitest-rule-tester": "3.2.0",
|
|
15
15
|
"prettier": "3.9.6",
|
|
16
|
-
"vitest": "
|
|
16
|
+
"vitest": "5.0.0"
|
|
17
17
|
},
|
|
18
18
|
"engines": {
|
|
19
19
|
"node": ">=24",
|