Skip to content

Commit

Permalink
explicit-length-check: Check more cases (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 21, 2020
1 parent 650edc9 commit 83a6453
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
18 changes: 9 additions & 9 deletions docs/rules/explicit-length-check.md
@@ -1,6 +1,6 @@
# Enforce explicitly comparing the `length` property of a value

Enforce explicitly checking the length of an array in an `if` condition or ternary and enforce the comparison style.
Enforce explicitly checking the length of an object and enforce the comparison style.

This rule is fixable.

Expand All @@ -15,19 +15,19 @@ if (!foo.length) {}
```

```js
if (foo.length == 0) {}
while (foo.length == 0) {}
```

```js
if (foo.length < 1) {}
do {} while (foo.length < 1);
```

```js
if (0 === foo.length) {}
if (; 0 === foo.length;) {}
```

```js
if (0 == foo.length) {}
const unicorn = 0 == foo.length ? 1 : 2;
```

```js
Expand Down Expand Up @@ -60,19 +60,19 @@ if (foo.length !== 0) {}
```

```js
if (foo.length != 0) {}
while (foo.length != 0) {}
```

```js
if (foo.length >= 1) {}
do {} while (foo.length >= 1);
```

```js
if (0 !== foo.length) {}
for (; 0 !== foo.length; ) {}
```

```js
if (0 != foo.length) {}
const unicorn = 0 != foo.length ? 1 : 2;
```

```js
Expand Down
14 changes: 12 additions & 2 deletions rules/explicit-length-check.js
Expand Up @@ -113,6 +113,16 @@ function getZeroLengthNode(node) {
}
}

const selector = `:matches(${
[
'IfStatement',
'ConditionalExpression',
'WhileStatement',
'DoWhileStatement',
'ForStatement'
].join(', ')
}) > *.test`;

const create = context => {
const options = {
'non-zero': 'greater-than',
Expand Down Expand Up @@ -167,8 +177,8 @@ const create = context => {
}

return {
'IfStatement, ConditionalExpression': node => {
checkExpression(node.test);
[selector](node) {
checkExpression(node);
}
};
};
Expand Down
19 changes: 18 additions & 1 deletion test/explicit-length-check.js
Expand Up @@ -58,6 +58,20 @@ test({

// `ConditionalExpression`
'const bar = foo.length === 0 ? 1 : 2',
// `WhileStatement`
outdent`
while (foo.length > 0) {
foo.pop();
}
`,
// `DoWhileStatement`
outdent`
do {
foo.pop();
} while (foo.length > 0);
`,
// `ForStatement`
'for (; foo.length > 0; foo.pop());',

'if (foo.length !== 1) {}',
'if (foo.length > 1) {}',
Expand Down Expand Up @@ -96,5 +110,8 @@ test.visualize([
'if (foo.bar && foo.bar.length) {}',
'if (foo.length || foo.bar()) {}',
'if (!!(!!foo.length)) {}',
'if (!(foo.length === 0)) {}'
'if (!(foo.length === 0)) {}',
'while (foo.length >= 1) {}',
'do {} while (foo.length);',
'for (let i = 0; (bar && !foo.length); i ++) {}'
]);
48 changes: 48 additions & 0 deletions test/snapshots/explicit-length-check.js.md
Expand Up @@ -779,3 +779,51 @@ Generated by [AVA](https://avajs.dev).
> 1 | if (!(foo.length === 0)) {}␊
| ^^^^^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊
`

## explicit-length-check - #8

> Snapshot 1
`␊
Input:␊
1 | while (foo.length >= 1) {}␊
Output:␊
1 | while (foo.length > 0) {}␊
Error 1/1:␊
> 1 | while (foo.length >= 1) {}␊
| ^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊
`

## explicit-length-check - #9

> Snapshot 1
`␊
Input:␊
1 | do {} while (foo.length);␊
Output:␊
1 | do {} while (foo.length > 0);␊
Error 1/1:␊
> 1 | do {} while (foo.length);␊
| ^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊
`

## explicit-length-check - #10

> Snapshot 1
`␊
Input:␊
1 | for (let i = 0; (bar && !foo.length); i ++) {}␊
Output:␊
1 | for (let i = 0; (bar && foo.length === 0); i ++) {}␊
Error 1/1:␊
> 1 | for (let i = 0; (bar && !foo.length); i ++) {}␊
| ^^^^^^^^^^^ Use `.length === 0` when checking length is zero.␊
`
Binary file modified test/snapshots/explicit-length-check.js.snap
Binary file not shown.

0 comments on commit 83a6453

Please sign in to comment.