Skip to content
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

Docs: Document the exception in no-unsafe-negation #12161

Merged
merged 1 commit into from Aug 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 29 additions & 3 deletions docs/rules/no-unsafe-negation.md
Expand Up @@ -39,10 +39,36 @@ if (!(key in object)) {
if (!(obj instanceof Ctor)) {
// obj is not an instance of Ctor
}
```

### Exception

For rare situations when negating the left operand is intended, this rule allows an exception.
If the whole negation is explicitly wrapped in parentheses, the rule will not report a problem.

Examples of **correct** code for this rule:

```js
/*eslint no-unsafe-negation: "error"*/

if ((!foo) in object) {
// allowed, because the negation is explicitly wrapped in parentheses
// it is equivalent to (foo ? "false" : "true") in object
// this is allowed as an exception for rare situations when that is the intended meaning
}

if(("" + !foo) in object) {
// you can also make the intention more explicit, with type conversion
}
```

Examples of **incorrect** code for this rule:

```js
/*eslint no-unsafe-negation: "error"*/

if(("" + !key) in object) {
// make operator precedence and type conversion explicit
// in a rare situation when that is the intended meaning
if (!(foo) in object) {
// this is not an allowed exception
}
```

Expand Down