eslint-plugin-hex-under 0.12.3 → 1.0.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
@@ -18,6 +18,13 @@ function add(a, b) {
18
18
  return a + b + 0x1f;
19
19
  }
20
20
 
21
+ const binary = 0b1111_1111;
22
+
23
+ const octal = 0o377;
24
+
25
+ // valid with { limit: 255, skipBigInt: true }
26
+ const mask = 0xdead_beefn;
27
+
21
28
  // invalid with { limit: 255 }
22
29
  const signal = 0x21b;
23
30
 
@@ -29,6 +36,10 @@ function add(a, b) {
29
36
 
30
37
  let d = 0xaa_ffn;
31
38
 
39
+ const binary = 0b1_0000_0000;
40
+
41
+ const octal = 0o400;
42
+
32
43
  // This can be transformed to:
33
44
  const signal = 539;
34
45
 
@@ -39,6 +50,10 @@ function add(a, b) {
39
50
  }
40
51
 
41
52
  let d = 43775;
53
+
54
+ const binary = 256;
55
+
56
+ const octal = 256;
42
57
  ```
43
58
 
44
59
  ## Integration
@@ -59,7 +74,9 @@ export default [
59
74
  'hex-under': eslintPluginHexUnder,
60
75
  },
61
76
  rules: {
62
- 'hex-under/hex-under': ['error', { limit: 255 }],
77
+ 'hex-under/hex-under': ['error', { limit: 255, skipBigInt: false }],
78
+ 'hex-under/octal-under': ['error', { limit: 511, skipBigInt: false }],
79
+ 'hex-under/binary-under': ['error', { limit: 15, skipBigInt: false }],
63
80
  },
64
81
  },
65
82
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-hex-under",
3
- "version": "0.12.3",
3
+ "version": "1.0.0",
4
4
  "type": "module",
5
5
  "main": "src/eslint-plugin-hex-under.js",
6
6
  "scripts": {
@@ -1,8 +1,12 @@
1
+ import binaryUnder from './rules/binary-under.js';
1
2
  import hexUnderRule from './rules/hex-under.js';
3
+ import octalUnder from './rules/octal-under.js';
2
4
 
3
5
  const plugin = {
4
6
  rules: {
5
7
  'hex-under': hexUnderRule,
8
+ 'octal-under': octalUnder,
9
+ 'binary-under': binaryUnder,
6
10
  },
7
11
  };
8
12
 
@@ -0,0 +1,71 @@
1
+ const BINARY_REGEX = /^0[bB][01_]+$/;
2
+ const BINARY_REGEX_BIGINT = /^0[bB][01_]+n$/;
3
+
4
+ export default {
5
+ meta: {
6
+ version: '0.1.0',
7
+ type: 'suggestion',
8
+ docs: {
9
+ description: 'Ensures binary numbers do not exceed a limit.',
10
+ recommended: false,
11
+ },
12
+ fixable: 'code',
13
+ schema: [
14
+ {
15
+ type: 'object',
16
+ properties: {
17
+ limit: { type: 'integer', minimum: 0 },
18
+ skipBigInt: { type: 'boolean' },
19
+ },
20
+ additionalProperties: false,
21
+ },
22
+ ],
23
+ messages: {
24
+ valueOver:
25
+ 'Binary number {{ raw }} ({{ value }}) exceeds the limit of {{ limit }}.',
26
+ },
27
+ },
28
+
29
+ create(context) {
30
+ const [{ limit = 15, skipBigInt = false } = {}] = context.options;
31
+
32
+ return {
33
+ 'Literal[raw=/^0[bB][01_]+n?$/]'(node) {
34
+ const raw = node.raw;
35
+ const isBigInt = BINARY_REGEX_BIGINT.test(raw);
36
+ const isBinary = BINARY_REGEX.test(raw);
37
+
38
+ if (skipBigInt && isBigInt) return;
39
+
40
+ const normalized = raw.replace(/_/g, '').replace(/n$/, '');
41
+ let value = null;
42
+
43
+ if (isBigInt) {
44
+ value = BigInt(normalized);
45
+ if (value <= BigInt(limit)) return;
46
+ }
47
+
48
+ if (isBinary) {
49
+ value = parseInt(normalized.slice(2), 2);
50
+ if (Number.isNaN(value) || value <= limit) return;
51
+ }
52
+
53
+ context.report({
54
+ node,
55
+ messageId: 'valueOver',
56
+ data: {
57
+ raw: raw,
58
+ value: value,
59
+ limit: limit,
60
+ },
61
+ fix(fixer) {
62
+ return fixer.replaceText(
63
+ node,
64
+ isBigInt ? `${String(value)}n` : String(value),
65
+ );
66
+ },
67
+ });
68
+ },
69
+ };
70
+ },
71
+ };
@@ -0,0 +1,76 @@
1
+ const OCTAL_REGEX = /^0[oO]?[0-7_]+$/;
2
+ const OCTAL_REGEX_BIGINT = /^0[oO]?[0-7_]+n$/;
3
+
4
+ export default {
5
+ meta: {
6
+ version: '0.1.0',
7
+ type: 'suggestion',
8
+ docs: {
9
+ description: 'Ensures octal numbers do not exceed a limit.',
10
+ recommended: false,
11
+ },
12
+ fixable: 'code',
13
+ schema: [
14
+ {
15
+ type: 'object',
16
+ properties: {
17
+ limit: { type: 'integer', minimum: 0 },
18
+ skipBigInt: { type: 'boolean' },
19
+ },
20
+ additionalProperties: false,
21
+ },
22
+ ],
23
+ messages: {
24
+ valueOver:
25
+ 'Octal number {{ raw }} ({{ value }}) exceeds the limit of {{ limit }}.',
26
+ },
27
+ },
28
+
29
+ create(context) {
30
+ const [{ limit = 511, skipBigInt = false } = {}] = context.options;
31
+
32
+ return {
33
+ 'Literal[raw=/^0[oO]?[0-7_]+n?$/]'(node) {
34
+ const raw = node.raw;
35
+
36
+ if (
37
+ raw.startsWith('0') &&
38
+ !raw.startsWith('0o') &&
39
+ !raw.startsWith('0O')
40
+ ) {
41
+ return;
42
+ }
43
+
44
+ const isBigInt = OCTAL_REGEX_BIGINT.test(raw);
45
+ const isOctal = OCTAL_REGEX.test(raw);
46
+
47
+ if (skipBigInt && isBigInt) return;
48
+
49
+ const normalized = raw.replace(/_/g, '').replace(/n$/, '');
50
+ let value = null;
51
+
52
+ if (isBigInt) {
53
+ value = BigInt(normalized);
54
+ if (value <= BigInt(limit)) return;
55
+ }
56
+
57
+ if (isOctal) {
58
+ value = parseInt(normalized.slice(2), 8);
59
+ if (Number.isNaN(value) || value <= limit) return;
60
+ }
61
+
62
+ context.report({
63
+ node,
64
+ messageId: 'valueOver',
65
+ data: { raw, value, limit },
66
+ fix(fixer) {
67
+ return fixer.replaceText(
68
+ node,
69
+ isBigInt ? `${String(value)}n` : String(value),
70
+ );
71
+ },
72
+ });
73
+ },
74
+ };
75
+ },
76
+ };