diff --git a/docs/rules/explicit-length-check.md b/docs/rules/explicit-length-check.md index 50b4660056..e22a526170 100644 --- a/docs/rules/explicit-length-check.md +++ b/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. @@ -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 @@ -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 diff --git a/rules/explicit-length-check.js b/rules/explicit-length-check.js index 6ad63df194..a9b38c05b4 100644 --- a/rules/explicit-length-check.js +++ b/rules/explicit-length-check.js @@ -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', @@ -167,8 +177,8 @@ const create = context => { } return { - 'IfStatement, ConditionalExpression': node => { - checkExpression(node.test); + [selector](node) { + checkExpression(node); } }; }; diff --git a/test/explicit-length-check.js b/test/explicit-length-check.js index af2b782b03..cc107626ea 100644 --- a/test/explicit-length-check.js +++ b/test/explicit-length-check.js @@ -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) {}', @@ -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 ++) {}' ]); diff --git a/test/snapshots/explicit-length-check.js.md b/test/snapshots/explicit-length-check.js.md index b8cd39d953..160393e24c 100644 --- a/test/snapshots/explicit-length-check.js.md +++ b/test/snapshots/explicit-length-check.js.md @@ -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.␊ + ` diff --git a/test/snapshots/explicit-length-check.js.snap b/test/snapshots/explicit-length-check.js.snap index d0bebe8262..5795d7d3e5 100644 Binary files a/test/snapshots/explicit-length-check.js.snap and b/test/snapshots/explicit-length-check.js.snap differ