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 2 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
6 changes: 4 additions & 2 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 validRadixNumbers = 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" && !validRadixNumbers.has(radix.value)) ||
(radix.type === "Identifier" && radix.name === "undefined")
);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/radix.js
Expand Up @@ -107,6 +107,13 @@ ruleTester.run("radix", rule, {
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", 37);",
errors: [{
message: "Invalid radix parameter.",
type: "CallExpression"
}]
},
{
code: "Number.parseInt();",
errors: [{
Expand All @@ -129,6 +136,13 @@ ruleTester.run("radix", rule, {
type: "CallExpression"
}]
},
{
code: "Number.parseInt(\"10\", 37);",
errors: [{
message: "Invalid radix parameter.",
type: "CallExpression"
}]
},
{
code: "parseInt(\"10\", 10);",
options: ["as-needed"],
Expand Down