Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: make radix rule stricter #12608

Merged
merged 6 commits into from Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/radix.md
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
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
41 changes: 36 additions & 5 deletions tests/lib/rules/radix.js
Expand Up @@ -18,6 +18,9 @@ ruleTester.run("radix", rule, {

valid: [
"parseInt(\"10\", 10);",
"parseInt(\"10\", 0x10);",
"parseInt(\"10\", 1.6e1);",
"parseInt(\"10\", 10.0);",
"parseInt(\"10\", foo);",
"Number.parseInt(\"10\", foo);",
{
Expand Down Expand Up @@ -75,35 +78,49 @@ 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\", 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 +146,20 @@ ruleTester.run("radix", rule, {
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