eslint-plugin-hex-under 0.8.3 → 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 +4 -4
- package/package.json +15 -8
- package/src/eslint-plugin-hex-under.js +4 -4
- package/src/rules/hex-under-bigint.js +14 -14
- package/src/rules/hex-under.js +33 -42
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
|
|
49
|
+
import eslintPluginHexUnder from 'eslint-plugin-hex-under';
|
|
50
50
|
|
|
51
51
|
export default [
|
|
52
52
|
{
|
|
53
|
-
files: [
|
|
53
|
+
files: ['*.js'],
|
|
54
54
|
plugins: {
|
|
55
|
-
|
|
55
|
+
'hex-under': eslintPluginHexUnder,
|
|
56
56
|
},
|
|
57
57
|
rules: {
|
|
58
|
-
|
|
58
|
+
'hex-under/hex-under': ['error', { limit: 255 }],
|
|
59
59
|
},
|
|
60
60
|
},
|
|
61
61
|
];
|
package/package.json
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-hex-under",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/eslint-plugin-hex-under.js",
|
|
6
6
|
"scripts": {
|
|
7
|
+
"coverage": "vitest run --coverage",
|
|
7
8
|
"lint": "prettier . --check && eslint .",
|
|
8
9
|
"test": "vitest run",
|
|
9
10
|
"test:all": "npm run test && npm run test:bats",
|
|
10
|
-
"test:bats": "bats test/*.bats",
|
|
11
|
-
"test:watch": "vitest"
|
|
12
|
-
"coverage": "vitest run --coverage"
|
|
11
|
+
"test:bats": "bats --print-output-on-failure test/*.bats",
|
|
12
|
+
"test:watch": "vitest"
|
|
13
13
|
},
|
|
14
|
-
"repository":
|
|
15
|
-
|
|
16
|
-
"
|
|
14
|
+
"repository": "github:0xflotus/eslint-plugin-hex-under",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"eslint",
|
|
17
|
+
"eslint-plugin",
|
|
18
|
+
"lint",
|
|
19
|
+
"hex",
|
|
20
|
+
"static-analysis"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22"
|
|
17
24
|
},
|
|
18
|
-
"keywords": [],
|
|
19
25
|
"author": "0xflotus",
|
|
20
26
|
"license": "MIT",
|
|
21
27
|
"description": "This ESLint rule proves that hex numbers are less than a specified value.",
|
|
22
28
|
"devDependencies": {
|
|
29
|
+
"@eslint/js": "10.0.1",
|
|
23
30
|
"@vitest/coverage-v8": "4.1.0",
|
|
24
31
|
"bats": "1.13.0",
|
|
25
32
|
"eslint": "10.0.3",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import hexUnderRule from
|
|
2
|
-
import hexUnderBigintRule from
|
|
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
|
-
|
|
7
|
-
|
|
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:
|
|
4
|
-
version:
|
|
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
|
-
|
|
12
|
+
'Proves that a hexadecimal bigint must be less than a specified value. Default is 255.',
|
|
13
13
|
},
|
|
14
|
-
fixable:
|
|
14
|
+
fixable: 'code',
|
|
15
15
|
schema: [
|
|
16
16
|
{
|
|
17
|
-
type:
|
|
17
|
+
type: 'object',
|
|
18
18
|
properties: {
|
|
19
19
|
limit: {
|
|
20
|
-
type:
|
|
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
|
-
|
|
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 ===
|
|
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
|
-
[
|
|
42
|
-
typeof token.value ===
|
|
43
|
-
token.value.startsWith(
|
|
44
|
-
token.value.endsWith(
|
|
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:
|
|
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() +
|
|
63
|
+
return fixer.replaceText(token, value.toString() + 'n');
|
|
64
64
|
},
|
|
65
65
|
});
|
|
66
66
|
}
|
package/src/rules/hex-under.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
meta: {
|
|
3
|
-
type:
|
|
4
|
-
version:
|
|
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
|
-
|
|
12
|
+
'Ensures that a hexadecimal number does not exceed a specified value (default: 255).',
|
|
13
|
+
recommended: false,
|
|
13
14
|
},
|
|
14
|
-
fixable:
|
|
15
|
+
fixable: 'code',
|
|
15
16
|
schema: [
|
|
16
17
|
{
|
|
17
|
-
type:
|
|
18
|
+
type: 'object',
|
|
18
19
|
properties: {
|
|
19
20
|
limit: {
|
|
20
|
-
type:
|
|
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
|
-
|
|
30
|
+
'Hex number {{ raw }} ({{ value }}) exceeds the limit of {{ limit }}.',
|
|
30
31
|
},
|
|
31
32
|
},
|
|
33
|
+
|
|
32
34
|
create(context) {
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
+
const { limit = 255 } = context.options[0] ?? {};
|
|
36
|
+
const HEX_REGEX = /^0[Xx][0-9a-fA-F_]+$/;
|
|
35
37
|
|
|
36
38
|
return {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
const value = parseInt(token.value, 16);
|
|
47
|
+
const value = node.value;
|
|
48
|
+
if (Number.isNaN(value)) return;
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
50
|
+
if (value <= limit) return;
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
},
|