Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Prefer `.find(…)` over first element of `.filter(…)` result | ||
|
||
[`Array#find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) breaks the loop as soon as it finds a match. | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
const item = array.filter(x => x === '🦄')[0]; | ||
``` | ||
|
||
```js | ||
const item = array.filter(x => x === '🦄').shift(); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const item = array.find(x => x === '🦄'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const methodSelector = require('./utils/method-selector'); | ||
|
||
const MESSAGE_ID_ZERO_INDEX = 'prefer-find-over-filter-zero-index'; | ||
const MESSAGE_ID_SHIFT = 'prefer-find-over-filter-shift'; | ||
|
||
const zeroIndexSelector = [ | ||
'MemberExpression', | ||
'[computed=true]', | ||
'[property.type="Literal"]', | ||
'[property.raw="0"]', | ||
methodSelector({ | ||
name: 'filter', | ||
min: 1, | ||
max: 2, | ||
property: 'object' | ||
}) | ||
].join(''); | ||
|
||
const shiftSelector = [ | ||
methodSelector({ | ||
name: 'shift', | ||
length: 0 | ||
}), | ||
methodSelector({ | ||
name: 'filter', | ||
min: 1, | ||
max: 2, | ||
property: 'callee.object' | ||
}) | ||
].join(''); | ||
|
||
const create = context => { | ||
return { | ||
[zeroIndexSelector](node) { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_ZERO_INDEX, | ||
fix: fixer => [ | ||
fixer.replaceText(node.object.callee.property, 'find'), | ||
fixer.removeRange([node.object.range[1], node.range[1]]) | ||
] | ||
}); | ||
}, | ||
[shiftSelector](node) { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_SHIFT, | ||
fix: fixer => [ | ||
fixer.replaceText(node.callee.object.callee.property, 'find'), | ||
fixer.removeRange([node.callee.object.range[1], node.range[1]]) | ||
] | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
[MESSAGE_ID_ZERO_INDEX]: 'Prefer `.find(…)` over `.filter(…)[0]`.', | ||
[MESSAGE_ID_SHIFT]: 'Prefer `.find(…)` over `.filter(…).shift()`.' | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import test from 'ava'; | ||
import {outdent} from 'outdent'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/prefer-find'; | ||
|
||
const MESSAGE_ID_ZERO_INDEX = 'prefer-find-over-filter-zero-index'; | ||
const MESSAGE_ID_SHIFT = 'prefer-find-over-filter-shift'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
parserOptions: { | ||
ecmaVersion: 2020 | ||
} | ||
}); | ||
|
||
ruleTester.run('prefer-find', rule, { | ||
valid: [ | ||
'array.find(foo)', | ||
|
||
// Test `[0]` | ||
'array.filter(foo)', | ||
'array.filter(foo)[+0]', | ||
'array.filter(foo)[-0]', | ||
'array.filter(foo)[1-1]', | ||
'array.filter(foo)["0"]', | ||
'array.filter(foo).first', | ||
|
||
// Test `.shift()` | ||
// Not `CallExpression` | ||
'array.filter(foo).shift', | ||
// Not `MemberExpression` | ||
'shift(array.filter(foo))', | ||
// `callee.property` is not a `Identifier` | ||
'array.filter(foo)["shift"]()', | ||
// Computed | ||
'array.filter(foo)[shift]()', | ||
// Not `shift` | ||
'array.filter(foo).notShift(fn)', | ||
// More or less argument(s) | ||
'array.filter(foo).shift(extraArgument)', | ||
'array.filter(foo).shift(...[])', | ||
|
||
// Test `.filter()` | ||
// Not `CallExpression` | ||
'array.filter[0]', | ||
'array.filter.shift()', | ||
// Not `MemberExpression` | ||
'filter(foo)[0]', | ||
'filter(foo).shift()', | ||
// `callee.property` is not a `Identifier` | ||
'array["filter"](foo)[0]', | ||
'array["filter"](foo).shift()', | ||
// Computed | ||
'array[filter](foo)[0]', | ||
'array[filter](foo).shift()', | ||
// Not `filter` | ||
'array.notFilter(foo)[0]', | ||
'array.notFilter(foo).shift()', | ||
// More or less argument(s) | ||
'array.filter()[0]', | ||
'array.filter(foo, thisArgument, extraArgument)[0]', | ||
'array.filter(...foo)[0]', | ||
'array.filter().shift()', | ||
'array.filter(foo, thisArgument, extraArgument).shift()', | ||
'array.filter(...foo).shift()' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'array.filter(foo)[0]', | ||
output: 'array.find(foo)', | ||
errors: [{messageId: MESSAGE_ID_ZERO_INDEX}] | ||
}, | ||
{ | ||
code: 'array.filter(foo).shift()', | ||
output: 'array.find(foo)', | ||
errors: [{messageId: MESSAGE_ID_SHIFT}] | ||
}, | ||
{ | ||
code: 'array.filter(foo, thisArgument)[0]', | ||
output: 'array.find(foo, thisArgument)', | ||
errors: [{messageId: MESSAGE_ID_ZERO_INDEX}] | ||
}, | ||
{ | ||
code: 'array.filter(foo, thisArgument).shift()', | ||
output: 'array.find(foo, thisArgument)', | ||
errors: [{messageId: MESSAGE_ID_SHIFT}] | ||
}, | ||
{ | ||
code: outdent` | ||
const item = array | ||
// comment 1 | ||
.filter( | ||
// comment 2 | ||
x => x === '🦄' | ||
) | ||
// comment 3 | ||
.shift() | ||
// comment 4 | ||
; | ||
`, | ||
output: outdent` | ||
const item = array | ||
// comment 1 | ||
.find( | ||
// comment 2 | ||
x => x === '🦄' | ||
) | ||
// comment 4 | ||
; | ||
`, | ||
errors: [{messageId: MESSAGE_ID_SHIFT}] | ||
} | ||
] | ||
}); |