Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite
explicit-length-check
rule (#938)
- Loading branch information
Showing
6 changed files
with
1,026 additions
and
371 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 |
---|---|---|
@@ -1,61 +1,123 @@ | ||
# Enforce explicitly comparing the `length` property of a value | ||
|
||
Enforce explicitly checking the length of a value array in an `if` condition, rather than checking the truthiness of the length, and enforce comparison style. | ||
Enforce explicitly checking the length of an array in an `if` condition or ternary and enforce the comparison style. | ||
|
||
This rule is partly fixable. | ||
This rule is fixable. | ||
|
||
## Zero comparisons | ||
|
||
Enforce comparison with `=== 0` when checking for zero length. | ||
|
||
### Fail | ||
|
||
```js | ||
if (string.length) {} | ||
if (array.length) {} | ||
if (!array.length) {} | ||
if (array.length !== 0) {} | ||
if (!foo.length) {} | ||
``` | ||
|
||
```js | ||
if (foo.length == 0) {} | ||
``` | ||
|
||
```js | ||
if (foo.length < 1) {} | ||
``` | ||
|
||
```js | ||
if (0 === foo.length) {} | ||
``` | ||
|
||
```js | ||
if (0 == foo.length) {} | ||
``` | ||
|
||
```js | ||
if (1 > foo.length) {} | ||
``` | ||
|
||
```js | ||
// Negative style is forbid too | ||
if (!(foo.length > 0)) {} | ||
``` | ||
|
||
### Pass | ||
|
||
```js | ||
if (string.length > 0) {} | ||
if (array.length > 0) {} | ||
if (array.length === 0) {} | ||
if (foo.length === 0) {} | ||
``` | ||
|
||
```js | ||
const unicorn = foo.length === 0 ? 1 : 2; | ||
``` | ||
|
||
## Zero comparisons | ||
## Non-zero comparisons | ||
|
||
Enforce comparison with `=== 0` when checking for zero length. | ||
Enforce comparison with `> 0` when checking for non-zero length. | ||
|
||
### Fail | ||
|
||
```js | ||
if (string.length < 1) {} | ||
if (foo.length !== 0) {} | ||
``` | ||
|
||
```js | ||
if (foo.length != 0) {} | ||
``` | ||
|
||
```js | ||
if (foo.length >= 1) {} | ||
``` | ||
|
||
```js | ||
if (0 !== foo.length) {} | ||
``` | ||
|
||
```js | ||
if (0 != foo.length) {} | ||
``` | ||
|
||
```js | ||
if (0 < foo.length) {} | ||
``` | ||
|
||
```js | ||
if (1 <= foo.length) {} | ||
``` | ||
|
||
```js | ||
// Negative style is forbid too | ||
if (!(foo.length === 0)) {} | ||
``` | ||
|
||
### Pass | ||
|
||
```js | ||
if (array.length === 0) {} | ||
if (foo.length > 0) {} | ||
``` | ||
|
||
```js | ||
const unicorn = foo.length > 0 ? 1 : 2; | ||
``` | ||
|
||
## Non-zero comparisons | ||
### Options | ||
|
||
You can define your preferred way of checking non-zero length by providing a `non-zero` option (`greater-than` by default): | ||
|
||
```js | ||
{ | ||
'unicorn/explicit-length-check': ['error', { | ||
'non-zero': 'not-equal' | ||
}] | ||
'unicorn/explicit-length-check': [ | ||
'error', | ||
{ | ||
'non-zero': 'not-equal' | ||
} | ||
] | ||
} | ||
``` | ||
|
||
The `non-zero` option can be configured with one of the following: | ||
|
||
- `greater-than` (default) | ||
- Enforces non-zero to be checked with: `array.length > 0` | ||
- Enforces non-zero to be checked with: `foo.length > 0` | ||
- `not-equal` | ||
- Enforces non-zero to be checked with: `array.length !== 0` | ||
- Enforces non-zero to be checked with: `foo.length !== 0` | ||
- `greater-than-or-equal` | ||
- Enforces non-zero to be checked with: `array.length >= 1` | ||
- Enforces non-zero to be checked with: `foo.length >= 1` |
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.