Skip to content

Commit

Permalink
explicit-length-check: Remove greater-than-or-equal option (#1397)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 3, 2021
1 parent 100e906 commit db1a2b5
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 272 deletions.
2 changes: 0 additions & 2 deletions docs/rules/explicit-length-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ The `non-zero` option can be configured with one of the following:
- Enforces non-zero to be checked with: `foo.length > 0`
- `not-equal`
- Enforces non-zero to be checked with: `foo.length !== 0`
- `greater-than-or-equal`
- Enforces non-zero to be checked with: `foo.length >= 1`

## Unsafe to fix case

Expand Down
3 changes: 3 additions & 0 deletions docs/rules/prefer-array-some.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Prefer using [`Array#some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) over:

- Non-zero length check on the result of [`Array#filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter).

We only check `.filter().length > 0` and `.filter().length !== 0`. These two non-zero length check styles are allowed in [`unicorn/explicit-length-check`](./explicit-length-check.md#options) rule. It is recommended to use them together.

- Using [`Array#find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) to ensure at least one element in the array passes a given check.

This rule is fixable for `.filter(…).length` check and has a suggestion for `.find(…)`.
Expand Down
7 changes: 0 additions & 7 deletions rules/explicit-length-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ const nonZeroStyles = new Map([
code: '!== 0',
test: node => isCompareRight(node, '!==', 0)
}
],
[
'greater-than-or-equal',
{
code: '>= 1',
test: node => isCompareRight(node, '>=', 1)
}
]
]);
const zeroStyle = {
Expand Down
9 changes: 3 additions & 6 deletions rules/prefer-array-some.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ const arrayFindCallSelector = methodCallSelector({
const arrayFilterCallSelector = [
'BinaryExpression',
'[right.type="Literal"]',
// We assume the user already follows `unicorn/explicit-length-check`, these are allowed in that rule
matches([
'[operator=">"][right.raw="0"]',
'[operator="!=="][right.raw="0"]',
'[operator=">="][right.raw="1"]'
]),
'[right.raw="0"]',
// We assume the user already follows `unicorn/explicit-length-check`. These are allowed in that rule.
matches(['[operator=">"]', '[operator="!=="]']),
' > ',
`${memberExpressionSelector('length')}.left`,
' > ',
Expand Down
22 changes: 5 additions & 17 deletions test/explicit-length-check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ test({
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) {}',
Expand Down Expand Up @@ -122,9 +118,9 @@ test({
}),
suggestionCase({
code: 'const x = foo.length || bar()',
output: 'const x = foo.length >= 1 || bar()',
desc: 'Replace `.length` with `.length >= 1`.',
options: [{'non-zero': 'greater-than-or-equal'}]
output: 'const x = foo.length > 0 || bar()',
desc: 'Replace `.length` with `.length > 0`.',
options: [{'non-zero': 'greater-than'}]
}),
suggestionCase({
code: '() => foo.length && bar()',
Expand Down Expand Up @@ -168,18 +164,10 @@ test.snapshot({
`,
options: [{'non-zero': 'not-equal'}]
},
{
code: outdent`
const foo = (
${nonZeroCases.filter(code => code !== 'foo.length >= 1').join(' &&\n\t')}
) ? 1 : 2;
`,
options: [{'non-zero': 'greater-than-or-equal'}]
},
{
// Known, number static value
code: 'const foo = { length: 123 }; if (foo.length) {}',
options: [{'non-zero': 'greater-than-or-equal'}]
options: [{'non-zero': 'not-equal'}]
},
'if (foo.bar && foo.bar.length) {}',
'if (foo.length || foo.bar()) {}',
Expand Down Expand Up @@ -232,7 +220,7 @@ test.snapshot({
},
{
code: '<template><div v-if="foo.length"></div></template>',
options: [{'non-zero': 'greater-than-or-equal'}]
options: [{'non-zero': 'greater-than'}]
},
'<template><div v-if="foo.length && bar"></div></template>',
'<script>if (foo.length) {}</script>',
Expand Down
8 changes: 1 addition & 7 deletions test/prefer-array-some.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ test.snapshot({

// - `.filter(…).length > 0`
// - `.filter(…).length !== 0`
// - `.filter(…).length >= 1`
test.snapshot({
valid: [
// `> 0`
Expand All @@ -133,6 +132,7 @@ test.snapshot({
'0 !== array.filter(fn).length',

// `>= 1`
'array.filter(fn).length >= 1',
'array.filter(fn).length >= 1.',
'array.filter(fn).length >= 1.0',
'array.filter(fn).length >= 0x1',
Expand All @@ -158,7 +158,6 @@ test.snapshot({
invalid: [
'array.filter(fn).length > 0',
'array.filter(fn).length !== 0',
'array.filter(fn).length >= 1',
outdent`
if (
((
Expand Down Expand Up @@ -200,11 +199,6 @@ test.vue({
output: '<template><div v-if="foo.some(fn)"></div></template>',
errors: 1
},
{
code: '<template><div v-if="foo.filter(fn).length >=1"></div></template>',
output: '<template><div v-if="foo.some(fn)"></div></template>',
errors: 1
},
{
code: '<script>if (foo.filter(fn).length > 0);</script>',
output: '<script>if (foo.some(fn));</script>',
Expand Down

0 comments on commit db1a2b5

Please sign in to comment.