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

New: no-loss-of-precision (fixes #11279) #12747

Merged
merged 28 commits into from May 20, 2020
Merged

New: no-loss-of-precision (fixes #11279) #12747

merged 28 commits into from May 20, 2020

Conversation

jmoore914
Copy link
Contributor

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[ ] Bug fix (template)
[ X] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

#11279

What changes did you make? (Give an overview)

  • Added lib/rules/no-loss-of-precision.js for implementation of new rule
  • Added tests/lib/rules/no-loss-of-precision.js for tests of new rule
  • Added docs/rules/no-loss-of-precision.md for documentation of new rule
  • Updated lib/rules/index.js to add rule to index
  • Updated conf/eslint-recommended to make rule recommended
  • Updated tools/rule-types.json to set the rule as a problem

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

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Jan 5, 2020
@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Jan 9, 2020
Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I left a few suggestions.

conf/eslint-recommended.js Outdated Show resolved Hide resolved
docs/rules/no-loss-of-precision.md Outdated Show resolved Hide resolved
lib/rules/no-loss-of-precision.js Outdated Show resolved Hide resolved
lib/rules/no-loss-of-precision.js Outdated Show resolved Hide resolved
lib/rules/no-loss-of-precision.js Outdated Show resolved Hide resolved
lib/rules/no-loss-of-precision.js Outdated Show resolved Hide resolved
lib/rules/no-loss-of-precision.js Outdated Show resolved Hide resolved
lib/rules/no-loss-of-precision.js Outdated Show resolved Hide resolved
lib/rules/no-loss-of-precision.js Outdated Show resolved Hide resolved
lib/rules/no-loss-of-precision.js Outdated Show resolved Hide resolved
@kaicataldo kaicataldo changed the title New: noLossOfPrecision (fixes #11279) New: no-loss-of-precision (fixes #11279) Jan 27, 2020
function isBaseTen(node) {
const prefixes = ["0x", "0X", "0b", "0B", "0o", "0O"];

return prefixes.every(prefix => !node.raw.startsWith(prefix)) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will treat the literal 0 as octal, which is incorrect although it doesn't affect the result of the precision check.


return prefixes.every(prefix => !node.raw.startsWith(prefix)) &&
(!node.raw.startsWith("0") ||
node.raw.startsWith("0e") ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be incorrect for something like 0E5.

(!node.raw.startsWith("0") ||
node.raw.startsWith("0e") ||
node.raw.startsWith("0.") ||
!isOctalDigitsOnly(node.raw));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be simplified to:

return prefixes.every(prefix => !node.raw.startsWith(prefix)) &&
    !/^0[0-7]+$/.test(node.raw)

@mdjermanovic mdjermanovic linked an issue Feb 9, 2020 that may be closed by this pull request
@nzakas
Copy link
Member

nzakas commented May 19, 2020

@kaicataldo has your concerns been addressed?

Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@nzakas nzakas merged commit c636d57 into eslint:master May 20, 2020
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Nov 17, 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 Nov 17, 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 feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: Disallow number literals that lose precision
5 participants