Skip to content

unicorn/consistent-existence-index-check Style

🛠️ An auto-fix is available for this rule.

What it does

Enforce consistent style for element existence checks with indexOf(), lastIndexOf(), findIndex(), and findLastIndex()

Why is this bad?

This rule is only meant to enforce a specific style and make comparisons more clear.

Examples

Examples of incorrect code for this rule:

javascript
const index = foo.indexOf("bar");
if (index < 0) {
}
javascript
const index = foo.indexOf("bar");
if (index >= 0) {
}

Examples of correct code for this rule:

javascript
const index = foo.indexOf("bar");
if (index === -1) {
}
javascript
const index = foo.indexOf("bar");
if (index !== -1) {
}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/consistent-existence-index-check
json
{
  "rules": {
    "unicorn/consistent-existence-index-check": "error"
  }
}

References

Released under the MIT License.