eslint-plugin-hex-under 0.8.4 → 0.9.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
@@ -46,16 +46,16 @@ npm install --save-dev eslint-plugin-hex-under
46
46
  ```js
47
47
  // eslint.config.js
48
48
 
49
- import eslintPluginHexUnder from "eslint-plugin-hex-under";
49
+ import eslintPluginHexUnder from 'eslint-plugin-hex-under';
50
50
 
51
51
  export default [
52
52
  {
53
- files: ["*.js"],
53
+ files: ['*.js'],
54
54
  plugins: {
55
- "hex-under": eslintPluginHexUnder,
55
+ 'hex-under': eslintPluginHexUnder,
56
56
  },
57
57
  rules: {
58
- "hex-under/hex-under": ["error", { limit: 255 }],
58
+ 'hex-under/hex-under': ['error', { limit: 255 }],
59
59
  },
60
60
  },
61
61
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-hex-under",
3
- "version": "0.8.4",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "main": "src/eslint-plugin-hex-under.js",
6
6
  "scripts": {
@@ -8,7 +8,7 @@
8
8
  "lint": "prettier . --check && eslint .",
9
9
  "test": "vitest run",
10
10
  "test:all": "npm run test && npm run test:bats",
11
- "test:bats": "bats test/*.bats",
11
+ "test:bats": "bats --print-output-on-failure test/*.bats",
12
12
  "test:watch": "vitest"
13
13
  },
14
14
  "repository": "github:0xflotus/eslint-plugin-hex-under",
@@ -26,6 +26,7 @@
26
26
  "license": "MIT",
27
27
  "description": "This ESLint rule proves that hex numbers are less than a specified value.",
28
28
  "devDependencies": {
29
+ "@eslint/js": "10.0.1",
29
30
  "@vitest/coverage-v8": "4.1.0",
30
31
  "bats": "1.13.0",
31
32
  "eslint": "10.0.3",
@@ -1,10 +1,10 @@
1
- import hexUnderRule from "./rules/hex-under.js";
2
- import hexUnderBigintRule from "./rules/hex-under-bigint.js";
1
+ import hexUnderRule from './rules/hex-under.js';
2
+ import hexUnderBigintRule from './rules/hex-under-bigint.js';
3
3
 
4
4
  const plugin = {
5
5
  rules: {
6
- "hex-under": hexUnderRule,
7
- "hex-under-bigint": hexUnderBigintRule,
6
+ 'hex-under': hexUnderRule,
7
+ 'hex-under-bigint': hexUnderBigintRule,
8
8
  },
9
9
  };
10
10
 
@@ -1,7 +1,7 @@
1
1
  export default {
2
2
  meta: {
3
- type: "suggestion",
4
- version: "0.0.2",
3
+ type: 'suggestion',
4
+ version: '0.0.2',
5
5
  defaultOptions: [
6
6
  {
7
7
  limit: 255,
@@ -9,15 +9,15 @@ 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
+ 'Proves that a hexadecimal bigint must be less than a specified value. Default is 255.',
13
13
  },
14
- fixable: "code",
14
+ fixable: 'code',
15
15
  schema: [
16
16
  {
17
- type: "object",
17
+ type: 'object',
18
18
  properties: {
19
19
  limit: {
20
- type: "integer",
20
+ type: 'integer',
21
21
  minimum: 0,
22
22
  },
23
23
  },
@@ -26,22 +26,22 @@ export default {
26
26
  ],
27
27
  messages: {
28
28
  valueOverGeneralBigInt:
29
- "This bigint must be less than or equal {{ limit }}. {{ overValue }} ({{ over255Raw }}) is greater than {{ limit }}.",
29
+ 'This bigint must be less than or equal {{ limit }}. {{ overValue }} ({{ over255Raw }}) is greater than {{ limit }}.',
30
30
  },
31
31
  },
32
32
  create(context) {
33
33
  const options = context.options[0] || {};
34
- const limit = typeof options.limit === "number" ? options.limit : 255;
34
+ const limit = typeof options.limit === 'number' ? options.limit : 255;
35
35
 
36
36
  return {
37
37
  onCodePathEnd: function (_codePath, node) {
38
38
  const tokens =
39
39
  node.tokens?.filter(
40
40
  (token) =>
41
- ["Numeric", "Identifier"].includes(token.type) &&
42
- typeof token.value === "string" &&
43
- token.value.startsWith("0x") &&
44
- token.value.endsWith("n"),
41
+ ['Numeric', 'Identifier'].includes(token.type) &&
42
+ typeof token.value === 'string' &&
43
+ token.value.startsWith('0x') &&
44
+ token.value.endsWith('n'),
45
45
  ) || [];
46
46
 
47
47
  for (const token of tokens) {
@@ -53,14 +53,14 @@ export default {
53
53
  if (value > limitBigInt) {
54
54
  context.report({
55
55
  node: token,
56
- messageId: "valueOverGeneralBigInt",
56
+ messageId: 'valueOverGeneralBigInt',
57
57
  data: {
58
58
  limit: limit,
59
59
  over255Raw: token.value,
60
60
  overValue: value.toString(),
61
61
  },
62
62
  fix(fixer) {
63
- return fixer.replaceText(token, value.toString() + "n");
63
+ return fixer.replaceText(token, value.toString() + 'n');
64
64
  },
65
65
  });
66
66
  }
@@ -1,7 +1,7 @@
1
1
  export default {
2
2
  meta: {
3
- type: "suggestion",
4
- version: "0.3.0",
3
+ type: 'suggestion',
4
+ version: '0.4.0',
5
5
  defaultOptions: [
6
6
  {
7
7
  limit: 255,
@@ -9,15 +9,16 @@ export default {
9
9
  ],
10
10
  docs: {
11
11
  description:
12
- "Proves that a hexadecimal number must be less than a specified value. Default is 255.",
12
+ 'Ensures that a hexadecimal number does not exceed a specified value (default: 255).',
13
+ recommended: false,
13
14
  },
14
- fixable: "code",
15
+ fixable: 'code',
15
16
  schema: [
16
17
  {
17
- type: "object",
18
+ type: 'object',
18
19
  properties: {
19
20
  limit: {
20
- type: "integer",
21
+ type: 'integer',
21
22
  minimum: 0,
22
23
  },
23
24
  },
@@ -26,50 +27,40 @@ export default {
26
27
  ],
27
28
  messages: {
28
29
  valueOverGeneral:
29
- "This number 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
  },
33
+
32
34
  create(context) {
33
- const options = context.options[0] || {};
34
- const limit = typeof options.limit === "number" ? options.limit : 255;
35
+ const { limit = 255 } = context.options[0] ?? {};
36
+ const HEX_REGEX = /^0[Xx][0-9a-fA-F_]+$/;
35
37
 
36
38
  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
- ) || [];
39
+ Literal(node) {
40
+ if (typeof node.value !== 'number' || typeof node.raw !== 'string')
41
+ return;
42
+
43
+ const raw = node.raw;
44
+
45
+ if (!HEX_REGEX.test(raw)) return;
46
46
 
47
- for (const token of tokens) {
48
- try {
49
- const value = parseInt(token.value, 16);
47
+ const value = node.value;
48
+ if (Number.isNaN(value)) return;
50
49
 
51
- if (isNaN(value)) {
52
- continue;
53
- }
50
+ if (value <= limit) return;
54
51
 
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
- }
52
+ context.report({
53
+ node,
54
+ messageId: 'valueOverGeneral',
55
+ data: {
56
+ raw,
57
+ value,
58
+ limit,
59
+ },
60
+ fix(fixer) {
61
+ return fixer.replaceText(node, String(value));
62
+ },
63
+ });
73
64
  },
74
65
  };
75
66
  },