eslint-plugin-hex-under 0.9.0 → 0.10.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/package.json +1 -1
- package/src/rules/hex-under-bigint.js +27 -37
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
meta: {
|
|
3
3
|
type: 'suggestion',
|
|
4
|
-
version: '0.0
|
|
4
|
+
version: '0.1.0',
|
|
5
5
|
defaultOptions: [
|
|
6
6
|
{
|
|
7
7
|
limit: 255,
|
|
@@ -9,7 +9,8 @@ export default {
|
|
|
9
9
|
],
|
|
10
10
|
docs: {
|
|
11
11
|
description:
|
|
12
|
-
'
|
|
12
|
+
'Ensures that a hexadecimal bigint does not exceed a specified value (default: 255).',
|
|
13
|
+
recommended: false,
|
|
13
14
|
},
|
|
14
15
|
fixable: 'code',
|
|
15
16
|
schema: [
|
|
@@ -26,48 +27,37 @@ export default {
|
|
|
26
27
|
],
|
|
27
28
|
messages: {
|
|
28
29
|
valueOverGeneralBigInt:
|
|
29
|
-
'
|
|
30
|
+
'Hex number {{ raw }} ({{ value }}) exceeds the limit of {{ limit }}.',
|
|
30
31
|
},
|
|
31
32
|
},
|
|
32
33
|
create(context) {
|
|
33
|
-
const
|
|
34
|
-
const
|
|
34
|
+
const { limit = 255 } = context.options[0] ?? {};
|
|
35
|
+
const HEX_REGEX = /^0[Xx][0-9a-fA-F_]+n$/;
|
|
35
36
|
|
|
36
37
|
return {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
node.tokens?.filter(
|
|
40
|
-
(token) =>
|
|
41
|
-
['Numeric', 'Identifier'].includes(token.type) &&
|
|
42
|
-
typeof token.value === 'string' &&
|
|
43
|
-
token.value.startsWith('0x') &&
|
|
44
|
-
token.value.endsWith('n'),
|
|
45
|
-
) || [];
|
|
38
|
+
Literal(node) {
|
|
39
|
+
if (typeof node.raw !== 'string') return;
|
|
46
40
|
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
const hexValue = token.value.slice(0, -1);
|
|
50
|
-
const value = BigInt(hexValue);
|
|
51
|
-
const limitBigInt = BigInt(limit);
|
|
41
|
+
const raw = node.raw;
|
|
52
42
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
}
|
|
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
|
+
});
|
|
71
61
|
},
|
|
72
62
|
};
|
|
73
63
|
},
|