2 title: no-constant-binary-expression
6 - no-constant-condition
8 - https://eslint.org/blog/2022/07/interesting-bugs-caught-by-no-constant-binary-expression/
12 Comparisons which will always evaluate to true or false and logical expressions (`||`, `&&`, `??`) which either always short-circuit or never short-circuit are both likely indications of programmer error.
14 These errors are especially common in complex expressions where operator precedence is easy to misjudge. For example:
17 // One might think this would evaluate as `a + (b ?? c)`:
20 // But it actually evaluates as `(a + b) ?? c`. Since `a + b` can never be null,
21 // the `?? c` has no effect.
24 Additionally, this rule detects comparisons to newly constructed objects/arrays/functions/etc. In JavaScript, where objects are compared by reference, a newly constructed object can _never_ `===` any other value. This can be surprising for programmers coming from languages where objects are compared by value.
27 // Programmers coming from a language where objects are compared by value might expect this to work:
28 const isEmpty = x === [];
30 // However, this will always result in `isEmpty` being `false`.
35 This rule identifies `==` and `===` comparisons which, based on the semantics of the JavaScript language, will always evaluate to `true` or `false`.
37 It also identifies `||`, `&&` and `??` logical expressions which will either always or never short-circuit.
39 Examples of **incorrect** code for this rule:
44 /*eslint no-constant-binary-expression: "error"*/
46 const value1 = +x == null;
48 const value2 = condition ? x : {} || DEFAULT;
50 const value3 = !foo == null;
52 const value4 = new Boolean(foo) === true;
54 const objIsEmpty = someObj === {};
56 const arrIsEmpty = someArr === [];
61 Examples of **correct** code for this rule:
66 /*eslint no-constant-binary-expression: "error"*/
68 const value1 = x == null;
70 const value2 = (condition ? x : {}) || DEFAULT;
72 const value3 = !(foo == null);
74 const value4 = Boolean(foo) === true;
76 const objIsEmpty = Object.keys(someObj).length === 0;
78 const arrIsEmpty = someArr.length === 0;