eslint-plugin-hex-under 0.0.3
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/LICENSE +21 -0
- package/README.md +38 -0
- package/eslint-plugin-hex-under.js +3 -0
- package/eslint.config.js +15 -0
- package/hex-under.js +58 -0
- package/hex-under.test.js +60 -0
- package/package.json +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 0xflotus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# eslint-plugin-hex-under
|
|
2
|
+
|
|
3
|
+
## hex-under
|
|
4
|
+
|
|
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.
|
|
6
|
+
|
|
7
|
+
### Example
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
// valid with { limit: 255 }
|
|
11
|
+
const signal = 0xef;
|
|
12
|
+
|
|
13
|
+
// invalid with { limit: 255 }
|
|
14
|
+
const signal = 0x21b;
|
|
15
|
+
|
|
16
|
+
// This will be transformed to:
|
|
17
|
+
const signal = 539;
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Integration
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
// eslint.config.js
|
|
24
|
+
|
|
25
|
+
const eslintPluginHexUnder = require("./eslint-plugin-hex-under");
|
|
26
|
+
|
|
27
|
+
module.exports = [
|
|
28
|
+
{
|
|
29
|
+
files: ["*.js"],
|
|
30
|
+
plugins: {
|
|
31
|
+
"hex-under": eslintPluginHexUnder,
|
|
32
|
+
},
|
|
33
|
+
rules: {
|
|
34
|
+
"hex-under/hex-under-256": ["error", { limit: 255 }],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
```
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const eslintPluginHexUnder = require("./eslint-plugin-hex-under");
|
|
4
|
+
|
|
5
|
+
module.exports = [
|
|
6
|
+
{
|
|
7
|
+
files: ["*.js"],
|
|
8
|
+
plugins: {
|
|
9
|
+
"hex-under": eslintPluginHexUnder,
|
|
10
|
+
},
|
|
11
|
+
rules: {
|
|
12
|
+
"hex-under/hex-under-256": ["error", { limit: 255 }],
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
];
|
package/hex-under.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "problem",
|
|
4
|
+
docs: {
|
|
5
|
+
description: "Proves that a hexadecimal number must be less than a specified value. Default is 255.",
|
|
6
|
+
},
|
|
7
|
+
fixable: "code",
|
|
8
|
+
schema: [
|
|
9
|
+
{
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
limit: {
|
|
13
|
+
type: "integer",
|
|
14
|
+
minimum: 1,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
additionalProperties: false,
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
messages: {
|
|
21
|
+
valueOver:
|
|
22
|
+
"Value of '{{ variableName }}' must be less than {{ limitPlusOne }}. {{ over255Raw }} is greater than {{ limit }}.",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
create(context) {
|
|
26
|
+
const limit = context.options[0]?.limit || 255;
|
|
27
|
+
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
|
+
limitPlusOne: limit + 1,
|
|
42
|
+
over255Raw: node.init.raw,
|
|
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
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const { RuleTester } = require("eslint");
|
|
2
|
+
const hexUnder256Rule = require("./hex-under");
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
languageOptions: { ecmaVersion: 2015 },
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
ruleTester.run("hex-under-256", hexUnder256Rule, {
|
|
9
|
+
valid: [
|
|
10
|
+
{
|
|
11
|
+
options: [{ limit: 255 }],
|
|
12
|
+
code: "const foo = 0xff;",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
options: [{ limit: 256 }],
|
|
16
|
+
code: "const foo = 0x100;",
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
invalid: [
|
|
20
|
+
{
|
|
21
|
+
code: "const foo = 0x100;",
|
|
22
|
+
output: "const foo = 256;",
|
|
23
|
+
errors: 1,
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
ruleTester.run("hex-under-256", hexUnder256Rule, {
|
|
29
|
+
valid: [
|
|
30
|
+
{
|
|
31
|
+
options: [{ limit: 255 }],
|
|
32
|
+
code: "let foo = 0xff;",
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
invalid: [
|
|
36
|
+
{
|
|
37
|
+
code: "let foo = 0x100;",
|
|
38
|
+
output: "let foo = 256;",
|
|
39
|
+
errors: 1,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
ruleTester.run("hex-under-256", hexUnder256Rule, {
|
|
45
|
+
valid: [
|
|
46
|
+
{
|
|
47
|
+
options: [{ limit: 256 }],
|
|
48
|
+
code: "var foo = 0xff;",
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
invalid: [
|
|
52
|
+
{
|
|
53
|
+
code: "var foo = 0x100;",
|
|
54
|
+
output: "var foo = 256;",
|
|
55
|
+
errors: 1,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
console.log("All tests passed!");
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-hex-under",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"main": "eslint-plugin-hex-under.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"lint": "prettier . --check && eslint .",
|
|
7
|
+
"test": "node hex-under-256.test.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "0xflotus",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"description": "This ESLint rule proves that hex numbers are less than 256",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"eslint": "9.20.1",
|
|
15
|
+
"prettier": "3.5.1"
|
|
16
|
+
}
|
|
17
|
+
}
|