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

feat(eslint-plugin): add no-magic-numbers rule #373

Merged
merged 6 commits into from May 8, 2019

Conversation

scottohara
Copy link
Contributor

@scottohara scottohara commented Mar 25, 2019

Extends the no-magic-numbers rule from ESlint core with an additional option (ignoreNumericLiteralTypes) to ignore Typescript numeric literal types when checking for magic numbers.

Fixes #99

The new option allows for type definitions such as the ones listed below to be considered valid.

Currently, the base no-magic-numbers rule currently considers these type definitions as errors, despite there being no valid way (at least that I'm aware of) to define numeric literal types that won't violate the magic numbers rule.

Examples of allowed code

/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": true }]*/

type SmallPrimes = 2 | 3 | 5 | 7 | 11;   // handles union types

type Step = 1 | -1;  // handles negative numbers

type MeaningOfLife = 42;  // handles single numbers

Please note that this is my first time contributing a TS rule, so if I've missed anything or if there is a preferred way to implement this, suggestions are welcome.

Also note, unsure if this should be a "recommended" rule or not.

@codecov
Copy link

codecov bot commented Mar 25, 2019

Codecov Report

Merging #373 into master will decrease coverage by 0.02%.
The diff coverage is 93.33%.

@@            Coverage Diff             @@
##           master     #373      +/-   ##
==========================================
- Coverage   95.76%   95.74%   -0.03%     
==========================================
  Files          78       79       +1     
  Lines        3493     3523      +30     
  Branches      963      975      +12     
==========================================
+ Hits         3345     3373      +28     
  Misses         53       53              
- Partials       95       97       +2
Impacted Files Coverage Δ
...ckages/eslint-plugin/src/rules/no-magic-numbers.ts 93.33% <93.33%> (ø)

@scottohara scottohara force-pushed the no-magic-numbers branch 2 times, most recently from 35c1237 to ac78113 Compare March 26, 2019 05:40
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

Code itself LGTM. Few (very) minor nits.

Haven't gotten around to doing it for the existing rules yet, but one thing we want to do with extension rules is to stop copying the documentation from eslint.
Whilst it does force users to click through for more documentation, it saves us from having to track changes on eslint master to update our docs.

Could you please change the docs to something akin to following structure:

  1. Header
  2. Rule details
    1. The description
    2. Link to the existing rule documentation.
  3. Rule Changes
    1. Added Options.

Thanks for your contribution!

packages/eslint-plugin/src/rules/no-magic-numbers.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/no-magic-numbers.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/no-magic-numbers.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/no-magic-numbers.ts Outdated Show resolved Hide resolved
@scottohara
Copy link
Contributor Author

Thanks @bradzacher for the positive feedback.

Regarding the docs format, I agree that not duplicating the core docs is a good move; so I've updated the docs with what I believe you were recommending.

Hope I've understood correctly.

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

everything looks good, one last fix.

packages/eslint-plugin/src/rules/no-magic-numbers.ts Outdated Show resolved Hide resolved
@bradzacher bradzacher added 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready enhancement: new plugin rule New rule request for eslint-plugin labels Apr 11, 2019
@bradzacher bradzacher merged commit 43fa09c into typescript-eslint:master May 8, 2019
@scottohara scottohara deleted the no-magic-numbers branch May 8, 2019 22:03
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready enhancement: new plugin rule New rule request for eslint-plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[no-magic-number] fails on numeric literal types
3 participants