Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explicit-length-check: Use 'non-zero': 'greater-than' by default #850

Merged
merged 2 commits into from Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was a typo because this section talks about zero comparisons. So I fixed it.


### Fail

Expand All @@ -35,13 +35,13 @@ if (string.length < 1) {}
### Pass

```js
if (array.length !== 0) {}
if (array.length === 0) {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was a typo because this section talks about zero comparisons. So I fixed it.

```


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