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

Fix: prefer-numeric-literals doesn't check types of literal arguments #12655

Merged
merged 1 commit into from Dec 20, 2019

Conversation

mdjermanovic
Copy link
Member

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 rule disallows calls to parseInt() or Number.parseInt() if called with two arguments: a string; and a radix option of 2 (binary), 8 (octal), or 16 (hexadecimal).

This PR fixes the following:

  1. The rule didn't check the type of the first literal argument.

It did check if it's a Literal. Now it also checks if it's a string 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.

/* eslint prefer-numeric-literals: error */

parseInt(1e8, 2); // fixed to 0b100000000;

Demo Link

  1. The rule didn't check the type of the second literal argument.

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 arguments 2, 8, and 16.

Targeting string radix arguments is inconsistent with the radix rule, which reports these as invalid (Demo Link).

Bigint radix argument causes ESLint to crash when the rule tries to evaluate the expression:

/* eslint prefer-numeric-literals: error */

parseInt('11', 2n); // TypeError: Cannot convert a BigInt value to a number.

Demo Link

  1. The rule used objects as maps.

Demo Link

What changes did you make? (Give an overview)

The rule now targets only parseInt and Number.parseInt that have a string literal as the first argument and a number literal as the second argument.

Also, converted objects used as maps to Map.

Is there anything you'd like reviewers to focus on?

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Dec 11, 2019
@mdjermanovic mdjermanovic added bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Dec 11, 2019
@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 12, 2019
@btmills btmills merged commit 45364af into master Dec 20, 2019
@btmills btmills deleted the prefernumericliterals-bigintcrash branch December 20, 2019 20:13
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jun 19, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jun 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants