The `no-magic-numbers` rule should ignore indexOf === -1
See original GitHub issueWhat version are you using?
2.6.0
What did you do?
/*eslint no-magic-numbers: ["error"]*/
var data = ['foo', 'bar', 'baz'];
var key = 'bar';
var hasKey = data.indexOf(key) !== -1;
What happened?
error No magic number: -1 no-magic-numbers
What did you expect to happen?
Consider indexOf(something) === -1;
as ok, because it’s a common pattern for checking whether something
is in array.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
no-magic-numbers - ESLint - Pluggable JavaScript Linter
The no-magic-numbers rule aims to make code more readable and refactoring easier by ensuring that special numbers are declared as constants to make...
Read more >Disabling an ESLint rule for a specific file not working
In the codebase I'm working on the no-magic-numbers rule is set for the whole project. Thing is that I need to disable that...
Read more >Java static code analysis: Magic numbers should not be used
That is why magic numbers must be demystified by first being assigned to clearly named variables before being used. -1, 0 and 1...
Read more >Android - Coding Rules
That is why magic numbers must be demystified by first being assigned to clearly named variables before being used. -1, 0 and 1...
Read more >Linters | golangci-lint
Name Description Presets Since
asasalint ⚙️ check for pass any as any in variadic func(...any) bugs 1.47.0
bidichk ⚙️ Checks for dangerous unicode character sequences...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@d4rkr00t Instead of whitelisting
-1
everywhere, you might want to define a constantconst INDEXOF_NOT_FOUND = -1;
and compare against that.I don’t think a commonly used pattern makes the number less magic. The meaning of this number is still implicit.