New Magic Numbers rule is not usable in practice #1883
Comments
That is a fair assessment. I think the rule should be more lenient. Tagging @SimonSchick |
Sorry I missed that specific use case, I'll look into it tomorrow. |
I personally think it's fine the way it is now. You'd just need to define more things, but I think that's in the spirit of this rule.
|
That's rather cumbersome, some flexibility would be nice. |
This rule in general seems overly strict, which is why I've just decided to disable the rule. I say just disable the rule if it's something that doesn't work well for your team or workflow. All the rules are optional. |
An idea for making this more flexible could be to allow magic numbers if there's a block comment (with content) explaining what the magic number is directly following the number, like what TypeScript already does when compiling values from a const MILLIS_IN_SECOND = 1000 /* milliseconds in second */; // Allowed
let secondsInMinute: number = 60 * MILLIS_IN_SECOND; // Not allowed
let secondsInMinute: number = 60 /**/ * MILLIS_IN_SECOND; // Not allowed
let time: number = 60 /* seconds in minute */ * MILLIS_IN_SECOND; // Allowed
let idx: number = 'Slashes are awesome // !'.lastIndexOf('/') - 2; // Not allowed
let idx: number = 'Slashes are awesome // !'.lastIndexOf('/') - 2 /* get character before / */; // Allowed |
@adidahiya Another fair approach would be to allow relatively "common", low numbers. Say, integers in a This would make situations like the one below to not be problem for the linter. expect(doAction).toHaveBeenCalledTimes(3);
// [tslint] 'magic numbers' are not allowed (no-magic-numbers) My common sense tells me that const numberOfTimesDoActionShouldHaveBeenCalled = 3;
expect(doAction).toHaveBeenCalledTimes(numberOfTimesDoActionShouldHaveBeenCalled); However, this: expect(doAction).toHaveBeenCalledTimes(1398); Should raise some eyebrows. |
Another example is when trying to have constants that hold values for a pager.
It would be a nice feature if rule would accept |
The PR I opened would allow this const pageSizeOptions = [10, 50, 100]; /* any comment at all */ |
What is the status of this issue? Are the PRs being merged? Example with |
Milliseconds are still problem, could this be fixed? I think that time constants are used a lot. :) |
I believe battling a tslint rule severity by adding a code smell is counterproductive and desperate. The rule is driving me nuts sometimes too. Currently there are 2 options for this rule in tslint e.g. |
@aervin is there any param to allow cases like you mentioned? |
Closing in favor of eslint rule: typescript-eslint/typescript-eslint#934, typescript-eslint/typescript-eslint#938 |
🤖 Beep boop! 👉 TSLint is deprecated 👈 (#4534) and you should switch to typescript-eslint! 🤖 🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋 |
Bug Report
4.1.0
2.0.10
Example: a 5 minute read timeout
The only thing that works at the moment:
The text was updated successfully, but these errors were encountered: