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
New: prefer-numeric-literals
rule (fixes #6068)
#7029
Conversation
@azhang496, thanks for your PR! By analyzing the annotation information on this pull request, we identified @nzakas, @alberto and @kaicataldo to be potential reviewers |
LGTM |
```js | ||
/*eslint prefer-numeric-literals: "error"*/ | ||
|
||
parseInt(\"111110111\", 2) === 503; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the red/syntax highlighting issue is the \"
- just "
should be ok
LGTM |
@@ -0,0 +1,41 @@ | |||
# disallow `parseInt` in favor of binary, octal, and hexadecimal literals (prefer-numeric-literals) | |||
|
|||
Binary and octal literals were introduced in ES6. Prior to this, `parseInt` was used to turn binary and octal strings into integers. This rule encourages use of binary, octal, and hexadecimal literals instead of `parseInt`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use parseInt()
with parens throughout? We try to stick to that convention in the docs.
This looks really good, just a few small things to clean up on the docs. |
LGTM |
) { | ||
context.report({ | ||
node, | ||
message: "Use {{radixName}} literals instead of parseInt", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two small things - could we use parseInt()
here to be consistent with the docs? Makes it clear that it's a function as well. Also, would you mind adding a period to the end of the error message? A while back we went through to try to make it consistent: #6762
So I'm thinking: "Use {{radixName}} literals instead of parseInt()."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes good catch on the parseInt()
!
One nit pick in the error message, but otherwise this looks great to me. Thanks for all the recent contributions, @azhang496! |
LGTM |
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow `parseInt` in favor of binary, octal, and hexadecimal literals", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for all the nitpicking - caught one more thing! This should match the other descriptions and say parseInt()
.
LGTM |
@@ -0,0 +1,49 @@ | |||
# disallow `parseInt()` in favor of binary, octal, and hexadecimal literals (prefer-numeric-literals) | |||
|
|||
The `parseInt()` function can be used to turn binary, octal, and hexadecimal strings into integers. As binary, octal, and hexadecimal literals are supported in ES6, this rule encourages use of those numeric literals instead of `parseInt()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe rule encourages the use
👍 |
LGTM. Thanks for contributing to ESLint! |
LGTM, thanks @azhang496! |
ref: #6068