question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The `no-magic-numbers` rule should ignore indexOf === -1

See original GitHub issue

What 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:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
michaelficarracommented, Apr 11, 2016

@d4rkr00t Instead of whitelisting -1 everywhere, you might want to define a constant const INDEXOF_NOT_FOUND = -1; and compare against that.

1reaction
lo1tumacommented, Apr 4, 2016

I don’t think a commonly used pattern makes the number less magic. The meaning of this number is still implicit.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found