Skip to content

Commit 83a6453

Browse files
authored
explicit-length-check: Check more cases (#941)
1 parent 650edc9 commit 83a6453

File tree

5 files changed

+87
-12
lines changed

5 files changed

+87
-12
lines changed

docs/rules/explicit-length-check.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Enforce explicitly comparing the `length` property of a value
22

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

55
This rule is fixable.
66

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

1717
```js
18-
if (foo.length == 0) {}
18+
while (foo.length == 0) {}
1919
```
2020

2121
```js
22-
if (foo.length < 1) {}
22+
do {} while (foo.length < 1);
2323
```
2424

2525
```js
26-
if (0 === foo.length) {}
26+
if (; 0 === foo.length;) {}
2727
```
2828

2929
```js
30-
if (0 == foo.length) {}
30+
const unicorn = 0 == foo.length ? 1 : 2;
3131
```
3232

3333
```js
@@ -60,19 +60,19 @@ if (foo.length !== 0) {}
6060
```
6161

6262
```js
63-
if (foo.length != 0) {}
63+
while (foo.length != 0) {}
6464
```
6565

6666
```js
67-
if (foo.length >= 1) {}
67+
do {} while (foo.length >= 1);
6868
```
6969

7070
```js
71-
if (0 !== foo.length) {}
71+
for (; 0 !== foo.length; ) {}
7272
```
7373

7474
```js
75-
if (0 != foo.length) {}
75+
const unicorn = 0 != foo.length ? 1 : 2;
7676
```
7777

7878
```js

rules/explicit-length-check.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ function getZeroLengthNode(node) {
113113
}
114114
}
115115

116+
const selector = `:matches(${
117+
[
118+
'IfStatement',
119+
'ConditionalExpression',
120+
'WhileStatement',
121+
'DoWhileStatement',
122+
'ForStatement'
123+
].join(', ')
124+
}) > *.test`;
125+
116126
const create = context => {
117127
const options = {
118128
'non-zero': 'greater-than',
@@ -167,8 +177,8 @@ const create = context => {
167177
}
168178

169179
return {
170-
'IfStatement, ConditionalExpression': node => {
171-
checkExpression(node.test);
180+
[selector](node) {
181+
checkExpression(node);
172182
}
173183
};
174184
};

test/explicit-length-check.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ test({
5858

5959
// `ConditionalExpression`
6060
'const bar = foo.length === 0 ? 1 : 2',
61+
// `WhileStatement`
62+
outdent`
63+
while (foo.length > 0) {
64+
foo.pop();
65+
}
66+
`,
67+
// `DoWhileStatement`
68+
outdent`
69+
do {
70+
foo.pop();
71+
} while (foo.length > 0);
72+
`,
73+
// `ForStatement`
74+
'for (; foo.length > 0; foo.pop());',
6175

6276
'if (foo.length !== 1) {}',
6377
'if (foo.length > 1) {}',
@@ -96,5 +110,8 @@ test.visualize([
96110
'if (foo.bar && foo.bar.length) {}',
97111
'if (foo.length || foo.bar()) {}',
98112
'if (!!(!!foo.length)) {}',
99-
'if (!(foo.length === 0)) {}'
113+
'if (!(foo.length === 0)) {}',
114+
'while (foo.length >= 1) {}',
115+
'do {} while (foo.length);',
116+
'for (let i = 0; (bar && !foo.length); i ++) {}'
100117
]);

test/snapshots/explicit-length-check.js.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,51 @@ Generated by [AVA](https://avajs.dev).
779779
> 1 | if (!(foo.length === 0)) {}␊
780780
| ^^^^^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊
781781
`
782+
783+
## explicit-length-check - #8
784+
785+
> Snapshot 1
786+
787+
`␊
788+
Input:␊
789+
1 | while (foo.length >= 1) {}␊
790+
791+
Output:␊
792+
1 | while (foo.length > 0) {}␊
793+
794+
Error 1/1:␊
795+
> 1 | while (foo.length >= 1) {}␊
796+
| ^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊
797+
`
798+
799+
## explicit-length-check - #9
800+
801+
> Snapshot 1
802+
803+
`␊
804+
Input:␊
805+
1 | do {} while (foo.length);␊
806+
807+
Output:␊
808+
1 | do {} while (foo.length > 0);␊
809+
810+
Error 1/1:␊
811+
> 1 | do {} while (foo.length);␊
812+
| ^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊
813+
`
814+
815+
## explicit-length-check - #10
816+
817+
> Snapshot 1
818+
819+
`␊
820+
Input:␊
821+
1 | for (let i = 0; (bar && !foo.length); i ++) {}␊
822+
823+
Output:␊
824+
1 | for (let i = 0; (bar && foo.length === 0); i ++) {}␊
825+
826+
Error 1/1:␊
827+
> 1 | for (let i = 0; (bar && !foo.length); i ++) {}␊
828+
| ^^^^^^^^^^^ Use `.length === 0` when checking length is zero.␊
829+
`
190 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)