eslint-plugin-hex-under 0.1.2 → 0.2.1

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/hex-under.js CHANGED
@@ -2,7 +2,8 @@ module.exports = {
2
2
  meta: {
3
3
  type: "problem",
4
4
  docs: {
5
- description: "Proves that a hexadecimal number must be less than a specified value. Default is 255.",
5
+ description:
6
+ "Proves that a hexadecimal number must be less than a specified value. Default is 255.",
6
7
  },
7
8
  fixable: "code",
8
9
  schema: [
@@ -18,38 +19,32 @@ module.exports = {
18
19
  },
19
20
  ],
20
21
  messages: {
21
- valueOver:
22
- "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 }}.",
23
24
  },
24
25
  },
25
26
  create(context) {
26
27
  const limit = context.options[0]?.limit || 255;
27
28
  return {
28
- VariableDeclarator(node) {
29
- if (["const", "let", "var"].some((el) => el === node.parent.kind)) {
30
- if (
31
- node.init &&
32
- node.init.type === "Literal" &&
33
- node.init.raw.startsWith("0x")
34
- ) {
35
- if (parseInt(node.init.value) > limit) {
36
- context.report({
37
- node,
38
- messageId: "valueOver",
39
- data: {
40
- limit: limit,
41
- over255Raw: node.init.raw,
42
- overValue: node.init.value,
43
- variableName: node.id.name,
44
- },
45
- fix(fixer) {
46
- return fixer.replaceText(
47
- node.init,
48
- parseInt(node.init.value),
49
- );
50
- },
51
- });
52
- }
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
+ });
53
48
  }
54
49
  }
55
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,10 +1,10 @@
1
1
  {
2
2
  "name": "eslint-plugin-hex-under",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "main": "eslint-plugin-hex-under.js",
5
5
  "scripts": {
6
6
  "lint": "prettier . --check && eslint .",
7
- "test": "node hex-under-256.test.js"
7
+ "test": "node hex-under.test.js"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",