eslint-plugin-hex-under 1.7.5 → 1.7.7
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 +84 -32
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,29 +1,47 @@
|
|
|
1
1
|
# eslint-plugin-hex-under
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Enforce readability by limiting non-decimal numeric literals (hex, binary, octal) in JavaScript.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Automatically converts large non-decimal literals into readable decimal values or reports them as errors.
|
|
6
6
|
|
|
7
7
|
## Why?
|
|
8
8
|
|
|
9
|
-
Numeric literals like `0xfff` or `0b101010101` are compact, but often hard to read and reason about.
|
|
9
|
+
Numeric literals like `0xfff` or `0b101010101` are compact, but often hard to read and reason about—especially for developers unfamiliar with bitwise operations.
|
|
10
|
+
|
|
11
|
+
This can lead to:
|
|
12
|
+
|
|
13
|
+
- Reduced code readability
|
|
14
|
+
- Slower code reviews
|
|
15
|
+
- Hidden "magic numbers"
|
|
16
|
+
|
|
17
|
+
This plugin enforces limits to keep numeric literals understandable at a glance.
|
|
10
18
|
|
|
11
19
|
## hex-under
|
|
12
20
|
|
|
13
|
-
This ESLint plugin ensures that numeric literals written in non-decimal formats (hexadecimal, binary, or octal) do not exceed a specified maximum value.
|
|
21
|
+
This ESLint plugin ensures that numeric literals written in non-decimal formats (hexadecimal, binary, or octal) do not exceed a specified maximum value.
|
|
22
|
+
By default, the limits are:
|
|
23
|
+
|
|
24
|
+
- Hexadecimal: `0xff` (255)
|
|
25
|
+
- Binary: `0b1111` (15)
|
|
26
|
+
- Octal: `0o777` (511)
|
|
27
|
+
|
|
28
|
+
Values exceeding these limits are automatically converted to decimal.
|
|
14
29
|
|
|
15
30
|
## When should I use this?
|
|
16
31
|
|
|
17
32
|
Use this plugin if:
|
|
18
|
-
- You care about code readability
|
|
19
|
-
- Your team avoids "magic numbers"
|
|
20
|
-
- You review low-level or bitwise-heavy code
|
|
21
33
|
|
|
22
|
-
|
|
34
|
+
- You want to improve code readability in your codebase
|
|
35
|
+
- Your team avoids hard-to-read numeric literals ("magic numbers")
|
|
36
|
+
- You work with bitwise operations but want to keep them understandable
|
|
37
|
+
- You review code where non-decimal formats are frequently used
|
|
38
|
+
|
|
39
|
+
### Examples
|
|
40
|
+
|
|
41
|
+
#### valid with default limits
|
|
23
42
|
|
|
24
43
|
```js
|
|
25
|
-
//
|
|
26
|
-
const signal = 0xef;
|
|
44
|
+
const signal = 0xef; // OK: below default hex limit (255)
|
|
27
45
|
|
|
28
46
|
let func = () => 0xab;
|
|
29
47
|
|
|
@@ -34,18 +52,11 @@ function add(a, b) {
|
|
|
34
52
|
const binary = 0b1111_1111;
|
|
35
53
|
|
|
36
54
|
const octal = 0o377;
|
|
55
|
+
```
|
|
37
56
|
|
|
38
|
-
|
|
39
|
-
// ignore-hex-under
|
|
40
|
-
const hexTooBig = 0xfffff;
|
|
41
|
-
const binTooBig = 0b1000_0000_0000; // ignore-binary-under
|
|
42
|
-
// ignore-octal-under
|
|
43
|
-
const octalTooBig = 0o777777;
|
|
44
|
-
|
|
45
|
-
// valid with { limit: 255, skipBigInt: true }
|
|
46
|
-
const mask = 0xdead_beefn;
|
|
57
|
+
#### Invalid with default limits
|
|
47
58
|
|
|
48
|
-
|
|
59
|
+
```js
|
|
49
60
|
const signal = 0x21b;
|
|
50
61
|
|
|
51
62
|
let func = () => 0xabc;
|
|
@@ -59,7 +70,11 @@ let d = 0xaa_ffn;
|
|
|
59
70
|
const binary = 0b1_0000_0000;
|
|
60
71
|
|
|
61
72
|
const octal = 0o400;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### Auto-fixable
|
|
62
76
|
|
|
77
|
+
```js
|
|
63
78
|
// This can be transformed to:
|
|
64
79
|
const signal = 539;
|
|
65
80
|
|
|
@@ -76,31 +91,52 @@ const binary = 256;
|
|
|
76
91
|
const octal = 256;
|
|
77
92
|
```
|
|
78
93
|
|
|
79
|
-
|
|
94
|
+
#### Ignore with line comments
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
// ignore-hex-under
|
|
98
|
+
const hexTooBig = 0xfffff;
|
|
99
|
+
|
|
100
|
+
const binTooBig = 0b1000_0000_0000; // ignore-binary-under
|
|
101
|
+
|
|
102
|
+
// ignore-octal-under
|
|
103
|
+
const octalTooBig = 0o777777;
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### Ignore Bigint values
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
// valid with { limit: 255, skipBigInt: true }
|
|
110
|
+
const mask = 0xdead_beefn;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### Disable rules globally
|
|
80
114
|
|
|
81
115
|
```js
|
|
82
116
|
/* ignore-all-hex-under */
|
|
83
117
|
|
|
84
|
-
//
|
|
118
|
+
// ignore-all-hex-under must be the very first line and a block comment.
|
|
119
|
+
// This will ignore all of the whole file.
|
|
85
120
|
const foo = 0xffff;
|
|
86
|
-
|
|
87
121
|
const bar = 0b10100010101;
|
|
88
122
|
```
|
|
89
123
|
|
|
90
|
-
|
|
124
|
+
#### Disable specific formats
|
|
91
125
|
|
|
92
126
|
```js
|
|
93
|
-
// This should
|
|
127
|
+
// This should ignore all hex numbers but not octal or binary numbers.
|
|
94
128
|
|
|
95
129
|
/* ignore-hex-under */
|
|
96
130
|
|
|
97
|
-
const hex = 0x100; //
|
|
98
|
-
const octal = 0o1000; //
|
|
99
|
-
const binary = 0b10000; //
|
|
131
|
+
const hex = 0x100; // stays 0x100
|
|
132
|
+
const octal = 0o1000; // fixed to 512
|
|
133
|
+
const binary = 0b10000; // fixed to 16
|
|
100
134
|
```
|
|
101
135
|
|
|
102
136
|
## Integration
|
|
103
137
|
|
|
138
|
+
Requires ESLint v9+ (flat config)
|
|
139
|
+
|
|
104
140
|
```sh
|
|
105
141
|
npm install --save-dev eslint-plugin-hex-under
|
|
106
142
|
```
|
|
@@ -125,12 +161,28 @@ export default [
|
|
|
125
161
|
];
|
|
126
162
|
```
|
|
127
163
|
|
|
164
|
+
## Rules
|
|
165
|
+
|
|
166
|
+
| Rule | Description |
|
|
167
|
+
| ------------------------ | --------------------------- |
|
|
168
|
+
| `hex-under/hex-under` | Limits hexadecimal literals |
|
|
169
|
+
| `hex-under/binary-under` | Limits binary literals |
|
|
170
|
+
| `hex-under/octal-under` | Limits octal literals |
|
|
171
|
+
|
|
128
172
|
## Configuration
|
|
129
173
|
|
|
130
|
-
Option | Type | Default
|
|
131
|
-
|
|
132
|
-
`limit` | number | format-specific
|
|
133
|
-
`skipBigInt` | boolean | false
|
|
174
|
+
| Option | Type | Default | Description |
|
|
175
|
+
| ------------ | ------- | --------------- | --------------------- |
|
|
176
|
+
| `limit` | number | format-specific | Maximum allowed value |
|
|
177
|
+
| `skipBigInt` | boolean | false | Ignore BigInt values |
|
|
178
|
+
|
|
179
|
+
### Ignoring rules
|
|
180
|
+
|
|
181
|
+
You can disable rules using inline comments:
|
|
182
|
+
|
|
183
|
+
- `ignore-hex-under`
|
|
184
|
+
- `ignore-binary-under`
|
|
185
|
+
- `ignore-octal-under`
|
|
134
186
|
|
|
135
187
|
## Testing & Code Coverage
|
|
136
188
|
|
package/package.json
CHANGED