eslint-plugin-hex-under 0.10.0 → 0.11.0
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 +4 -22
- package/package.json +1 -1
- package/src/eslint-plugin-hex-under.js +0 -2
- package/src/rules/hex-under.js +24 -10
- package/src/rules/hex-under-bigint.js +0 -64
package/README.md
CHANGED
|
@@ -27,6 +27,8 @@ function add(a, b) {
|
|
|
27
27
|
return a + b + 0x100;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
let d = 0xaa_ffn;
|
|
31
|
+
|
|
30
32
|
// This can be transformed to:
|
|
31
33
|
const signal = 539;
|
|
32
34
|
|
|
@@ -35,6 +37,8 @@ let func = () => 2748;
|
|
|
35
37
|
function add(a, b) {
|
|
36
38
|
return a + b + 256;
|
|
37
39
|
}
|
|
40
|
+
|
|
41
|
+
let d = 43775;
|
|
38
42
|
```
|
|
39
43
|
|
|
40
44
|
## Integration
|
|
@@ -61,28 +65,6 @@ export default [
|
|
|
61
65
|
];
|
|
62
66
|
```
|
|
63
67
|
|
|
64
|
-
#### Experimental BigInteger Support
|
|
65
|
-
|
|
66
|
-
There is a similar rule to prove for BigInteger hexadecimal numbers. You can enable it similar to the `hex-under` rule. The default limit is 255.
|
|
67
|
-
|
|
68
|
-
```js
|
|
69
|
-
// eslint.config.js
|
|
70
|
-
|
|
71
|
-
import eslintPluginHexUnder from "eslint-plugin-hex-under";
|
|
72
|
-
|
|
73
|
-
export default = [
|
|
74
|
-
{
|
|
75
|
-
files: ["*.js"],
|
|
76
|
-
plugins: {
|
|
77
|
-
"hex-under": eslintPluginHexUnder,
|
|
78
|
-
},
|
|
79
|
-
rules: {
|
|
80
|
-
"hex-under/hex-under-bigint": "warn",
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
];
|
|
84
|
-
```
|
|
85
|
-
|
|
86
68
|
## Testing & Code Coverage
|
|
87
69
|
|
|
88
70
|
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
package/src/rules/hex-under.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
meta: {
|
|
3
3
|
type: 'suggestion',
|
|
4
|
-
version: '0.
|
|
4
|
+
version: '0.5.0',
|
|
5
5
|
defaultOptions: [
|
|
6
6
|
{
|
|
7
7
|
limit: 255,
|
|
@@ -32,22 +32,33 @@ export default {
|
|
|
32
32
|
},
|
|
33
33
|
|
|
34
34
|
create(context) {
|
|
35
|
-
const { limit = 255 } = context.options
|
|
36
|
-
const HEX_REGEX = /^0[
|
|
35
|
+
const [{ limit = 255 } = {}] = context.options;
|
|
36
|
+
const HEX_REGEX = /^0[xX][0-9a-fA-F_]+$/;
|
|
37
|
+
const HEX_REGEX_BIGINT = /^0[xX][0-9a-fA-F_]+n$/;
|
|
37
38
|
|
|
38
39
|
return {
|
|
39
40
|
Literal(node) {
|
|
40
|
-
if (typeof node.value !== 'number'
|
|
41
|
+
if (typeof node.value !== 'number' && typeof node.value !== 'bigint')
|
|
41
42
|
return;
|
|
43
|
+
if (typeof node.raw !== 'string') return;
|
|
42
44
|
|
|
43
45
|
const raw = node.raw;
|
|
46
|
+
const isHexBigInt = HEX_REGEX_BIGINT.test(raw);
|
|
47
|
+
const isHex = HEX_REGEX.test(raw);
|
|
44
48
|
|
|
45
|
-
if (!
|
|
49
|
+
if (!isHex && !isHexBigInt) return;
|
|
46
50
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
const normalized = raw.replace(/_/g, '').replace(/n$/, '');
|
|
52
|
+
let value = null;
|
|
53
|
+
if (isHexBigInt) {
|
|
54
|
+
value = BigInt(normalized);
|
|
55
|
+
if (value <= BigInt(limit)) return;
|
|
56
|
+
} else if (isHex) {
|
|
57
|
+
value = Number(normalized);
|
|
58
|
+
if (Number.isNaN(value) || value <= limit) return;
|
|
59
|
+
} else {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
51
62
|
|
|
52
63
|
context.report({
|
|
53
64
|
node,
|
|
@@ -58,7 +69,10 @@ export default {
|
|
|
58
69
|
limit,
|
|
59
70
|
},
|
|
60
71
|
fix(fixer) {
|
|
61
|
-
return fixer.replaceText(
|
|
72
|
+
return fixer.replaceText(
|
|
73
|
+
node,
|
|
74
|
+
isHexBigInt ? `${String(BigInt(value))}n` : String(value),
|
|
75
|
+
);
|
|
62
76
|
},
|
|
63
77
|
});
|
|
64
78
|
},
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
meta: {
|
|
3
|
-
type: 'suggestion',
|
|
4
|
-
version: '0.1.0',
|
|
5
|
-
defaultOptions: [
|
|
6
|
-
{
|
|
7
|
-
limit: 255,
|
|
8
|
-
},
|
|
9
|
-
],
|
|
10
|
-
docs: {
|
|
11
|
-
description:
|
|
12
|
-
'Ensures that a hexadecimal bigint does not exceed a specified value (default: 255).',
|
|
13
|
-
recommended: false,
|
|
14
|
-
},
|
|
15
|
-
fixable: 'code',
|
|
16
|
-
schema: [
|
|
17
|
-
{
|
|
18
|
-
type: 'object',
|
|
19
|
-
properties: {
|
|
20
|
-
limit: {
|
|
21
|
-
type: 'integer',
|
|
22
|
-
minimum: 0,
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
additionalProperties: false,
|
|
26
|
-
},
|
|
27
|
-
],
|
|
28
|
-
messages: {
|
|
29
|
-
valueOverGeneralBigInt:
|
|
30
|
-
'Hex number {{ raw }} ({{ value }}) exceeds the limit of {{ limit }}.',
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
create(context) {
|
|
34
|
-
const { limit = 255 } = context.options[0] ?? {};
|
|
35
|
-
const HEX_REGEX = /^0[Xx][0-9a-fA-F_]+n$/;
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
Literal(node) {
|
|
39
|
-
if (typeof node.raw !== 'string') return;
|
|
40
|
-
|
|
41
|
-
const raw = node.raw;
|
|
42
|
-
|
|
43
|
-
if (!HEX_REGEX.test(raw)) return;
|
|
44
|
-
|
|
45
|
-
const value = BigInt(node.raw.slice(0, -1).replace(/_/g, ''));
|
|
46
|
-
|
|
47
|
-
if (value <= BigInt(limit)) return;
|
|
48
|
-
|
|
49
|
-
context.report({
|
|
50
|
-
node,
|
|
51
|
-
messageId: 'valueOverGeneralBigInt',
|
|
52
|
-
data: {
|
|
53
|
-
raw,
|
|
54
|
-
value,
|
|
55
|
-
limit,
|
|
56
|
-
},
|
|
57
|
-
fix(fixer) {
|
|
58
|
-
return fixer.replaceText(node, `${String(BigInt(value))}n`);
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
},
|
|
64
|
-
};
|