-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-magic-numbers] add ignoreReadonlyClassProperties option #938
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
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 |
0e51fa2
to
428fa17
Compare
428fa17
to
95b3032
Compare
There was a problem hiding this 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?
…ipt-eslint into feature/934-add-ignore-readonly-class-properties-option
80a02b9
to
3bdadee
Compare
@bradzacher updated docs. |
There was a problem hiding this 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!
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 ... confirmed with TSLint: This rule (at least TSLint's version), is already quite difficult to work with, so we shouldn't make it more difficult. |
I think it shouldn't even need an option. |
I think an option makes sense. Not everyone might want to allow readonly props as magic number holders. Having it off by default is partially for compatibility, otherwise it relaxes the rule's default checks - which is a breaking change. |
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.
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. |
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. |
all of the options added by this plugin are currently turned off by default. 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. |
It would be
Well it seems that I'm the only one with this opinion, so I'll just leave it then. |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Codecov Report
@@ 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
|
Fixes #934