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

@mdjermanovic mdjermanovic commented Dec 11, 2019

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?

@btmills btmills merged commit 45364af into master Dec 20, 2019
18 checks passed
18 checks passed
@github-actions
Verify Files
Details
@github-actions
Test (ubuntu-latest, 13.x)
Details
@github-actions
Test (ubuntu-latest, 12.x)
Details
@github-actions
Test (ubuntu-latest, 10.x)
Details
@github-actions
Test (ubuntu-latest, 8.x)
Details
@github-actions
Test (ubuntu-latest, 8.10.0)
Details
@github-actions
Test (windows-latest, 12.x)
Details
@github-actions
Test (macOS-latest, 12.x)
Details
@github-actions
Browser Test
Details
@eslint-deprecated
commit-message Commit message follows guidelines
Details
@azure-pipelines
continuous-integration Build #20191211.1 succeeded
Details
@azure-pipelines
continuous-integration (Test on Node.js 10 (Linux)) Test on Node.js 10 (Linux) succeeded
Details
@azure-pipelines
continuous-integration (Test on Node.js 12 (Linux)) Test on Node.js 12 (Linux) succeeded
Details
@azure-pipelines
continuous-integration (Test on Node.js 12 (Windows)) Test on Node.js 12 (Windows) succeeded
Details
@azure-pipelines
continuous-integration (Test on Node.js 12 (macOS)) Test on Node.js 12 (macOS) succeeded
Details
@azure-pipelines
continuous-integration (Test on Node.js 8 (Linux)) Test on Node.js 8 (Linux) succeeded
Details
licence/cla Contributor License Agreement is signed.
Details
@eslint-deprecated
release-monitor No patch release is pending
Details
@btmills btmills deleted the prefernumericliterals-bigintcrash branch Dec 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants