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): [no-magic-numbers] add ignoreReadonlyClassProperties option #938

Conversation

a-tarasyuk
Copy link
Contributor

Fixes #934

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @a-tarasyuk!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint

@a-tarasyuk a-tarasyuk force-pushed the feature/934-add-ignore-readonly-class-properties-option branch from 0e51fa2 to 428fa17 Compare September 3, 2019 14:58
@a-tarasyuk a-tarasyuk force-pushed the feature/934-add-ignore-readonly-class-properties-option branch from 428fa17 to 95b3032 Compare September 3, 2019 15:04
@bradzacher bradzacher added the enhancement: plugin rule option New rule option for an existing eslint-plugin rule label Sep 3, 2019
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 doing this!
Code LGTM.
Could you please add this to the rule readme as well?

packages/eslint-plugin/src/rules/no-magic-numbers.ts Outdated Show resolved Hide resolved
@a-tarasyuk a-tarasyuk force-pushed the feature/934-add-ignore-readonly-class-properties-option branch from 80a02b9 to 3bdadee Compare September 3, 2019 15:42
@a-tarasyuk
Copy link
Contributor Author

@bradzacher updated docs.

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.

LGTM - thanks for doing this!

@bradzacher bradzacher added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Sep 3, 2019
@glen-84
Copy link
Contributor

glen-84 commented Sep 3, 2019

Hmm.

Shouldn't this be allowed by default? In fact, shouldn't any assignment of a number to a named variable be allowed? Isn't the definition of a magic number a number that is "unnamed"?

For example:

class Example {
    // Not magic.
    public static readonly SECONDS = 60;
    // Magic ... what is one of the 60s referring to?
    public static readonly MINUTES = 60 * 60;
}

(it doesn't even need to be static or readonly)

... confirmed with TSLint:

image

This rule (at least TSLint's version), is already quite difficult to work with, so we shouldn't make it more difficult.

@phaux
Copy link
Contributor

phaux commented Sep 3, 2019

I think it shouldn't even need an option. readonly class props should be simply handled the same as const declarations

@bradzacher
Copy link
Member

I think an option makes sense.

Not everyone might want to allow readonly props as magic number holders.
The rule has been live for a number of months, and #934 is the first time it's been asked, so that would suggest this is the case.

Having it off by default is partially for compatibility, otherwise it relaxes the rule's default checks - which is a breaking change.

@glen-84
Copy link
Contributor

glen-84 commented Sep 4, 2019

So basically, by default:

var x = 5;

^ This is valid.

readonly x = 5;

^ But this is not?

That doesn't really make sense to me.

From the TSLint description: "Disallows the use [of] constant number values outside of variable assignments."

That's all it's supposed to do.

The rule has been live for a number of months, and #934 is the first time it's been asked.

2nd time. I was going to open the same issue, but decided against it because the rule is (in general) quite difficult to use, especially on an existing codebase.

Also keep in mind that it is not included in the recommended configurations, and I'm pretty sure that it's one of the lesser-used rules.

@phaux
Copy link
Contributor

phaux commented Sep 5, 2019

In the end it doesn't matter that much, because you can always replace a readonly class prop with a const. Having number constant as instance member is unnecessary overhead.

@bradzacher
Copy link
Member

bradzacher commented Sep 5, 2019

all of the options added by this plugin are currently turned off by default.
I don't use this rule because I don't agree with it personally, and nobody asked beforehand, so it wasn't on my radar to turn the options on to default for the v2 release.

Can certainly consider turning them on for v3, but I don't see the point of holding back this PR until we are ready for another major bump.

If you feel strongly that the options should be on by default, happy for you to raise an issue so we can track it, and get it in with the v3 release.

@glen-84
Copy link
Contributor

glen-84 commented Sep 5, 2019

Having number constant as instance member is unnecessary overhead.

It would be static.

If you feel strongly that the options should be on by default, happy for you to raise an issue so we can track it, and get it in with the v3 release.

Well it seems that I'm the only one with this opinion, so I'll just leave it then.

@JamesHenry
Copy link
Member

I’d be on board with changing the default of the option in v3, I think readonly properties clearly satisfy the same idea as consts when it comes to identifying otherwise “magic numbers”, but agree it would be a breaking change to do it now

Copy link
Member

@JamesHenry JamesHenry left a comment

Choose a reason for hiding this comment

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

Thanks!

@JamesHenry JamesHenry removed the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Sep 12, 2019
@codecov
Copy link

codecov bot commented Sep 12, 2019

Codecov Report

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

@@            Coverage Diff             @@
##           master     #938      +/-   ##
==========================================
+ Coverage   94.13%   94.13%   +<.01%     
==========================================
  Files         115      115              
  Lines        5012     5017       +5     
  Branches     1400     1403       +3     
==========================================
+ Hits         4718     4723       +5     
  Misses        166      166              
  Partials      128      128
Impacted Files Coverage Δ
...ckages/eslint-plugin/src/rules/no-magic-numbers.ts 92.5% <100%> (+1.07%) ⬆️

@JamesHenry JamesHenry merged commit aeea4cd into typescript-eslint:master Sep 12, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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.

[no-magic-numbers] False positive for readonly properties
5 participants