eslint-plugin-hex-under 0.5.1 → 0.5.2

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.5.1",
3
+ "version": "0.5.2",
4
4
  "main": "src/eslint-plugin-hex-under.js",
5
5
  "scripts": {
6
6
  "lint": "prettier . --check && eslint .",
@@ -0,0 +1,74 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: "suggestion",
4
+ version: "0.0.1",
5
+ defaultOptions: [
6
+ {
7
+ limit: 255,
8
+ },
9
+ ],
10
+ docs: {
11
+ description:
12
+ "Proves that a hexadecimal bigint must be less than a specified value. Default is 255.",
13
+ },
14
+ fixable: "code",
15
+ schema: [
16
+ {
17
+ type: "object",
18
+ properties: {
19
+ limit: {
20
+ type: "integer",
21
+ minimum: 0,
22
+ },
23
+ },
24
+ additionalProperties: false,
25
+ },
26
+ ],
27
+ messages: {
28
+ valueOverGeneralBigInt:
29
+ "This bigint must be less than or equal {{ limit }}. {{ overValue }} ({{ over255Raw }}) is greater than {{ limit }}.",
30
+ },
31
+ },
32
+ create(context) {
33
+ const options = context.options[0] || {};
34
+ const limit = typeof options.limit === "number" ? options.limit : 255;
35
+
36
+ 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
+ ) || [];
46
+
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);
52
+
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
+ }
71
+ },
72
+ };
73
+ },
74
+ };
@@ -0,0 +1,76 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: "suggestion",
4
+ version: "0.3.0",
5
+ defaultOptions: [
6
+ {
7
+ limit: 255,
8
+ },
9
+ ],
10
+ docs: {
11
+ description:
12
+ "Proves that a hexadecimal number must be less than a specified value. Default is 255.",
13
+ },
14
+ fixable: "code",
15
+ schema: [
16
+ {
17
+ type: "object",
18
+ properties: {
19
+ limit: {
20
+ type: "integer",
21
+ minimum: 0,
22
+ },
23
+ },
24
+ additionalProperties: false,
25
+ },
26
+ ],
27
+ messages: {
28
+ valueOverGeneral:
29
+ "This number must be less than or equal {{ limit }}. {{ overValue }} ({{ over255Raw }}) is greater than {{ limit }}.",
30
+ },
31
+ },
32
+ create(context) {
33
+ const options = context.options[0] || {};
34
+ const limit = typeof options.limit === "number" ? options.limit : 255;
35
+
36
+ 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
+ ) || [];
46
+
47
+ for (const token of tokens) {
48
+ try {
49
+ const value = parseInt(token.value, 16);
50
+
51
+ if (isNaN(value)) {
52
+ continue;
53
+ }
54
+
55
+ if (value > limit) {
56
+ context.report({
57
+ node: token,
58
+ messageId: "valueOverGeneral",
59
+ data: {
60
+ limit: limit,
61
+ over255Raw: token.value,
62
+ overValue: value,
63
+ },
64
+ fix(fixer) {
65
+ return fixer.replaceText(token, String(value));
66
+ },
67
+ });
68
+ }
69
+ } catch {
70
+ continue;
71
+ }
72
+ }
73
+ },
74
+ };
75
+ },
76
+ };