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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-hex-under",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "main": "src/eslint-plugin-hex-under.js",
6
6
  "scripts": {
@@ -1,7 +1,7 @@
1
1
  export default {
2
2
  meta: {
3
3
  type: 'suggestion',
4
- version: '0.0.2',
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
- 'Proves that a hexadecimal bigint must be less than a specified value. Default is 255.',
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
- 'This bigint must be less than or equal {{ limit }}. {{ overValue }} ({{ over255Raw }}) is greater than {{ limit }}.',
30
+ 'Hex number {{ raw }} ({{ value }}) exceeds the limit of {{ limit }}.',
30
31
  },
31
32
  },
32
33
  create(context) {
33
- const options = context.options[0] || {};
34
- const limit = typeof options.limit === 'number' ? options.limit : 255;
34
+ const { limit = 255 } = context.options[0] ?? {};
35
+ const HEX_REGEX = /^0[Xx][0-9a-fA-F_]+n$/;
35
36
 
36
37
  return {
37
- onCodePathEnd: function (_codePath, node) {
38
- const tokens =
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
- for (const token of tokens) {
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
- if (value > limitBigInt) {
54
- context.report({
55
- node: token,
56
- messageId: 'valueOverGeneralBigInt',
57
- data: {
58
- limit: limit,
59
- over255Raw: token.value,
60
- overValue: value.toString(),
61
- },
62
- fix(fixer) {
63
- return fixer.replaceText(token, value.toString() + 'n');
64
- },
65
- });
66
- }
67
- } catch {
68
- continue;
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
  },