Skip to content

Commit cd80077

Browse files
committed
feat: add rule to prove that a hexadecimal number is less than 256
0 parents  commit cd80077

8 files changed

+1255
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# eslint-plugin-hex-under
2+
3+
## hex-under-256
4+
5+
This ESLint plugin proves, if you use hexadecimal numbers in your variable declaration code, that its value is less than 256. If you use a hexadecimal number greater or equal 256, it will be transformed to its decimal value.
6+
7+
### Example
8+
9+
```js
10+
// valid
11+
const signal = 0xef;
12+
13+
// invalid
14+
const signal = 0x21b;
15+
16+
// This will be transformed to:
17+
const signal = 539;
18+
```

Diff for: eslint-plugin-hex-under.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const hexUnder256Rule = require("./hex-under-256");
2+
const plugin = { rules: { "hex-under-256": hexUnder256Rule } };
3+
module.exports = plugin;

Diff for: eslint.config.js

+15
Original file line numberDiff line numberDiff line change
@@ -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",
13+
},
14+
},
15+
];

Diff for: hex-under-256.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = {
2+
meta: {
3+
type: "problem",
4+
docs: {
5+
description: "Proves that a hexadecimal number must be less than 256.",
6+
},
7+
fixable: "code",
8+
schema: [],
9+
},
10+
create(context) {
11+
return {
12+
VariableDeclarator(node) {
13+
if (
14+
node.parent.kind === "const" ||
15+
node.parent.kind === "let" ||
16+
node.parent.kind === "var"
17+
) {
18+
if (
19+
node.init &&
20+
node.init.type === "Literal" &&
21+
node.init.raw.startsWith("0x")
22+
) {
23+
const limit = 0xff;
24+
if (parseInt(node.init.value) > limit) {
25+
context.report({
26+
node,
27+
message:
28+
"Value of '{{ variableName }}' must be less than 256. {{ over255Raw }} is greater than 255.",
29+
data: {
30+
over255Raw: node.init.raw,
31+
variableName: node.id.name,
32+
},
33+
fix(fixer) {
34+
return fixer.replaceText(
35+
node.init,
36+
parseInt(node.init.value),
37+
);
38+
},
39+
});
40+
}
41+
}
42+
}
43+
},
44+
};
45+
},
46+
};

Diff for: hex-under-256.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { RuleTester } = require("eslint");
2+
const hexUnder256Rule = require("./hex-under-256");
3+
4+
const ruleTester = new RuleTester({
5+
languageOptions: { ecmaVersion: 2015 },
6+
});
7+
8+
ruleTester.run("hex-under-256", hexUnder256Rule, {
9+
valid: [
10+
{
11+
code: "const foo = 0xff;",
12+
},
13+
],
14+
invalid: [
15+
{
16+
code: "const foo = 0x100;",
17+
output: "const foo = 256;",
18+
errors: 1,
19+
},
20+
],
21+
});
22+
23+
ruleTester.run("hex-under-256", hexUnder256Rule, {
24+
valid: [
25+
{
26+
code: "let foo = 0xff;",
27+
},
28+
],
29+
invalid: [
30+
{
31+
code: "let foo = 0x100;",
32+
output: "let foo = 256;",
33+
errors: 1,
34+
},
35+
],
36+
});
37+
38+
ruleTester.run("hex-under-256", hexUnder256Rule, {
39+
valid: [
40+
{
41+
code: "var foo = 0xff;",
42+
},
43+
],
44+
invalid: [
45+
{
46+
code: "var foo = 0x100;",
47+
output: "var foo = 256;",
48+
errors: 1,
49+
},
50+
],
51+
});
52+
53+
console.log("All tests passed!");

0 commit comments

Comments
 (0)