eslint-plugin-hex-under 1.7.6 → 1.7.8
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 +79 -28
- package/package.json +1 -1
- package/src/eslint-plugin-hex-under.js +17 -0
package/README.md
CHANGED
|
@@ -1,30 +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
33
|
|
|
19
|
-
- You
|
|
20
|
-
- Your team avoids "magic numbers"
|
|
21
|
-
- You
|
|
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
|
|
22
40
|
|
|
23
|
-
|
|
41
|
+
#### valid with default limits
|
|
24
42
|
|
|
25
43
|
```js
|
|
26
|
-
//
|
|
27
|
-
const signal = 0xef;
|
|
44
|
+
const signal = 0xef; // OK: below default hex limit (255)
|
|
28
45
|
|
|
29
46
|
let func = () => 0xab;
|
|
30
47
|
|
|
@@ -35,18 +52,11 @@ function add(a, b) {
|
|
|
35
52
|
const binary = 0b1111_1111;
|
|
36
53
|
|
|
37
54
|
const octal = 0o377;
|
|
55
|
+
```
|
|
38
56
|
|
|
39
|
-
|
|
40
|
-
// ignore-hex-under
|
|
41
|
-
const hexTooBig = 0xfffff;
|
|
42
|
-
const binTooBig = 0b1000_0000_0000; // ignore-binary-under
|
|
43
|
-
// ignore-octal-under
|
|
44
|
-
const octalTooBig = 0o777777;
|
|
45
|
-
|
|
46
|
-
// valid with { limit: 255, skipBigInt: true }
|
|
47
|
-
const mask = 0xdead_beefn;
|
|
57
|
+
#### Invalid with default limits
|
|
48
58
|
|
|
49
|
-
|
|
59
|
+
```js
|
|
50
60
|
const signal = 0x21b;
|
|
51
61
|
|
|
52
62
|
let func = () => 0xabc;
|
|
@@ -60,7 +70,11 @@ let d = 0xaa_ffn;
|
|
|
60
70
|
const binary = 0b1_0000_0000;
|
|
61
71
|
|
|
62
72
|
const octal = 0o400;
|
|
73
|
+
```
|
|
63
74
|
|
|
75
|
+
#### Auto-fixable
|
|
76
|
+
|
|
77
|
+
```js
|
|
64
78
|
// This can be transformed to:
|
|
65
79
|
const signal = 539;
|
|
66
80
|
|
|
@@ -77,31 +91,52 @@ const binary = 256;
|
|
|
77
91
|
const octal = 256;
|
|
78
92
|
```
|
|
79
93
|
|
|
80
|
-
|
|
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
|
|
81
114
|
|
|
82
115
|
```js
|
|
83
116
|
/* ignore-all-hex-under */
|
|
84
117
|
|
|
85
|
-
//
|
|
118
|
+
// ignore-all-hex-under must be the very first line and a block comment.
|
|
119
|
+
// This will ignore all of the whole file.
|
|
86
120
|
const foo = 0xffff;
|
|
87
|
-
|
|
88
121
|
const bar = 0b10100010101;
|
|
89
122
|
```
|
|
90
123
|
|
|
91
|
-
|
|
124
|
+
#### Disable specific formats
|
|
92
125
|
|
|
93
126
|
```js
|
|
94
|
-
// This should
|
|
127
|
+
// This should ignore all hex numbers but not octal or binary numbers.
|
|
95
128
|
|
|
96
129
|
/* ignore-hex-under */
|
|
97
130
|
|
|
98
|
-
const hex = 0x100; //
|
|
99
|
-
const octal = 0o1000; //
|
|
100
|
-
const binary = 0b10000; //
|
|
131
|
+
const hex = 0x100; // stays 0x100
|
|
132
|
+
const octal = 0o1000; // fixed to 512
|
|
133
|
+
const binary = 0b10000; // fixed to 16
|
|
101
134
|
```
|
|
102
135
|
|
|
103
136
|
## Integration
|
|
104
137
|
|
|
138
|
+
Requires ESLint v9+ (flat config)
|
|
139
|
+
|
|
105
140
|
```sh
|
|
106
141
|
npm install --save-dev eslint-plugin-hex-under
|
|
107
142
|
```
|
|
@@ -126,6 +161,14 @@ export default [
|
|
|
126
161
|
];
|
|
127
162
|
```
|
|
128
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
|
+
|
|
129
172
|
## Configuration
|
|
130
173
|
|
|
131
174
|
| Option | Type | Default | Description |
|
|
@@ -133,6 +176,14 @@ export default [
|
|
|
133
176
|
| `limit` | number | format-specific | Maximum allowed value |
|
|
134
177
|
| `skipBigInt` | boolean | false | Ignore BigInt values |
|
|
135
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`
|
|
186
|
+
|
|
136
187
|
## Testing & Code Coverage
|
|
137
188
|
|
|
138
189
|
This project uses **Vitest** as its test runner with comprehensive code coverage tracking. All tests are written using Vitest's modern testing framework and ESLint's RuleTester for validating rule behavior.
|
package/package.json
CHANGED
|
@@ -10,4 +10,21 @@ const plugin = {
|
|
|
10
10
|
},
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
export const configs = {
|
|
14
|
+
recommended: {
|
|
15
|
+
rules: {
|
|
16
|
+
'hex-under/hex-under': ['warn', { limit: 255, skipBigInt: false }],
|
|
17
|
+
'hex-under/octal-under': ['warn', { limit: 511, skipBigInt: false }],
|
|
18
|
+
'hex-under/binary-under': ['warn', { limit: 255, skipBigInt: false }],
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
all: {
|
|
22
|
+
rules: {
|
|
23
|
+
'hex-under/hex-under': ['error', { limit: 255, skipBigInt: false }],
|
|
24
|
+
'hex-under/octal-under': ['error', { limit: 511, skipBigInt: false }],
|
|
25
|
+
'hex-under/binary-under': ['error', { limit: 15, skipBigInt: false }],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
13
30
|
export default plugin;
|