-
-
Notifications
You must be signed in to change notification settings - Fork 387
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
Rule proposal: Prefer Number
methods
#594
Comments
I never see anyone use Relate issue on eslint eslint/eslint#8894
|
Maybe we could just try enforcing |
One note, please use this
|
Name suggestion? How about |
|
Should we suggest |
👍
No, |
I would like to see a reasoning for why number properties are better, (and not just because of global-phobia) I came here for the isNaN i can understand it's not the same as
There may just be usecases where you would prefer to use one over the other, just because of it's differences |
No one is forcing you to use this rule. You can opt out of it.
There's an option to not check
Consistency. When there are some that are better and should be preferred, it's better for readability to consistency use the |
That's what all linter say. |
Reason agains Number.parseInt Don't work in IE But it's pretty much dead now anyway |
Isn't all these properties not available in IE? If you are targeting IE, you can't check any of them. |
I think we can add option for |
There are cases when you, for example, parse query params and want to be able to convert a string into a number if possible: So you would do:
|
This is why we didn't auto fix this case https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-number-properties.md |
That's the point of this rule, you should refactor to use |
What is the suggested refactor though? This? - if (isNaN(numberOrIdk)) {
+ if (typeof numberOrIdk !== number || Number.isNaN(numberOrIdk)) { If so, it's quite verbose. The lack of an autofix doesn't exactly justify a "bad" rule. In this case, if my code is equivalent, then it could still technically autofix it. |
I think - if (isNaN(numberOrIdk)) {
+ if (Number.isNaN(Number(numberOrIdk))) { is more correct. |
Fail
Pass
This rule cannot be auto-fixable as the globals methods have slightly different behavior. However, it should use the ESLint suggestions API.
Should we also enforce
Number.NaN
overNaN
? They equivalent. I'm leaning towards "yes", for consistency, and also less use of floating globals.The text was updated successfully, but these errors were encountered: