eslint-plugin-hex-under 0.1.4 → 0.2.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/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # eslint-plugin-hex-under
2
2
 
3
+ Sometimes it's really hard for humans to read hexadecimal numbers and know the exact decimal value.
4
+
3
5
  ## hex-under
4
6
 
5
- This ESLint plugin proves, if you use hexadecimal numbers in your variable declaration 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.
7
+ 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.
6
8
 
7
9
  ### Example
8
10
 
@@ -10,11 +12,30 @@ This ESLint plugin proves, if you use hexadecimal numbers in your variable decla
10
12
  // valid with { limit: 255 }
11
13
  const signal = 0xef;
12
14
 
15
+ let func = () => 0xab;
16
+
17
+ function add(a, b) {
18
+ return a + b + 0x1f;
19
+ }
20
+
13
21
  // invalid with { limit: 255 }
14
22
  const signal = 0x21b;
15
23
 
24
+ let func = () => 0xabc;
25
+
26
+ function add(a, b) {
27
+ return a + b + 0x100;
28
+ }
29
+
16
30
  // This can be transformed to:
17
31
  const signal = 539;
32
+
33
+ let func = () => 2748;
34
+
35
+ function add(a, b) {
36
+ return a + b + 256;
37
+ }
38
+
18
39
  ```
19
40
 
20
41
  ## Integration
package/hex-under.js CHANGED
@@ -19,38 +19,32 @@ module.exports = {
19
19
  },
20
20
  ],
21
21
  messages: {
22
- valueOver:
23
- "Value of '{{ variableName }}' must be less than or equal {{ limit }}. {{ overValue }} ({{ over255Raw }}) is greater than {{ limit }}.",
22
+ valueOverGeneral:
23
+ "This number must be less than or equal {{ limit }}. {{ overValue }} ({{ over255Raw }}) is greater than {{ limit }}.",
24
24
  },
25
25
  },
26
26
  create(context) {
27
27
  const limit = context.options[0]?.limit || 255;
28
28
  return {
29
- VariableDeclarator(node) {
30
- if (["const", "let", "var"].some((el) => el === node.parent.kind)) {
31
- if (
32
- node.init &&
33
- node.init.type === "Literal" &&
34
- node.init.raw.startsWith("0x")
35
- ) {
36
- if (parseInt(node.init.value) > limit) {
37
- context.report({
38
- node,
39
- messageId: "valueOver",
40
- data: {
41
- limit: limit,
42
- over255Raw: node.init.raw,
43
- overValue: node.init.value,
44
- variableName: node.id.name,
45
- },
46
- fix(fixer) {
47
- return fixer.replaceText(
48
- node.init,
49
- parseInt(node.init.value),
50
- );
51
- },
52
- });
53
- }
29
+ onCodePathEnd: function (_codePath, node) {
30
+ const tokens =
31
+ node.tokens?.filter(
32
+ (token) => token.type === "Numeric" && token.value.startsWith("0x"),
33
+ ) || [];
34
+ for (const token of tokens) {
35
+ if (token.value > limit) {
36
+ context.report({
37
+ node: token,
38
+ messageId: "valueOverGeneral",
39
+ data: {
40
+ limit: limit,
41
+ over255Raw: token.value,
42
+ overValue: parseInt(token.value),
43
+ },
44
+ fix(fixer) {
45
+ return fixer.replaceText(token, parseInt(token.value));
46
+ },
47
+ });
54
48
  }
55
49
  }
56
50
  },
package/hex-under.test.js CHANGED
@@ -15,6 +15,23 @@ ruleTester.run("hex-under", hexUnderRule, {
15
15
  options: [{ limit: 256 }],
16
16
  code: "const foo = 0x100;",
17
17
  },
18
+ {
19
+ options: [{ limit: 255 }],
20
+ code: "let foo = 0xff;",
21
+ },
22
+ {
23
+ options: [{ limit: 256 }],
24
+ code: "var foo = 0xff;",
25
+ },
26
+ {
27
+ code: "function func() {\n return 0xff;\n}",
28
+ },
29
+ {
30
+ code: "functionA(0xef);",
31
+ },
32
+ {
33
+ code: "const func = () => 0xab;",
34
+ },
18
35
  ],
19
36
  invalid: [
20
37
  {
@@ -22,38 +39,31 @@ ruleTester.run("hex-under", hexUnderRule, {
22
39
  output: "const foo = 256;",
23
40
  errors: 1,
24
41
  },
25
- ],
26
- });
27
-
28
- ruleTester.run("hex-under", hexUnderRule, {
29
- valid: [
30
- {
31
- options: [{ limit: 255 }],
32
- code: "let foo = 0xff;",
33
- },
34
- ],
35
- invalid: [
36
42
  {
37
43
  code: "let foo = 0x100;",
38
44
  output: "let foo = 256;",
39
45
  errors: 1,
40
46
  },
41
- ],
42
- });
43
-
44
- ruleTester.run("hex-under", hexUnderRule, {
45
- valid: [
46
- {
47
- options: [{ limit: 256 }],
48
- code: "var foo = 0xff;",
49
- },
50
- ],
51
- invalid: [
52
47
  {
53
48
  code: "var foo = 0x100;",
54
49
  output: "var foo = 256;",
55
50
  errors: 1,
56
51
  },
52
+ {
53
+ code: "function func() {\n return 0x100;\n}",
54
+ output: "function func() {\n return 256;\n}",
55
+ errors: 1,
56
+ },
57
+ {
58
+ code: "functionA(0x1234);",
59
+ output: "functionA(4660);",
60
+ errors: 1,
61
+ },
62
+ {
63
+ code: "const func = () => 0xabc;",
64
+ output: "const func = () => 2748;",
65
+ errors: 1,
66
+ },
57
67
  ],
58
68
  });
59
69
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-hex-under",
3
- "version": "0.1.4",
3
+ "version": "0.2.2",
4
4
  "main": "eslint-plugin-hex-under.js",
5
5
  "scripts": {
6
6
  "lint": "prettier . --check && eslint .",
@@ -1,39 +0,0 @@
1
- name: Build and Test
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
- pull_request:
8
- branches:
9
- - main
10
-
11
- jobs:
12
- lint:
13
- name: Linting
14
- runs-on: ubuntu-latest
15
- steps:
16
- - uses: actions/checkout@v4
17
- - name: Setup Node.js
18
- uses: actions/setup-node@v4
19
- with:
20
- node-version: 22
21
- - run: npm install
22
- - name: Run Linting
23
- run: DEBUG=eslint:eslint npm run lint
24
- build:
25
- name: Testing
26
- needs: lint
27
- strategy:
28
- matrix:
29
- os: [ubuntu-latest, ubuntu-24.04-arm]
30
- node-version: [18, 20, 22]
31
- runs-on: ${{ matrix.os }}
32
- steps:
33
- - uses: actions/checkout@v4
34
- - name: Setup Node.js ${{ matrix.node-version }}
35
- uses: actions/setup-node@v4
36
- with:
37
- node-version: ${{ matrix.node-version }}
38
- - run: npm install
39
- - run: npm test