Skip to content

Commit

Permalink
Docs: Document the exception in no-unsafe-negation (#12161)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and kaicataldo committed Aug 26, 2019
1 parent ca658fb commit 6abc7b7
Showing 1 changed file with 29 additions and 3 deletions.
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

0 comments on commit 6abc7b7

Please sign in to comment.