]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-unsafe-negation.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-unsafe-negation.md
1 # disallow negating the left operand of relational operators (no-unsafe-negation)
2
3 Just as developers might type `-a + b` when they mean `-(a + b)` for the negative of a sum, they might type `!key in object` by mistake when they almost certainly mean `!(key in object)` to test that a key is not in an object. `!obj instanceof Ctor` is similar.
4
5 ## Rule Details
6
7 This rule disallows negating the left operand of the following relational operators:
8
9 - [`in` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in).
10 - [`instanceof` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof).
11
12 Examples of **incorrect** code for this rule:
13
14 ```js
15 /*eslint no-unsafe-negation: "error"*/
16
17 if (!key in object) {
18 // operator precedence makes it equivalent to (!key) in object
19 // and type conversion makes it equivalent to (key ? "false" : "true") in object
20 }
21
22 if (!obj instanceof Ctor) {
23 // operator precedence makes it equivalent to (!obj) instanceof Ctor
24 // and it equivalent to always false since boolean values are not objects.
25 }
26 ```
27
28 Examples of **correct** code for this rule:
29
30 ```js
31 /*eslint no-unsafe-negation: "error"*/
32
33 if (!(key in object)) {
34 // key is not in object
35 }
36
37 if (!(obj instanceof Ctor)) {
38 // obj is not an instance of Ctor
39 }
40 ```
41
42 ### Exception
43
44 For rare situations when negating the left operand is intended, this rule allows an exception.
45 If the whole negation is explicitly wrapped in parentheses, the rule will not report a problem.
46
47 Examples of **correct** code for this rule:
48
49 ```js
50 /*eslint no-unsafe-negation: "error"*/
51
52 if ((!foo) in object) {
53 // allowed, because the negation is explicitly wrapped in parentheses
54 // it is equivalent to (foo ? "false" : "true") in object
55 // this is allowed as an exception for rare situations when that is the intended meaning
56 }
57
58 if(("" + !foo) in object) {
59 // you can also make the intention more explicit, with type conversion
60 }
61 ```
62
63 Examples of **incorrect** code for this rule:
64
65 ```js
66 /*eslint no-unsafe-negation: "error"*/
67
68 if (!(foo) in object) {
69 // this is not an allowed exception
70 }
71 ```
72
73 ## Options
74
75 This rule has an object option:
76
77 - `"enforceForOrderingRelations": false` (default) allows negation of the left-hand side of ordering relational operators (`<`, `>`, `<=`, `>=`)
78 - `"enforceForOrderingRelations": true` disallows negation of the left-hand side of ordering relational operators
79
80 ### enforceForOrderingRelations
81
82 With this option set to `true` the rule is additionally enforced for:
83
84 - `<` operator.
85 - `>` operator.
86 - `<=` operator.
87 - `>=` operator.
88
89 The purpose is to avoid expressions such as `! a < b` (which is equivalent to `(a ? 0 : 1) < b`) when what is really intended is `!(a < b)`.
90
91 Examples of additional **incorrect** code for this rule with the `{ "enforceForOrderingRelations": true }` option:
92
93 ```js
94 /*eslint no-unsafe-negation: ["error", { "enforceForOrderingRelations": true }]*/
95
96 if (! a < b) {}
97
98 while (! a > b) {}
99
100 foo = ! a <= b;
101
102 foo = ! a >= b;
103 ```
104
105 ## When Not To Use It
106
107 If you don't want to notify unsafe logical negations, then it's safe to disable this rule.