Skip to content

Commit

Permalink
Rewrite explicit-length-check rule (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 20, 2020
1 parent 16275b8 commit 94ae87a
Show file tree
Hide file tree
Showing 6 changed files with 1,026 additions and 371 deletions.
102 changes: 82 additions & 20 deletions docs/rules/explicit-length-check.md
@@ -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`
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -111,7 +111,7 @@ Configure it in `package.json`.
- [error-message](docs/rules/error-message.md) - Enforce passing a `message` value when creating a built-in error.
- [escape-case](docs/rules/escape-case.md) - Require escape sequences to use uppercase values. *(fixable)*
- [expiring-todo-comments](docs/rules/expiring-todo-comments.md) - Add expiration conditions to TODO comments.
- [explicit-length-check](docs/rules/explicit-length-check.md) - Enforce explicitly comparing the `length` property of a value. *(partly fixable)*
- [explicit-length-check](docs/rules/explicit-length-check.md) - Enforce explicitly comparing the `length` property of a value. *(fixable)*
- [filename-case](docs/rules/filename-case.md) - Enforce a case style for filenames.
- [import-index](docs/rules/import-index.md) - Enforce importing index files with `.`. *(fixable)*
- [import-style](docs/rules/import-style.md) - Enforce specific import styles per module.
Expand Down

0 comments on commit 94ae87a

Please sign in to comment.