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-expression (#15296)
* feat: Add rule no-constant-binary-expression 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. * Share `no-constant-condition`'s `isConstant` Here we extract `isConstant` into ast-utils and share it between `no-constant-condition` and `no-constant-binary-expression`. * Update title of docs page * Avoid false positive on shadowed builtins * Use isLogicalAssignmentOperator * Ensure we don't treat user defined new expressions as always new * Move docs to the new docs directory * Address review feedback * Address more review feedback * Address review feedback * Fix typo * Update lib/rules/utils/ast-utils.js Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Update docs/src/rules/no-constant-binary-expression.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Update docs/src/rules/no-constant-binary-expression.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Update docs/src/rules/no-constant-binary-expression.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
- Loading branch information
1 parent
db28f2c
commit ab6363d
Showing
8 changed files
with
1,093 additions
and
198 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# no-constant-binary-expression | ||
|
||
Disallows expressions where the operation doesn't affect the value. | ||
|
||
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. | ||
|
||
These errors are especially common in complex expressions where operator precedence is easy to misjudge. For example: | ||
|
||
```js | ||
// One might think this would evaluate as `x + (b ?? c)`: | ||
const x = a + b ?? c; | ||
|
||
// But it actually evaluates as `(a + b) ?? c`. Since `a + b` can never be null, | ||
// the `?? c` has no effect. | ||
``` | ||
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. | ||
```js | ||
// Programmers coming from a language where objects are compared by value might expect this to work: | ||
const isEmpty = x === []; | ||
|
||
// However, this will always result in `isEmpty` being `false`. | ||
``` | ||
## Rule Details | ||
This rule identifies `==` and `===` comparisons which, based on the semantics of the JavaScript language, will always evaluate to `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-expression: "error"*/ | ||
|
||
const value1 = +x == null; | ||
|
||
const value2 = condition ? x : {} || DEFAULT; | ||
|
||
const value3 = !foo == null; | ||
|
||
const value4 = new Boolean(foo) === true; | ||
|
||
const objIsEmpty = someObj === {}; | ||
|
||
const arrIsEmpty = someArr === []; | ||
``` | ||
Examples of **correct** code for this rule: | ||
```js | ||
/*eslint no-constant-binary-expression: "error"*/ | ||
|
||
const value1 = x == null; | ||
|
||
const value2 = (condition ? x : {}) || DEFAULT; | ||
|
||
const value3 = !(foo == null); | ||
|
||
const value4 = Boolean(foo) === true; | ||
|
||
const objIsEmpty = Object.keys(someObj).length === 0; | ||
|
||
const arrIsEmpty = someArr.length === 0; | ||
``` | ||
Related Rules: | ||
* [no-constant-condition](no-constant-condition.md) |
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.