eslint-plugin-hex-under 1.11.0 → 1.11.1
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 +174 -74
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -2,98 +2,171 @@
|
|
|
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
|
|
50
|
+
|
|
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': [
|
|
64
|
+
'error',
|
|
65
|
+
{ limit: 255, checkBigInt: true },
|
|
66
|
+
],
|
|
67
|
+
'hex-under/octal-under': [
|
|
68
|
+
'error',
|
|
69
|
+
{ limit: 511, checkBigInt: true },
|
|
70
|
+
],
|
|
71
|
+
'hex-under/binary-under': [
|
|
72
|
+
'error',
|
|
73
|
+
{ limit: 15, checkBigInt: true },
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
```
|
|
35
79
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
80
|
+
You can enable only the formats you need.
|
|
81
|
+
|
|
82
|
+
For example:
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
rules: {
|
|
86
|
+
'hex-under/hex-under': ['error', { limit: 255 }],
|
|
87
|
+
}
|
|
88
|
+
```
|
|
40
89
|
|
|
41
90
|
### Examples
|
|
42
91
|
|
|
43
|
-
|
|
92
|
+
Valid with default limits
|
|
44
93
|
|
|
45
94
|
```js
|
|
46
|
-
const signal = 0xef; //
|
|
95
|
+
const signal = 0xef; // 239
|
|
47
96
|
|
|
48
|
-
|
|
97
|
+
const func = () => 0xab; // 171
|
|
49
98
|
|
|
50
99
|
function add(a, b) {
|
|
51
|
-
return a + b + 0x1f;
|
|
100
|
+
return a + b + 0x1f; // 31
|
|
52
101
|
}
|
|
53
102
|
|
|
54
|
-
const binary = 0b1111;
|
|
103
|
+
const binary = 0b1111; // 15
|
|
104
|
+
|
|
105
|
+
const octal = 0o377; // 255
|
|
106
|
+
|
|
107
|
+
Invalid with default limits
|
|
108
|
+
const signal = 0x21b; // 539
|
|
109
|
+
|
|
110
|
+
const func = () => 0xabc; // 2748
|
|
55
111
|
|
|
56
|
-
|
|
112
|
+
function add(a, b) {
|
|
113
|
+
return a + b + 0x100; // 256
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const d = 0xaa_ffn;
|
|
117
|
+
|
|
118
|
+
const binary = 0b1_0000_0000; // 256
|
|
119
|
+
|
|
120
|
+
const octal = 0o1000; // 512
|
|
57
121
|
```
|
|
58
122
|
|
|
59
|
-
|
|
123
|
+
### Auto-fix
|
|
124
|
+
|
|
125
|
+
The rules are automatically fixable with ESLint's --fix option.
|
|
126
|
+
|
|
127
|
+
For example:
|
|
60
128
|
|
|
61
129
|
```js
|
|
62
130
|
const signal = 0x21b;
|
|
63
131
|
|
|
64
|
-
|
|
132
|
+
const func = () => 0xabc;
|
|
65
133
|
|
|
66
134
|
function add(a, b) {
|
|
67
135
|
return a + b + 0x100;
|
|
68
136
|
}
|
|
69
137
|
|
|
70
|
-
let d = 0xaa_ffn;
|
|
71
|
-
|
|
72
138
|
const binary = 0b1_0000_0000;
|
|
73
139
|
|
|
74
140
|
const octal = 0o1000;
|
|
75
141
|
```
|
|
76
142
|
|
|
77
|
-
|
|
143
|
+
Running:
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
eslint . --fix
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
converts the values to decimal:
|
|
78
150
|
|
|
79
151
|
```js
|
|
80
|
-
// This can be transformed to:
|
|
81
152
|
const signal = 539;
|
|
82
153
|
|
|
83
|
-
|
|
154
|
+
const func = () => 2748;
|
|
84
155
|
|
|
85
156
|
function add(a, b) {
|
|
86
157
|
return a + b + 256;
|
|
87
158
|
}
|
|
88
159
|
|
|
89
|
-
let d = 43775;
|
|
90
|
-
|
|
91
160
|
const binary = 256;
|
|
92
161
|
|
|
93
162
|
const octal = 512;
|
|
94
163
|
```
|
|
95
164
|
|
|
96
|
-
|
|
165
|
+
The source code is not modified during normal linting. Conversion only happens when ESLint's auto-fix functionality is used.
|
|
166
|
+
|
|
167
|
+
### Ignoring individual literals
|
|
168
|
+
|
|
169
|
+
You can disable a rule for a specific line using ESLint's standard inline comments:
|
|
97
170
|
|
|
98
171
|
```js
|
|
99
172
|
// eslint-disable-next-line hex-under/hex-under
|
|
@@ -106,76 +179,103 @@ const binTooBig = 0b1000_0000_0000;
|
|
|
106
179
|
const octalTooBig = 0o777777;
|
|
107
180
|
```
|
|
108
181
|
|
|
109
|
-
|
|
182
|
+
### BigInt
|
|
183
|
+
|
|
184
|
+
BigInt literals can optionally be checked using the checkBigInt option.
|
|
185
|
+
|
|
186
|
+
By default:
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
checkBigInt: true
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
For example:
|
|
110
193
|
|
|
111
194
|
```js
|
|
112
|
-
// valid with { limit: 255, checkBigInt: false }
|
|
113
195
|
const mask = 0xdead_beefn;
|
|
114
196
|
```
|
|
115
197
|
|
|
116
|
-
|
|
198
|
+
With checkBigInt: true, this literal is checked against the configured limit.
|
|
117
199
|
|
|
118
|
-
|
|
200
|
+
If you don't want BigInt literals to be checked, set:
|
|
119
201
|
|
|
120
|
-
```
|
|
121
|
-
|
|
202
|
+
```js
|
|
203
|
+
checkBigInt: false
|
|
122
204
|
```
|
|
123
205
|
|
|
206
|
+
For example:
|
|
207
|
+
|
|
124
208
|
```js
|
|
125
|
-
|
|
209
|
+
rules: {
|
|
210
|
+
'hex-under/hex-under': [
|
|
211
|
+
'error',
|
|
212
|
+
{
|
|
213
|
+
limit: 255,
|
|
214
|
+
checkBigInt: false,
|
|
215
|
+
},
|
|
216
|
+
],
|
|
217
|
+
}
|
|
218
|
+
```
|
|
126
219
|
|
|
127
|
-
|
|
220
|
+
This allows:
|
|
128
221
|
|
|
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
|
-
];
|
|
222
|
+
```js
|
|
223
|
+
const mask = 0xdead_beefn;
|
|
142
224
|
```
|
|
143
225
|
|
|
144
226
|
## Rules
|
|
145
227
|
|
|
146
|
-
| Rule
|
|
147
|
-
|
|
148
|
-
|
|
|
149
|
-
|
|
|
150
|
-
|
|
|
228
|
+
| Rule | Description
|
|
229
|
+
|---|---
|
|
230
|
+
| hex-under/hex-under | Limits hexadecimal numeric literals
|
|
231
|
+
| hex-under/binary-under | Limits binary numeric literals
|
|
232
|
+
| hex-under/octal-under | Limits octal numeric literals
|
|
151
233
|
|
|
152
|
-
|
|
234
|
+
Each rule can be configured independently.
|
|
235
|
+
|
|
236
|
+
## Options
|
|
237
|
+
|
|
238
|
+
| Option | Type | Default | Description
|
|
239
|
+
|---|---|---|---
|
|
240
|
+
| limit | number | Format-specific | Maximum allowed numeric value
|
|
241
|
+
|checkBigInt | boolean | true | Whether BigInt literals should be checked
|
|
153
242
|
|
|
154
|
-
|
|
155
|
-
| ------------- | ------- | --------------- | --------------------- |
|
|
156
|
-
| `limit` | number | format-specific | Maximum allowed value |
|
|
157
|
-
| `checkBigInt` | boolean | true | Check BigInt values |
|
|
243
|
+
The limit is inclusive. A literal equal to the limit is valid; a literal greater than the limit is reported.
|
|
158
244
|
|
|
159
245
|
## Testing & Code Coverage
|
|
160
246
|
|
|
161
|
-
This project uses
|
|
162
|
-
Additionally this project uses bats to test the `eslint --fix` command's output.
|
|
247
|
+
This project uses Vitest as its test runner and ESLint's RuleTester for validating rule behavior.
|
|
163
248
|
|
|
164
|
-
|
|
249
|
+
The project also uses bats to test the output of ESLint's --fix command.
|
|
165
250
|
|
|
166
|
-
|
|
167
|
-
|
|
251
|
+
## Running tests
|
|
252
|
+
|
|
253
|
+
### Run all tests:
|
|
254
|
+
|
|
255
|
+
```bash
|
|
168
256
|
npm run test:all
|
|
257
|
+
```
|
|
169
258
|
|
|
170
|
-
|
|
259
|
+
### Run Vitest:
|
|
260
|
+
|
|
261
|
+
```bash
|
|
171
262
|
npm run test
|
|
263
|
+
```
|
|
172
264
|
|
|
173
|
-
|
|
265
|
+
### Run Vitest in watch mode:
|
|
266
|
+
|
|
267
|
+
```bash
|
|
174
268
|
npm run test:watch
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Run tests with coverage:
|
|
175
272
|
|
|
176
|
-
|
|
273
|
+
```bash
|
|
177
274
|
npm run coverage
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### Run bats tests:
|
|
178
278
|
|
|
179
|
-
|
|
279
|
+
```bash
|
|
180
280
|
npm run test:bats
|
|
181
281
|
```
|
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.1",
|
|
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",
|