Skip to content

Commit 42f6487

Browse files
bmishfisker
andauthored
explicit-length-check: Ignore known, non-number length properties (#1264)
Co-authored-by: fisker Cheung <[email protected]>
1 parent 69a0fd7 commit 42f6487

File tree

4 files changed

+64
-20
lines changed

4 files changed

+64
-20
lines changed

rules/explicit-length-check.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const {isParenthesized} = require('eslint-utils');
2+
const {isParenthesized, getStaticValue} = require('eslint-utils');
33
const getDocumentationUrl = require('./utils/get-documentation-url');
44
const isLiteralValue = require('./utils/is-literal-value');
55
const isLogicalExpression = require('./utils/is-logical-expression');
@@ -154,6 +154,12 @@ function create(context) {
154154
let node;
155155
let autoFix = true;
156156

157+
const staticValue = getStaticValue(lengthNode, context.getScope());
158+
if (staticValue && (!Number.isInteger(staticValue.value) || staticValue.value < 0)) {
159+
// Ignore known, non-positive-integer length properties.
160+
return;
161+
}
162+
157163
let {isZeroLengthCheck, node: lengthCheckNode} = getLengthCheckNode(lengthNode);
158164
if (lengthCheckNode) {
159165
const {isNegative, node: ancestor} = getBooleanAncestor(lengthCheckNode);

test/explicit-length-check.mjs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ test({
9999

100100
'if (foo.length !== 1) {}',
101101
'if (foo.length > 1) {}',
102-
'if (foo.length < 2) {}'
102+
'if (foo.length < 2) {}',
103+
104+
// With known static length value
105+
'const foo = { size: "small" }; if (foo.size) {}', // Not a number
106+
'const foo = { length: -1 }; if (foo.length) {}', // Array lengths cannot be negative
107+
'const foo = { length: 1.5 }; if (foo.length) {}', // Array lengths must be integers
108+
'const foo = { length: NaN }; if (foo.length) {}', // Array lengths cannot be NaN
109+
'const foo = { length: Infinity }; if (foo.length) {}' // Array lengths cannot be Infinity
103110
],
104111
invalid: [
105112
suggestionCase({
@@ -159,6 +166,11 @@ test.snapshot({
159166
`,
160167
options: [{'non-zero': 'greater-than-or-equal'}]
161168
},
169+
{
170+
// Known, number static value
171+
code: 'const foo = { length: 123 }; if (foo.length) {}',
172+
options: [{'non-zero': 'greater-than-or-equal'}]
173+
},
162174
'if (foo.bar && foo.bar.length) {}',
163175
'if (foo.length || foo.bar()) {}',
164176
'if (!!(!!foo.length)) {}',

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

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,32 @@ Generated by [AVA](https://avajs.dev).
843843
`
844844

845845
## Invalid #4
846+
1 | const foo = { length: 123 }; if (foo.length) {}
847+
848+
> Options
849+
850+
`␊
851+
[␊
852+
{␊
853+
"non-zero": "greater-than-or-equal"␊
854+
}␊
855+
]␊
856+
`
857+
858+
> Output
859+
860+
`␊
861+
1 | const foo = { length: 123 }; if (foo.length >= 1) {}␊
862+
`
863+
864+
> Error 1/1
865+
866+
`␊
867+
> 1 | const foo = { length: 123 }; if (foo.length) {}␊
868+
| ^^^^^^^^^^ Use \`.length >= 1\` when checking length is not zero.␊
869+
`
870+
871+
## Invalid #5
846872
1 | if (foo.bar && foo.bar.length) {}
847873

848874
> Output
@@ -858,7 +884,7 @@ Generated by [AVA](https://avajs.dev).
858884
| ^^^^^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
859885
`
860886

861-
## Invalid #5
887+
## Invalid #6
862888
1 | if (foo.length || foo.bar()) {}
863889

864890
> Output
@@ -874,7 +900,7 @@ Generated by [AVA](https://avajs.dev).
874900
| ^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
875901
`
876902

877-
## Invalid #6
903+
## Invalid #7
878904
1 | if (!!(!!foo.length)) {}
879905

880906
> Output
@@ -890,7 +916,7 @@ Generated by [AVA](https://avajs.dev).
890916
| ^^^^^^^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
891917
`
892918

893-
## Invalid #7
919+
## Invalid #8
894920
1 | if (!(foo.length === 0)) {}
895921

896922
> Output
@@ -906,7 +932,7 @@ Generated by [AVA](https://avajs.dev).
906932
| ^^^^^^^^^^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
907933
`
908934

909-
## Invalid #8
935+
## Invalid #9
910936
1 | while (foo.length >= 1) {}
911937

912938
> Output
@@ -922,7 +948,7 @@ Generated by [AVA](https://avajs.dev).
922948
| ^^^^^^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
923949
`
924950

925-
## Invalid #9
951+
## Invalid #10
926952
1 | do {} while (foo.length);
927953

928954
> Output
@@ -938,7 +964,7 @@ Generated by [AVA](https://avajs.dev).
938964
| ^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
939965
`
940966

941-
## Invalid #10
967+
## Invalid #11
942968
1 | for (let i = 0; (bar && !foo.length); i ++) {}
943969

944970
> Output
@@ -954,7 +980,7 @@ Generated by [AVA](https://avajs.dev).
954980
| ^^^^^^^^^^^ Use \`.length === 0\` when checking length is zero.␊
955981
`
956982

957-
## Invalid #11
983+
## Invalid #12
958984
1 | const isEmpty = foo.length < 1;
959985

960986
> Output
@@ -970,7 +996,7 @@ Generated by [AVA](https://avajs.dev).
970996
| ^^^^^^^^^^^^^^ Use \`.length === 0\` when checking length is zero.␊
971997
`
972998

973-
## Invalid #12
999+
## Invalid #13
9741000
1 | bar(foo.length >= 1)
9751001

9761002
> Output
@@ -986,7 +1012,7 @@ Generated by [AVA](https://avajs.dev).
9861012
| ^^^^^^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
9871013
`
9881014

989-
## Invalid #13
1015+
## Invalid #14
9901016
1 | bar(!foo.length || foo.length)
9911017

9921018
> Output
@@ -1013,7 +1039,7 @@ Generated by [AVA](https://avajs.dev).
10131039
1 | bar(!foo.length || foo.length > 0)␊
10141040
`
10151041

1016-
## Invalid #14
1042+
## Invalid #15
10171043
1 | const bar = void !foo.length;
10181044

10191045
> Output
@@ -1029,7 +1055,7 @@ Generated by [AVA](https://avajs.dev).
10291055
| ^^^^^^^^^^^ Use \`.length === 0\` when checking length is zero.␊
10301056
`
10311057

1032-
## Invalid #15
1058+
## Invalid #16
10331059
1 | const isNotEmpty = Boolean(foo.length)
10341060

10351061
> Output
@@ -1045,7 +1071,7 @@ Generated by [AVA](https://avajs.dev).
10451071
| ^^^^^^^^^^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
10461072
`
10471073

1048-
## Invalid #16
1074+
## Invalid #17
10491075
1 | const isNotEmpty = Boolean(foo.length || bar)
10501076

10511077
> Output
@@ -1061,7 +1087,7 @@ Generated by [AVA](https://avajs.dev).
10611087
| ^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
10621088
`
10631089

1064-
## Invalid #17
1090+
## Invalid #18
10651091
1 | const isEmpty = Boolean(!foo.length)
10661092

10671093
> Output
@@ -1077,7 +1103,7 @@ Generated by [AVA](https://avajs.dev).
10771103
| ^^^^^^^^^^^^^^^^^^^^ Use \`.length === 0\` when checking length is zero.␊
10781104
`
10791105

1080-
## Invalid #18
1106+
## Invalid #19
10811107
1 | const isEmpty = Boolean(foo.length === 0)
10821108

10831109
> Output
@@ -1093,7 +1119,7 @@ Generated by [AVA](https://avajs.dev).
10931119
| ^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`.length === 0\` when checking length is zero.␊
10941120
`
10951121

1096-
## Invalid #19
1122+
## Invalid #20
10971123
1 | const isNotEmpty = !Boolean(foo.length === 0)
10981124

10991125
> Output
@@ -1109,7 +1135,7 @@ Generated by [AVA](https://avajs.dev).
11091135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`.length > 0\` when checking length is not zero.␊
11101136
`
11111137

1112-
## Invalid #20
1138+
## Invalid #21
11131139
1 | const isEmpty = !Boolean(!Boolean(foo.length === 0))
11141140

11151141
> Output
@@ -1125,7 +1151,7 @@ Generated by [AVA](https://avajs.dev).
11251151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`.length === 0\` when checking length is zero.␊
11261152
`
11271153

1128-
## Invalid #21
1154+
## Invalid #22
11291155
1 | if (foo.size) {}
11301156

11311157
> Output
@@ -1141,7 +1167,7 @@ Generated by [AVA](https://avajs.dev).
11411167
| ^^^^^^^^ Use \`.size > 0\` when checking size is not zero.␊
11421168
`
11431169

1144-
## Invalid #22
1170+
## Invalid #23
11451171
1 | if (foo.size && bar.length) {}
11461172

11471173
> Output
69 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)