]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # disallow negated conditions (no-negated-condition) |
2 | ||
3 | Negated conditions are more difficult to understand. Code can be made more readable by inverting the condition instead. | |
4 | ||
5 | ## Rule Details | |
6 | ||
7 | This rule disallows negated conditions in either of the following: | |
8 | ||
9 | * `if` statements which have an `else` branch | |
10 | * ternary expressions | |
11 | ||
12 | Examples of **incorrect** code for this rule: | |
13 | ||
14 | ```js | |
15 | /*eslint no-negated-condition: "error"*/ | |
16 | ||
17 | if (!a) { | |
18 | doSomething(); | |
19 | } else { | |
20 | doSomethingElse(); | |
21 | } | |
22 | ||
23 | if (a != b) { | |
24 | doSomething(); | |
25 | } else { | |
26 | doSomethingElse(); | |
27 | } | |
28 | ||
29 | if (a !== b) { | |
30 | doSomething(); | |
31 | } else { | |
32 | doSomethingElse(); | |
33 | } | |
34 | ||
35 | !a ? c : b | |
36 | ``` | |
37 | ||
38 | Examples of **correct** code for this rule: | |
39 | ||
40 | ```js | |
41 | /*eslint no-negated-condition: "error"*/ | |
42 | ||
43 | if (!a) { | |
44 | doSomething(); | |
45 | } | |
46 | ||
47 | if (!a) { | |
48 | doSomething(); | |
49 | } else if (b) { | |
50 | doSomething(); | |
51 | } | |
52 | ||
53 | if (a != b) { | |
54 | doSomething(); | |
55 | } | |
56 | ||
57 | a ? b : c | |
58 | ``` |