Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Add rule no-constant-binary-operand
I proposed the core idea of this rule in #13752 as an addition to `no-constant-condition`, but on the advice of the TSC, it was restructured as a standalone rule.
- Loading branch information
1 parent
35f3254
commit 0ec999e
Showing
7 changed files
with
748 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# disallow constant comparisons and expressions that always/never short circuit (no-constant-binary-operand) | ||
|
||
Comparisons which either will always evaluates to true, or always evaluates to false, and logical expressions (`||`, `&&`, `||`) which either always short circuit or never short circuit are both likely indications of programmer error. | ||
|
||
These errors are especially common in complex expressions where operator precedence is easy to misjudge. | ||
|
||
## Rule Details | ||
|
||
This rule identifies `==` and `===` comparisons which, based on the semantics of the JavaScript language, will always evaluate a constant `true` or `false`. | ||
|
||
It also identifies `||`, `&&` and `??` logical expressions which will either always, or never short circuit. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint no-constant-binary-operand: "error"*/ | ||
const value1 = +x == null; | ||
|
||
const value2 = condition ? x : {} || DEFAULT; | ||
|
||
const value3 = !foo == null; | ||
|
||
const value4 = new Boolean(foo) === true | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/*eslint no-constant-binary-operand: "error"*/ | ||
const value1 = x == null; | ||
|
||
const value2 = (condition ? x : {}) || DEFAULT; | ||
|
||
const value3 = !(foo == null); | ||
|
||
const value4 = Boolean(foo) === true | ||
``` | ||
|
||
See Also: | ||
|
||
- [`no-constant-condition`](https://eslint.org/docs/rules/no-constant-condition) which disallows constant values as test conditions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.