Skip to content

Commit

Permalink
feat: Add rule no-constant-binary-operand
Browse files Browse the repository at this point in the history
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
captbaritone committed Nov 24, 2021
1 parent 1e32ee5 commit b7961dd
Show file tree
Hide file tree
Showing 6 changed files with 755 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/rules/no-constant-binary-operand.md
@@ -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.
4 changes: 4 additions & 0 deletions docs/rules/no-constant-condition.md
Expand Up @@ -75,6 +75,10 @@ do {
var result = x !== 0 ? a : b;
```

See Also:

* [`no-constant-binary-operand`](https://eslint.org/docs/rules/no-constant-binary-operand) which disallows constant comparisons and logical expressions (`||`, `&&`, `??`) which always or never short circuit.

## Options

### checkLoops
Expand Down
1 change: 1 addition & 0 deletions lib/rules/index.js
Expand Up @@ -103,6 +103,7 @@ module.exports = new LazyLoadingRuleMap(Object.entries({
"no-confusing-arrow": () => require("./no-confusing-arrow"),
"no-console": () => require("./no-console"),
"no-const-assign": () => require("./no-const-assign"),
"no-constant-binary-operand": () => require("./no-constant-binary-operand"),
"no-constant-condition": () => require("./no-constant-condition"),
"no-constructor-return": () => require("./no-constructor-return"),
"no-continue": () => require("./no-continue"),
Expand Down

0 comments on commit b7961dd

Please sign in to comment.