diff --git a/docs/rules/explicit-length-check.md b/docs/rules/explicit-length-check.md index aa220a34b2..50b4660056 100644 --- a/docs/rules/explicit-length-check.md +++ b/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` diff --git a/readme.md b/readme.md index 74427c84a2..5fcd7957d5 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/rules/explicit-length-check.js b/rules/explicit-length-check.js index ec10863790..6ad63df194 100644 --- a/rules/explicit-length-check.js +++ b/rules/explicit-length-check.js @@ -2,157 +2,173 @@ const getDocumentationUrl = require('./utils/get-documentation-url'); const messages = { - compareToValue: '`length` property should be compared to a value.', - zeroEqual: 'Zero `.length` should be compared with `=== 0`.', - nonZeroEqual: 'Non-zero `.length` should be compared with `!== 0`.', - nonZeroGreater: 'Non-zero `.length` should be compared with `> 0`.', - nonZeroGreaterEqual: 'Non-zero `.length` should be compared with `>= 1`.' + 'non-zero': 'Use `.length {{code}}` when checking length is not zero.', + zero: 'Use `.length {{code}}` when checking length is zero.' }; -const operatorTypes = { - gt: ['>'], - gte: ['>='], - ne: ['!==', '!='] +const isLengthProperty = node => + node.type === 'MemberExpression' && + node.computed === false && + node.property.type === 'Identifier' && + node.property.name === 'length'; +const isLogicNot = node => + node.type === 'UnaryExpression' && + node.operator === '!'; +const isLiteralNumber = (node, value) => + node.type === 'Literal' && + typeof node.value === 'number' && + node.value === value; +const isCompareRight = (node, operator, value) => + node.type === 'BinaryExpression' && + node.operator === operator && + isLengthProperty(node.left) && + isLiteralNumber(node.right, value); +const isCompareLeft = (node, operator, value) => + node.type === 'BinaryExpression' && + node.operator === operator && + isLengthProperty(node.right) && + isLiteralNumber(node.left, value); +const nonZeroStyles = new Map([ + [ + 'greater-than', + { + code: '> 0', + test: node => isCompareRight(node, '>', 0) + } + ], + [ + 'not-equal', + { + code: '!== 0', + test: node => isCompareRight(node, '!==', 0) + } + ], + [ + 'greater-than-or-equal', + { + code: '>= 1', + test: node => isCompareRight(node, '>=', 1) + } + ] +]); +const zeroStyle = { + code: '=== 0', + test: node => isCompareRight(node, '===', 0) }; -function reportError(context, node, messageId, fixDetails) { - context.report({ - node, - messageId, - fix: fixDetails && (fixer => { - return fixer.replaceText( - node, - `${context.getSourceCode().getText(fixDetails.node)} ${fixDetails.operator} ${fixDetails.value}` - ); - }) - }); -} - -function checkZeroType(context, node) { - if (node.operator === '<' && node.right.value === 1) { - reportError( - context, - node, - 'zeroEqual', - { - node: node.left, - operator: '===', - value: 0 - } - ); +function getNonZeroLengthNode(node) { + // `foo.length` + if (isLengthProperty(node)) { + return node; } -} - -function checkNonZeroType(context, node, type = 'greater-than') { - const {value} = node.right; - const {operator} = node; - - switch (type) { - case 'greater-than': - if ( - (operatorTypes.gte.includes(operator) && value === 1) || - (operatorTypes.ne.includes(operator) && value === 0) - ) { - reportError( - context, - node, - 'nonZeroGreater', - { - node: node.left, - operator: '>', - value: 0 - } - ); - } - break; - case 'greater-than-or-equal': - if ( - (operatorTypes.gt.includes(operator) && value === 0) || - (operatorTypes.ne.includes(operator) && value === 0) - ) { - reportError( - context, - node, - 'nonZeroGreaterEqual', - { - node: node.left, - operator: '>=', - value: 1 - } - ); - } - - break; - case 'not-equal': - if ( - (operatorTypes.gt.includes(operator) && value === 0) || - (operatorTypes.gte.includes(operator) && value === 1) - ) { - reportError( - context, - node, - 'nonZeroEqual', - { - node: node.left, - operator: '!==', - value: 0 - } - ); - } - - break; - default: - break; + if ( + // `foo.length !== 0` + isCompareRight(node, '!==', 0) || + // `foo.length != 0` + isCompareRight(node, '!=', 0) || + // `foo.length > 0` + isCompareRight(node, '>', 0) || + // `foo.length >= 1` + isCompareRight(node, '>=', 1) + ) { + return node.left; } -} -function checkBinaryExpression(context, node, options) { if ( - node.right.type === 'Literal' && - node.left.type === 'MemberExpression' && - node.left.property.type === 'Identifier' && - node.left.property.name === 'length' + // `0 !== foo.length` + isCompareLeft(node, '!==', 0) || + // `0 !== foo.length` + isCompareLeft(node, '!=', 0) || + // `0 < foo.length` + isCompareLeft(node, '<', 0) || + // `1 <= foo.length` + isCompareLeft(node, '<=', 1) ) { - checkZeroType(context, node); - checkNonZeroType(context, node, options['non-zero']); + return node.right; } } -function checkExpression(context, node) { - if (node.type === 'LogicalExpression') { - checkExpression(context, node.left); - checkExpression(context, node.right); - return; - } - - if (node.type === 'UnaryExpression' && node.operator === '!') { - checkExpression(context, node.argument); - return; - } - - if (node.type === 'BinaryExpression') { - checkBinaryExpression(context, node, context.options[0] || {}); - return; +function getZeroLengthNode(node) { + if ( + // `foo.length === 0` + isCompareRight(node, '===', 0) || + // `foo.length == 0` + isCompareRight(node, '==', 0) || + // `foo.length < 1` + isCompareRight(node, '<', 1) + ) { + return node.left; } if ( - node.type === 'MemberExpression' && - node.property.type === 'Identifier' && - node.property.name === 'length' && - !node.computed + // `0 === foo.length` + isCompareLeft(node, '===', 0) || + // `0 == foo.length` + isCompareLeft(node, '==', 0) || + // `1 > foo.length` + isCompareLeft(node, '>', 1) ) { - reportError(context, node, 'compareToValue'); + return node.right; } } const create = context => { + const options = { + 'non-zero': 'greater-than', + ...context.options[0] + }; + const nonZeroStyle = nonZeroStyles.get(options['non-zero']); + const sourceCode = context.getSourceCode(); + + function checkExpression(node) { + // Is matched style + if (nonZeroStyle.test(node) || zeroStyle.test(node)) { + return; + } + + let isNegative = false; + let expression = node; + while (isLogicNot(expression)) { + isNegative = !isNegative; + expression = expression.argument; + } + + if (expression.type === 'LogicalExpression') { + checkExpression(expression.left); + checkExpression(expression.right); + return; + } + + let lengthNode; + let isCheckingZero = isNegative; + + const zeroLengthNode = getZeroLengthNode(expression); + if (zeroLengthNode) { + lengthNode = zeroLengthNode; + isCheckingZero = !isCheckingZero; + } else { + const nonZeroLengthNode = getNonZeroLengthNode(expression); + if (nonZeroLengthNode) { + lengthNode = nonZeroLengthNode; + } else { + return; + } + } + + const {code} = isCheckingZero ? zeroStyle : nonZeroStyle; + const messageId = isCheckingZero ? 'zero' : 'non-zero'; + context.report({ + node, + messageId, + data: {code}, + fix: fixer => fixer.replaceText(node, `${sourceCode.getText(lengthNode)} ${code}`) + }); + } + return { - IfStatement: node => { - checkExpression(context, node.test); - }, - ConditionalExpression: node => { - checkExpression(context, node.test); + 'IfStatement, ConditionalExpression': node => { + checkExpression(node.test); } }; }; @@ -162,7 +178,7 @@ const schema = [ type: 'object', properties: { 'non-zero': { - enum: ['not-equal', 'greater-than', 'greater-than-or-equal'], + enum: [...nonZeroStyles.keys()], default: 'greater-than' } } diff --git a/test/explicit-length-check.js b/test/explicit-length-check.js index 72b7200303..af2b782b03 100644 --- a/test/explicit-length-check.js +++ b/test/explicit-length-check.js @@ -1,198 +1,100 @@ +import {outdent} from 'outdent'; import {test} from './utils/test'; -function testCase(code, nonZeroType, messageIds, output) { - return { - code, - output: output || code, - errors: (messageIds || []).map(messageId => ({messageId})), - options: nonZeroType ? [{ - 'non-zero': nonZeroType - }] : [] - }; -} +const nonZeroCases = [ + 'foo.length', + '!!foo.length', + 'foo.length !== 0', + 'foo.length != 0', + 'foo.length > 0', + 'foo.length >= 1', + '0 !== foo.length', + '0 != foo.length', + '0 < foo.length', + '1 <= foo.length' +]; + +const zeroCases = [ + '!foo.length', + 'foo.length === 0', + 'foo.length == 0', + 'foo.length < 1', + '0 === foo.length', + '0 == foo.length', + '1 > foo.length' +]; test({ valid: [ - testCase('array.foo'), - testCase('array.length'), - testCase('array.length === 0'), - testCase('array.length !== 0'), - testCase('array.length > 0'), - testCase('if (array.foo) {}'), - testCase('if (length) {}'), - testCase('if ([].length > 0) {}'), - testCase('if ("".length > 0) {}'), - testCase('if (array.length === 0) {}'), - testCase('if (array.length == 0) {}'), - testCase('if (array.length === 1) {}'), - testCase('if (array.length <= 1) {}'), - testCase('if (array.length > 1) {}'), - testCase('if (array.length < 2) {}'), - testCase('const foo = [].length === 0 ? null : undefined'), - testCase('array.length', 'not-equal'), - testCase('array.length > 0', 'not-equal'), - testCase('array.length >= 1', 'not-equal'), - testCase('if ("".length !== 0) {}', 'not-equal'), - testCase('if ([].length === 0) {}', 'not-equal'), - testCase('if ([].length === 1) {}', 'not-equal'), - testCase('if ([].length <= 1) {}', 'not-equal'), - testCase('if ("".length == 0) {}', 'not-equal'), - testCase('array.length !== 0', 'greater-than'), - testCase('array.length >= 1', 'greater-than'), - testCase('if ("".length > 0) {}', 'greater-than'), - testCase('if ("".length >= 0) {}', 'greater-than'), - testCase('if ("".length >= 2) {}', 'greater-than'), - testCase('if ("".length >= 1) {}', 'greater-than-or-equal'), - testCase('array.length !== 0', 'greater-than-or-equal'), - testCase('array.length > 0', 'greater-than-or-equal'), - testCase('if ("".length === 0) {}', 'greater-than-or-equal'), - testCase('if ("".length > 2) {}', 'greater-than-or-equal'), - testCase('if ("".length === 2) {}', 'greater-than-or-equal'), - testCase('if ("".length === 0 && array.length >= 1) {}', 'greater-than-or-equal'), - testCase('if ("".length === 0 && array.length > 0) {}', 'greater-than'), - testCase('if ("".length === 0 && array.length !== 0) {}', 'not-equal'), - testCase('if (foo[length]) {}') + // Not `.length` + 'if (foo.notLength) {}', + 'if (length) {}', + 'if (foo[length]) {}', + 'if (foo["length"]) {}', + + // Not in `IfStatement` or `ConditionalExpression` + 'foo.length', + 'foo.length === 0', + 'foo.length !== 0', + 'foo.length > 0', + + // Checking 'non-zero' + 'if (foo.length > 0) {}', + { + code: 'if (foo.length > 0) {}', + options: [{'non-zero': 'greater-than'}] + }, + { + code: 'if (foo.length !== 0) {}', + options: [{'non-zero': 'not-equal'}] + }, + { + code: 'if (foo.length >= 1) {}', + options: [{'non-zero': 'greater-than-or-equal'}] + }, + + // Checking 'non-zero' + 'if (foo.length === 0) {}', + + // `ConditionalExpression` + 'const bar = foo.length === 0 ? 1 : 2', + + 'if (foo.length !== 1) {}', + 'if (foo.length > 1) {}', + 'if (foo.length < 2) {}' ], - invalid: [ - testCase( - 'if ([].length) {}', - undefined, - ['compareToValue'] - ), - testCase( - 'if ("".length) {}', - undefined, - ['compareToValue'] - ), - testCase( - 'if (array.length) {}', - undefined, - ['compareToValue'] - ), - testCase( - 'if (!array.length) {}', - undefined, - ['compareToValue'] - ), - testCase( - 'if (array.foo.length) {}', - undefined, - ['compareToValue'] - ), - testCase( - 'if (!!array.length) {}', - undefined, - ['compareToValue'] - ), - testCase( - 'if (array.length && array[0] === 1) {}', - undefined, - ['compareToValue'] - ), - testCase( - 'if (array[0] === 1 || array.length) {}', - undefined, - ['compareToValue'] - ), - testCase( - 'if (array.length < 1) {}', - undefined, - ['zeroEqual'], - 'if (array.length === 0) {}' - ), - testCase( - 'if (array.length<1) {}', - undefined, - ['zeroEqual'], - 'if (array.length === 0) {}' - ), - testCase( - 'if (array.length !== 0) {}', - undefined, - ['nonZeroGreater'], - 'if (array.length > 0) {}' - ), - testCase( - 'if (array.length !== 0 && array[0] === 1) {}', - undefined, - ['nonZeroGreater'], - 'if (array.length > 0 && array[0] === 1) {}' - ), - testCase( - 'if (array.length > 0) {}', - 'not-equal', - ['nonZeroEqual'], - 'if (array.length !== 0) {}' - ), - testCase( - 'if (array.length >= 1) {}', - 'not-equal', - ['nonZeroEqual'], - 'if (array.length !== 0) {}' - ), - testCase( - 'if (array.length != 0) {}', - 'greater-than', - ['nonZeroGreater'], - 'if (array.length > 0) {}' - ), - testCase( - 'if (array.length !== 0) {}', - 'greater-than', - ['nonZeroGreater'], - 'if (array.length > 0) {}' - ), - testCase( - 'if (array.length >= 1) {}', - 'greater-than', - ['nonZeroGreater'], - 'if (array.length > 0) {}' - ), - testCase( - 'if (array.length != 0) {}', - 'greater-than-or-equal', - ['nonZeroGreaterEqual'], - 'if (array.length >= 1) {}' - ), - testCase( - 'if (array.length !== 0) {}', - 'greater-than-or-equal', - ['nonZeroGreaterEqual'], - 'if (array.length >= 1) {}' - ), - testCase( - 'if (array.length > 0) {}', - 'greater-than-or-equal', - ['nonZeroGreaterEqual'], - 'if (array.length >= 1) {}' - ), - testCase( - 'if (array.length < 1 || array.length >= 1) {}', - 'not-equal', - ['zeroEqual', 'nonZeroEqual'], - 'if (array.length === 0 || array.length !== 0) {}' - ), - testCase( - 'const foo = [].length ? null : undefined', - undefined, - ['compareToValue'] - ) - ] + invalid: [] }); test.visualize([ - 'if ([].length) {}', - 'if (array.length < 1) {}', + outdent` + if ( + !!!( + ${zeroCases.filter(code => code !== 'foo.length === 0').join(' &&\n\t\t')} + ) || + !( + ${nonZeroCases.filter(code => code !== 'foo.length > 0').join(' ||\n\t\t')} + ) + ) {} + `, { - code: 'if (array.length > 0) {}', + code: outdent` + if ( + ${nonZeroCases.filter(code => code !== 'foo.length !== 0').join(' ||\n\t')} + ) {} + `, options: [{'non-zero': 'not-equal'}] }, { - code: 'if (array.length != 0) {}', - options: [{'non-zero': 'greater-than'}] - }, - { - code: 'if (array.length != 0) {}', + code: outdent` + const foo = ( + ${nonZeroCases.filter(code => code !== 'foo.length >= 1').join(' &&\n\t')} + ) ? 1 : 2; + `, options: [{'non-zero': 'greater-than-or-equal'}] - } + }, + 'if (foo.bar && foo.bar.length) {}', + 'if (foo.length || foo.bar()) {}', + 'if (!!(!!foo.length)) {}', + 'if (!(foo.length === 0)) {}' ]); diff --git a/test/snapshots/explicit-length-check.js.md b/test/snapshots/explicit-length-check.js.md index edf4adeaa4..b8cd39d953 100644 --- a/test/snapshots/explicit-length-check.js.md +++ b/test/snapshots/explicit-length-check.js.md @@ -10,14 +10,396 @@ Generated by [AVA](https://avajs.dev). `␊ Input:␊ - 1 | if ([].length) {}␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ ␊ Output:␊ - [Same as input]␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | foo.length === 0 &&␊ + 4 | foo.length === 0 &&␊ + 5 | foo.length === 0 &&␊ + 6 | foo.length === 0 &&␊ + 7 | foo.length === 0 &&␊ + 8 | foo.length === 0␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length > 0 ||␊ + 12 | foo.length > 0 ||␊ + 13 | foo.length > 0 ||␊ + 14 | foo.length > 0 ||␊ + 15 | foo.length > 0 ||␊ + 16 | foo.length > 0 ||␊ + 17 | foo.length > 0 ||␊ + 18 | foo.length > 0 ||␊ + 19 | foo.length > 0␊ + 20 | )␊ + 21 | ) {}␊ ␊ - Error 1/1:␊ - > 1 | if ([].length) {}␊ - | ^^^^^^^^^ `length` property should be compared to a value.␊ + Error 1/15:␊ + 1 | if (␊ + 2 | !!!(␊ + > 3 | !foo.length &&␊ + | ^^^^^^^^^^^ Use `.length === 0` when checking length is zero.␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 2/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + > 4 | foo.length == 0 &&␊ + | ^^^^^^^^^^^^^^^ Use `.length === 0` when checking length is zero.␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 3/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + > 5 | foo.length < 1 &&␊ + | ^^^^^^^^^^^^^^ Use `.length === 0` when checking length is zero.␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 4/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + > 6 | 0 === foo.length &&␊ + | ^^^^^^^^^^^^^^^^ Use `.length === 0` when checking length is zero.␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 5/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + > 7 | 0 == foo.length &&␊ + | ^^^^^^^^^^^^^^^ Use `.length === 0` when checking length is zero.␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 6/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + > 8 | 1 > foo.length␊ + | ^^^^^^^^^^^^^^ Use `.length === 0` when checking length is zero.␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 7/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + > 11 | foo.length ||␊ + | ^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 8/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + > 12 | !!foo.length ||␊ + | ^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 9/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + > 13 | foo.length !== 0 ||␊ + | ^^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 10/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + > 14 | foo.length != 0 ||␊ + | ^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 11/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + > 15 | foo.length >= 1 ||␊ + | ^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 12/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + > 16 | 0 !== foo.length ||␊ + | ^^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 13/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + > 17 | 0 != foo.length ||␊ + | ^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 18 | 0 < foo.length ||␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 14/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + > 18 | 0 < foo.length ||␊ + | ^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 19 | 1 <= foo.length␊ + 20 | )␊ + 21 | ) {}␊ + Error 15/15:␊ + 1 | if (␊ + 2 | !!!(␊ + 3 | !foo.length &&␊ + 4 | foo.length == 0 &&␊ + 5 | foo.length < 1 &&␊ + 6 | 0 === foo.length &&␊ + 7 | 0 == foo.length &&␊ + 8 | 1 > foo.length␊ + 9 | ) ||␊ + 10 | !(␊ + 11 | foo.length ||␊ + 12 | !!foo.length ||␊ + 13 | foo.length !== 0 ||␊ + 14 | foo.length != 0 ||␊ + 15 | foo.length >= 1 ||␊ + 16 | 0 !== foo.length ||␊ + 17 | 0 != foo.length ||␊ + 18 | 0 < foo.length ||␊ + > 19 | 1 <= foo.length␊ + | ^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + 20 | )␊ + 21 | ) {}␊ ` ## explicit-length-check - #2 @@ -25,15 +407,156 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `␊ + Options:␊ + [␊ + {␊ + "non-zero": "not-equal"␊ + }␊ + ]␊ + ␊ Input:␊ - 1 | if (array.length < 1) {}␊ + 1 | if (␊ + 2 | foo.length ||␊ + 3 | !!foo.length ||␊ + 4 | foo.length != 0 ||␊ + 5 | foo.length > 0 ||␊ + 6 | foo.length >= 1 ||␊ + 7 | 0 !== foo.length ||␊ + 8 | 0 != foo.length ||␊ + 9 | 0 < foo.length ||␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ ␊ Output:␊ - 1 | if (array.length === 0) {}␊ + 1 | if (␊ + 2 | foo.length !== 0 ||␊ + 3 | foo.length !== 0 ||␊ + 4 | foo.length !== 0 ||␊ + 5 | foo.length !== 0 ||␊ + 6 | foo.length !== 0 ||␊ + 7 | foo.length !== 0 ||␊ + 8 | foo.length !== 0 ||␊ + 9 | foo.length !== 0 ||␊ + 10 | foo.length !== 0␊ + 11 | ) {}␊ ␊ - Error 1/1:␊ - > 1 | if (array.length < 1) {}␊ - | ^^^^^^^^^^^^^^^^ Zero `.length` should be compared with `=== 0`.␊ + Error 1/9:␊ + 1 | if (␊ + > 2 | foo.length ||␊ + | ^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 3 | !!foo.length ||␊ + 4 | foo.length != 0 ||␊ + 5 | foo.length > 0 ||␊ + 6 | foo.length >= 1 ||␊ + 7 | 0 !== foo.length ||␊ + 8 | 0 != foo.length ||␊ + 9 | 0 < foo.length ||␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ + Error 2/9:␊ + 1 | if (␊ + 2 | foo.length ||␊ + > 3 | !!foo.length ||␊ + | ^^^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 4 | foo.length != 0 ||␊ + 5 | foo.length > 0 ||␊ + 6 | foo.length >= 1 ||␊ + 7 | 0 !== foo.length ||␊ + 8 | 0 != foo.length ||␊ + 9 | 0 < foo.length ||␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ + Error 3/9:␊ + 1 | if (␊ + 2 | foo.length ||␊ + 3 | !!foo.length ||␊ + > 4 | foo.length != 0 ||␊ + | ^^^^^^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 5 | foo.length > 0 ||␊ + 6 | foo.length >= 1 ||␊ + 7 | 0 !== foo.length ||␊ + 8 | 0 != foo.length ||␊ + 9 | 0 < foo.length ||␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ + Error 4/9:␊ + 1 | if (␊ + 2 | foo.length ||␊ + 3 | !!foo.length ||␊ + 4 | foo.length != 0 ||␊ + > 5 | foo.length > 0 ||␊ + | ^^^^^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 6 | foo.length >= 1 ||␊ + 7 | 0 !== foo.length ||␊ + 8 | 0 != foo.length ||␊ + 9 | 0 < foo.length ||␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ + Error 5/9:␊ + 1 | if (␊ + 2 | foo.length ||␊ + 3 | !!foo.length ||␊ + 4 | foo.length != 0 ||␊ + 5 | foo.length > 0 ||␊ + > 6 | foo.length >= 1 ||␊ + | ^^^^^^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 7 | 0 !== foo.length ||␊ + 8 | 0 != foo.length ||␊ + 9 | 0 < foo.length ||␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ + Error 6/9:␊ + 1 | if (␊ + 2 | foo.length ||␊ + 3 | !!foo.length ||␊ + 4 | foo.length != 0 ||␊ + 5 | foo.length > 0 ||␊ + 6 | foo.length >= 1 ||␊ + > 7 | 0 !== foo.length ||␊ + | ^^^^^^^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 8 | 0 != foo.length ||␊ + 9 | 0 < foo.length ||␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ + Error 7/9:␊ + 1 | if (␊ + 2 | foo.length ||␊ + 3 | !!foo.length ||␊ + 4 | foo.length != 0 ||␊ + 5 | foo.length > 0 ||␊ + 6 | foo.length >= 1 ||␊ + 7 | 0 !== foo.length ||␊ + > 8 | 0 != foo.length ||␊ + | ^^^^^^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 9 | 0 < foo.length ||␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ + Error 8/9:␊ + 1 | if (␊ + 2 | foo.length ||␊ + 3 | !!foo.length ||␊ + 4 | foo.length != 0 ||␊ + 5 | foo.length > 0 ||␊ + 6 | foo.length >= 1 ||␊ + 7 | 0 !== foo.length ||␊ + 8 | 0 != foo.length ||␊ + > 9 | 0 < foo.length ||␊ + | ^^^^^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 10 | 1 <= foo.length␊ + 11 | ) {}␊ + Error 9/9:␊ + 1 | if (␊ + 2 | foo.length ||␊ + 3 | !!foo.length ||␊ + 4 | foo.length != 0 ||␊ + 5 | foo.length > 0 ||␊ + 6 | foo.length >= 1 ||␊ + 7 | 0 !== foo.length ||␊ + 8 | 0 != foo.length ||␊ + 9 | 0 < foo.length ||␊ + > 10 | 1 <= foo.length␊ + | ^^^^^^^^^^^^^^^ Use `.length !== 0` when checking length is not zero.␊ + 11 | ) {}␊ ` ## explicit-length-check - #3 @@ -44,19 +567,153 @@ Generated by [AVA](https://avajs.dev). Options:␊ [␊ {␊ - "non-zero": "not-equal"␊ + "non-zero": "greater-than-or-equal"␊ }␊ ]␊ ␊ Input:␊ - 1 | if (array.length > 0) {}␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + 3 | !!foo.length &&␊ + 4 | foo.length !== 0 &&␊ + 5 | foo.length != 0 &&␊ + 6 | foo.length > 0 &&␊ + 7 | 0 !== foo.length &&␊ + 8 | 0 != foo.length &&␊ + 9 | 0 < foo.length &&␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ ␊ Output:␊ - 1 | if (array.length !== 0) {}␊ + 1 | const foo = (␊ + 2 | foo.length >= 1 &&␊ + 3 | foo.length >= 1 &&␊ + 4 | foo.length >= 1 &&␊ + 5 | foo.length >= 1 &&␊ + 6 | foo.length >= 1 &&␊ + 7 | foo.length >= 1 &&␊ + 8 | foo.length >= 1 &&␊ + 9 | foo.length >= 1 &&␊ + 10 | foo.length >= 1␊ + 11 | ) ? 1 : 2;␊ ␊ - Error 1/1:␊ - > 1 | if (array.length > 0) {}␊ - | ^^^^^^^^^^^^^^^^ Non-zero `.length` should be compared with `!== 0`.␊ + Error 1/9:␊ + 1 | const foo = (␊ + > 2 | foo.length &&␊ + | ^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 3 | !!foo.length &&␊ + 4 | foo.length !== 0 &&␊ + 5 | foo.length != 0 &&␊ + 6 | foo.length > 0 &&␊ + 7 | 0 !== foo.length &&␊ + 8 | 0 != foo.length &&␊ + 9 | 0 < foo.length &&␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ + Error 2/9:␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + > 3 | !!foo.length &&␊ + | ^^^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 4 | foo.length !== 0 &&␊ + 5 | foo.length != 0 &&␊ + 6 | foo.length > 0 &&␊ + 7 | 0 !== foo.length &&␊ + 8 | 0 != foo.length &&␊ + 9 | 0 < foo.length &&␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ + Error 3/9:␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + 3 | !!foo.length &&␊ + > 4 | foo.length !== 0 &&␊ + | ^^^^^^^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 5 | foo.length != 0 &&␊ + 6 | foo.length > 0 &&␊ + 7 | 0 !== foo.length &&␊ + 8 | 0 != foo.length &&␊ + 9 | 0 < foo.length &&␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ + Error 4/9:␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + 3 | !!foo.length &&␊ + 4 | foo.length !== 0 &&␊ + > 5 | foo.length != 0 &&␊ + | ^^^^^^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 6 | foo.length > 0 &&␊ + 7 | 0 !== foo.length &&␊ + 8 | 0 != foo.length &&␊ + 9 | 0 < foo.length &&␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ + Error 5/9:␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + 3 | !!foo.length &&␊ + 4 | foo.length !== 0 &&␊ + 5 | foo.length != 0 &&␊ + > 6 | foo.length > 0 &&␊ + | ^^^^^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 7 | 0 !== foo.length &&␊ + 8 | 0 != foo.length &&␊ + 9 | 0 < foo.length &&␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ + Error 6/9:␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + 3 | !!foo.length &&␊ + 4 | foo.length !== 0 &&␊ + 5 | foo.length != 0 &&␊ + 6 | foo.length > 0 &&␊ + > 7 | 0 !== foo.length &&␊ + | ^^^^^^^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 8 | 0 != foo.length &&␊ + 9 | 0 < foo.length &&␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ + Error 7/9:␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + 3 | !!foo.length &&␊ + 4 | foo.length !== 0 &&␊ + 5 | foo.length != 0 &&␊ + 6 | foo.length > 0 &&␊ + 7 | 0 !== foo.length &&␊ + > 8 | 0 != foo.length &&␊ + | ^^^^^^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 9 | 0 < foo.length &&␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ + Error 8/9:␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + 3 | !!foo.length &&␊ + 4 | foo.length !== 0 &&␊ + 5 | foo.length != 0 &&␊ + 6 | foo.length > 0 &&␊ + 7 | 0 !== foo.length &&␊ + 8 | 0 != foo.length &&␊ + > 9 | 0 < foo.length &&␊ + | ^^^^^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 10 | 1 <= foo.length␊ + 11 | ) ? 1 : 2;␊ + Error 9/9:␊ + 1 | const foo = (␊ + 2 | foo.length &&␊ + 3 | !!foo.length &&␊ + 4 | foo.length !== 0 &&␊ + 5 | foo.length != 0 &&␊ + 6 | foo.length > 0 &&␊ + 7 | 0 !== foo.length &&␊ + 8 | 0 != foo.length &&␊ + 9 | 0 < foo.length &&␊ + > 10 | 1 <= foo.length␊ + | ^^^^^^^^^^^^^^^ Use `.length >= 1` when checking length is not zero.␊ + 11 | ) ? 1 : 2;␊ ` ## explicit-length-check - #4 @@ -64,22 +721,15 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `␊ - Options:␊ - [␊ - {␊ - "non-zero": "greater-than"␊ - }␊ - ]␊ - ␊ Input:␊ - 1 | if (array.length != 0) {}␊ + 1 | if (foo.bar && foo.bar.length) {}␊ ␊ Output:␊ - 1 | if (array.length > 0) {}␊ + 1 | if (foo.bar && foo.bar.length > 0) {}␊ ␊ Error 1/1:␊ - > 1 | if (array.length != 0) {}␊ - | ^^^^^^^^^^^^^^^^^ Non-zero `.length` should be compared with `> 0`.␊ + > 1 | if (foo.bar && foo.bar.length) {}␊ + | ^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ ` ## explicit-length-check - #5 @@ -87,20 +737,45 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `␊ - Options:␊ - [␊ - {␊ - "non-zero": "greater-than-or-equal"␊ - }␊ - ]␊ + Input:␊ + 1 | if (foo.length || foo.bar()) {}␊ + ␊ + Output:␊ + 1 | if (foo.length > 0 || foo.bar()) {}␊ + ␊ + Error 1/1:␊ + > 1 | if (foo.length || foo.bar()) {}␊ + | ^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + ` + +## explicit-length-check - #6 + +> Snapshot 1 + + `␊ + Input:␊ + 1 | if (!!(!!foo.length)) {}␊ + ␊ + Output:␊ + 1 | if (foo.length > 0) {}␊ ␊ + Error 1/1:␊ + > 1 | if (!!(!!foo.length)) {}␊ + | ^^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ + ` + +## explicit-length-check - #7 + +> Snapshot 1 + + `␊ Input:␊ - 1 | if (array.length != 0) {}␊ + 1 | if (!(foo.length === 0)) {}␊ ␊ Output:␊ - 1 | if (array.length >= 1) {}␊ + 1 | if (foo.length > 0) {}␊ ␊ Error 1/1:␊ - > 1 | if (array.length != 0) {}␊ - | ^^^^^^^^^^^^^^^^^ Non-zero `.length` should be compared with `>= 1`.␊ + > 1 | if (!(foo.length === 0)) {}␊ + | ^^^^^^^^^^^^^^^^^^^ Use `.length > 0` when checking length is not zero.␊ ` diff --git a/test/snapshots/explicit-length-check.js.snap b/test/snapshots/explicit-length-check.js.snap index 42af1fde86..d0bebe8262 100644 Binary files a/test/snapshots/explicit-length-check.js.snap and b/test/snapshots/explicit-length-check.js.snap differ