eslint-plugin-hex-under 0.5.1 → 0.6.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 CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  Sometimes it's really hard for humans to read hexadecimal numbers and know the exact decimal value.
4
4
 
5
+ ## Testing & Code Coverage
6
+
7
+ 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.
8
+
9
+ ### Running Tests
10
+
11
+ ```sh
12
+ # Run all tests once
13
+ npm run test
14
+
15
+ # Run tests in watch mode (for development)
16
+ npm run test:watch
17
+
18
+ # Run tests with coverage report
19
+ npm run coverage
20
+ ```
21
+
5
22
  ## hex-under
6
23
 
7
24
  This ESLint plugin proves, if you use hexadecimal numbers in your code, that its value is less than or equal a specified value (default: 255). If you use a hexadecimal number greater than this specified value, it will be transformed to its decimal value.
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "eslint-plugin-hex-under",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
+ "type": "module",
4
5
  "main": "src/eslint-plugin-hex-under.js",
5
6
  "scripts": {
6
7
  "lint": "prettier . --check && eslint .",
7
- "test": "npm run test:hex-under && npm run test:bigint",
8
- "test:hex-under": "node test/hex-under.test.js",
9
- "test:bigint": "node test/hex-under-bigint.test.js"
8
+ "test": "vitest run",
9
+ "test:watch": "vitest",
10
+ "coverage": "vitest run --coverage"
10
11
  },
11
12
  "repository": {
12
13
  "type": "git",
@@ -17,8 +18,11 @@
17
18
  "license": "MIT",
18
19
  "description": "This ESLint rule proves that hex numbers are less than a specified value.",
19
20
  "devDependencies": {
21
+ "@vitest/coverage-v8": "4.1.0",
20
22
  "eslint": "10.0.3",
21
- "prettier": "3.8.1"
23
+ "prettier": "3.8.1",
24
+ "tinyexec": "1.0.4",
25
+ "vitest": "4.1.0"
22
26
  },
23
27
  "peerDependencies": {
24
28
  "eslint": "^10.0.0"
@@ -1,9 +1,11 @@
1
- const hexUnderRule = require("./rules/hex-under");
2
- const hexUnderBigintRule = require("./rules/hex-under-bigint");
1
+ import hexUnderRule from "./rules/hex-under.js";
2
+ import hexUnderBigintRule from "./rules/hex-under-bigint.js";
3
+
3
4
  const plugin = {
4
5
  rules: {
5
6
  "hex-under": hexUnderRule,
6
7
  "hex-under-bigint": hexUnderBigintRule,
7
8
  },
8
9
  };
9
- module.exports = plugin;
10
+
11
+ export default plugin;
@@ -0,0 +1,74 @@
1
+ export default {
2
+ meta: {
3
+ type: "suggestion",
4
+ version: "0.0.2",
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
+ export default {
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
+ };