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(eslint-plugin): [no-magic-numbers] add support for enums #543

Merged
merged 3 commits into from Jun 16, 2019
Merged

fix(eslint-plugin): [no-magic-numbers] add support for enums #543

merged 3 commits into from Jun 16, 2019

Conversation

jonathanrdelgado
Copy link
Contributor

@jonathanrdelgado jonathanrdelgado commented May 20, 2019

Fixes #573.

Currently, the following code will fail to lint using the default no-magic-numbers configuration

enum foo {
  SECOND = 1000,
  NUM = '0123456789',
}

One could make the argument that perhaps we instead want to define it in some other way, perhaps using constants and referring to them such as:

const MS_SECONDS = 1000;
enum foo {
  SECOND = MS_SECONDS,
  NUM = '0123456789',
}

However, this will result in a compiler error Computed values are not permitted in an enum with string valued members.ts(2553).

I made these changes to resolve that problem. I tried to follow the existing code pattern as much as possible, however, I am open to changes. I also assumed updating the documentation would be unnecessary for this fix, let me know if we would like a change there and I'll handle it.

Thanks for your time!

@codecov
Copy link

codecov bot commented May 20, 2019

Codecov Report

Merging #543 into master will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #543      +/-   ##
==========================================
+ Coverage   94.21%   94.22%   +<.01%     
==========================================
  Files         105      105              
  Lines        4341     4345       +4     
  Branches     1193     1195       +2     
==========================================
+ Hits         4090     4094       +4     
  Misses        146      146              
  Partials      105      105
Impacted Files Coverage Δ
...ckages/eslint-plugin/src/rules/no-magic-numbers.ts 94.11% <100%> (+0.78%) ⬆️

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.

thanks for working on this!

please add an option to gate this functionality with a default set to off, and document it in the rule's readme

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
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.

awesome, thanks!
LGTM

@bradzacher bradzacher added 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready enhancement: plugin rule option New rule option for an existing eslint-plugin rule labels May 22, 2019
@bradzacher bradzacher changed the title fix(eslint-plugin): add support for TS enums on no-magic-numbers rule fix(eslint-plugin): [no-magic-numbers] add support for enums Jun 16, 2019
@bradzacher bradzacher merged commit 5c40d01 into typescript-eslint:master Jun 16, 2019
@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: plugin rule option New rule option for an existing eslint-plugin rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[@typescript-eslint/no-magic-numbers] enum's are treated as "magic numbers"
2 participants