From 7c5df5f1fe7e7896e5bcf11aa5f726cf7eac55dd Mon Sep 17 00:00:00 2001 From: Evgeny Orekhov Date: Tue, 6 Oct 2020 19:27:54 +0300 Subject: [PATCH] `explicit-length-check`: Use `'non-zero': 'greater-than'` by default (#850) * `explicit-length-check`: Use `'non-zero': 'greater-than'` by default * npm run lint -- . --fix --- docs/rules/explicit-length-check.md | 14 +++++++------- rules/consistent-function-scoping.js | 2 +- rules/explicit-length-check.js | 5 +++-- rules/prefer-optional-catch-binding.js | 2 +- rules/utils/method-selector.js | 2 +- test/explicit-length-check.js | 14 ++++++++++++-- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/docs/rules/explicit-length-check.md b/docs/rules/explicit-length-check.md index 72a75c35e8..aa220a34b2 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 a value array in an `if` condition, rather than checking the truthiness of the length. +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. This rule is partly fixable. @@ -10,6 +10,7 @@ This rule is partly fixable. if (string.length) {} if (array.length) {} if (!array.length) {} +if (array.length !== 0) {} ``` ### Pass @@ -17,14 +18,13 @@ if (!array.length) {} ```js if (string.length > 0) {} if (array.length > 0) {} -if (array.length !== 0) {} if (array.length === 0) {} ``` ## Zero comparisons -Enforce comparison with `!== 0` when checking for zero length. +Enforce comparison with `=== 0` when checking for zero length. ### Fail @@ -35,13 +35,13 @@ if (string.length < 1) {} ### Pass ```js -if (array.length !== 0) {} +if (array.length === 0) {} ``` ## Non-zero comparisons -You can define your preferred way of checking non-zero length by providing a `non-zero` option: +You can define your preferred way of checking non-zero length by providing a `non-zero` option (`greater-than` by default): ```js { @@ -53,9 +53,9 @@ You can define your preferred way of checking non-zero length by providing a `no 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` - `not-equal` - Enforces non-zero to be checked with: `array.length !== 0` -- `greater-than` - - Enforces non-zero to be checked with: `array.length > 0` - `greater-than-or-equal` - Enforces non-zero to be checked with: `array.length >= 1` diff --git a/rules/consistent-function-scoping.js b/rules/consistent-function-scoping.js index 6246d5b31a..cb4e6819e3 100644 --- a/rules/consistent-function-scoping.js +++ b/rules/consistent-function-scoping.js @@ -164,7 +164,7 @@ const create = context => { JSXElement: () => { // Turn off this rule if we see a JSX element because scope // references does not include JSXElement nodes. - if (functions.length !== 0) { + if (functions.length > 0) { functions[functions.length - 1] = true; } }, diff --git a/rules/explicit-length-check.js b/rules/explicit-length-check.js index 4d1c3addac..ec10863790 100644 --- a/rules/explicit-length-check.js +++ b/rules/explicit-length-check.js @@ -43,7 +43,7 @@ function checkZeroType(context, node) { } } -function checkNonZeroType(context, node, type) { +function checkNonZeroType(context, node, type = 'greater-than') { const {value} = node.right; const {operator} = node; @@ -162,7 +162,8 @@ const schema = [ type: 'object', properties: { 'non-zero': { - enum: ['not-equal', 'greater-than', 'greater-than-or-equal'] + enum: ['not-equal', 'greater-than', 'greater-than-or-equal'], + default: 'greater-than' } } } diff --git a/rules/prefer-optional-catch-binding.js b/rules/prefer-optional-catch-binding.js index bb700d9943..71b7f036f3 100644 --- a/rules/prefer-optional-catch-binding.js +++ b/rules/prefer-optional-catch-binding.js @@ -19,7 +19,7 @@ const create = context => { const scope = context.getScope(); const variable = findVariable(scope, node); - if (variable.references.length !== 0) { + if (variable.references.length > 0) { return; } diff --git a/rules/utils/method-selector.js b/rules/utils/method-selector.js index 569be8eed4..6a349b040b 100644 --- a/rules/utils/method-selector.js +++ b/rules/utils/method-selector.js @@ -28,7 +28,7 @@ module.exports = options => { selector.push(`[${prefix}callee.property.name="${name}"]`); } - if (Array.isArray(names) && names.length !== 0) { + if (Array.isArray(names) && names.length > 0) { selector.push( ':matches(' + names.map(name => `[${prefix}callee.property.name="${name}"]`).join(', ') + diff --git a/test/explicit-length-check.js b/test/explicit-length-check.js index c5b8bad482..aecbae31b2 100644 --- a/test/explicit-length-check.js +++ b/test/explicit-length-check.js @@ -33,8 +33,6 @@ ruleTester.run('explicit-length-check', rule, { testCase('if ("".length > 0) {}'), testCase('if (array.length === 0) {}'), testCase('if (array.length == 0) {}'), - testCase('if (array.length !== 0) {}'), - testCase('if (array.length !== 0 && array[0] === 1) {}'), testCase('if (array.length === 1) {}'), testCase('if (array.length <= 1) {}'), testCase('if (array.length > 1) {}'), @@ -117,6 +115,18 @@ ruleTester.run('explicit-length-check', rule, { ['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',