Skip to content

Commit

Permalink
Breaking: make radix rule stricter (#12608)
Browse files Browse the repository at this point in the history
* Update: make `radix` rule stricter

* style: remove extra blank line

* Update message, more tests

* Variable name & exponential notation test

* test `10.0`

* Add tests `1` `2` `36`
  • Loading branch information
fisker authored and kaicataldo committed Jan 20, 2020
1 parent e03a7b3 commit c2217c0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/rules/radix.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var num = parseInt(someValue);

var num = parseInt("071", "abc");

var num = parseInt("071", 37);

var num = parseInt();
```

Expand Down
8 changes: 5 additions & 3 deletions lib/rules/radix.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const astUtils = require("./utils/ast-utils");
const MODE_ALWAYS = "always",
MODE_AS_NEEDED = "as-needed";

const validRadixValues = new Set(Array.from({ length: 37 - 2 }, (_, index) => index + 2));

/**
* Checks whether a given variable is shadowed or not.
* @param {eslint-scope.Variable} variable A variable to check.
Expand Down Expand Up @@ -47,14 +49,14 @@ function isParseIntMethod(node) {
*
* The following values are invalid.
*
* - A literal except numbers.
* - A literal except integers between 2 and 36.
* - undefined.
* @param {ASTNode} radix A node of radix to check.
* @returns {boolean} `true` if the node is valid.
*/
function isValidRadix(radix) {
return !(
(radix.type === "Literal" && typeof radix.value !== "number") ||
(radix.type === "Literal" && !validRadixValues.has(radix.value)) ||
(radix.type === "Identifier" && radix.name === "undefined")
);
}
Expand Down Expand Up @@ -128,7 +130,7 @@ module.exports = {
} else if (!isValidRadix(args[1])) {
context.report({
node,
message: "Invalid radix parameter."
message: "Invalid radix parameter, must be an integer between 2 and 36."
});
}
break;
Expand Down
57 changes: 52 additions & 5 deletions tests/lib/rules/radix.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ ruleTester.run("radix", rule, {

valid: [
"parseInt(\"10\", 10);",
"parseInt(\"10\", 2);",
"parseInt(\"10\", 36);",
"parseInt(\"10\", 0x10);",
"parseInt(\"10\", 1.6e1);",
"parseInt(\"10\", 10.0);",
"parseInt(\"10\", foo);",
"Number.parseInt(\"10\", foo);",
{
Expand Down Expand Up @@ -75,35 +80,56 @@ ruleTester.run("radix", rule, {
{
code: "parseInt(\"10\", null);",
errors: [{
message: "Invalid radix parameter.",
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", undefined);",
errors: [{
message: "Invalid radix parameter.",
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", true);",
errors: [{
message: "Invalid radix parameter.",
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", \"foo\");",
errors: [{
message: "Invalid radix parameter.",
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", \"123\");",
errors: [{
message: "Invalid radix parameter.",
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", 1);",
errors: [{
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", 37);",
errors: [{
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", 10.5);",
errors: [{
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
Expand All @@ -129,6 +155,27 @@ ruleTester.run("radix", rule, {
type: "CallExpression"
}]
},
{
code: "Number.parseInt(\"10\", 1);",
errors: [{
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "Number.parseInt(\"10\", 37);",
errors: [{
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "Number.parseInt(\"10\", 10.5);",
errors: [{
message: "Invalid radix parameter, must be an integer between 2 and 36.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", 10);",
options: ["as-needed"],
Expand Down

0 comments on commit c2217c0

Please sign in to comment.