Fix: prefer-numeric-literals doesn't check types of literal arguments #12655
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is the purpose of this pull request? (put an "X" next to item)
[X] Bug fix
This PR can produce only fewer warnings in
prefer-numeric-literals
.From the documentation:
This PR fixes the following:
It did check if it's a
Literal
. Now it also checks if it's astring
literal.This probably didn't cause some notable problems, but it isn't by the spec (which is explicit about the first argument), and it might be unwanted in some cases.
Demo Link
The only check was whether the literal's
value
, when coerced to string, exists as a key in the object map, so it ended up targeting the following radix arguments:2
,8
,16
,"2"
,"8"
,"16"
,2n
,8n
,16n
It's changed now to target only
number
arguments2
,8
, and16
.Targeting
string
radix arguments is inconsistent with theradix
rule, which reports these as invalid (Demo Link).Bigint radix argument causes ESLint to crash when the rule tries to evaluate the expression:
Demo Link
Demo Link
What changes did you make? (Give an overview)
The rule now targets only
parseInt
andNumber.parseInt
that have astring
literal as the first argument and anumber
literal as the second argument.Also, converted objects used as maps to
Map
.Is there anything you'd like reviewers to focus on?