Skip to content

Commit

Permalink
explicit-length-check: Use 'non-zero': 'greater-than' by default (#…
Browse files Browse the repository at this point in the history
…850)

* `explicit-length-check`: Use `'non-zero': 'greater-than'` by default

* npm run lint -- . --fix
  • Loading branch information
EvgenyOrekhov committed Oct 6, 2020
1 parent caf1389 commit 7c5df5f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
14 changes: 7 additions & 7 deletions 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.

Expand All @@ -10,21 +10,21 @@ This rule is partly fixable.
if (string.length) {}
if (array.length) {}
if (!array.length) {}
if (array.length !== 0) {}
```

### Pass

```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

Expand All @@ -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
{
Expand All @@ -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`
2 changes: 1 addition & 1 deletion rules/consistent-function-scoping.js
Expand Up @@ -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;
}
},
Expand Down
5 changes: 3 additions & 2 deletions rules/explicit-length-check.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-optional-catch-binding.js
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/utils/method-selector.js
Expand Up @@ -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(', ') +
Expand Down
14 changes: 12 additions & 2 deletions test/explicit-length-check.js
Expand Up @@ -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) {}'),
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 7c5df5f

Please sign in to comment.