-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: fisker Cheung <[email protected]> Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
8b4c502
commit 06ed7ee
Showing
10 changed files
with
332 additions
and
39 deletions.
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,48 @@ | ||
# Disallow `Array#reduce()` and `Array#reduceRight()` | ||
|
||
`Array#reduce()` and `Array#reduceRight()` usually [result in hard-to-read code](https://twitter.com/jaffathecake/status/1213077702300852224). In almost every case, it can be replaced by `.map`, `.filter`, or a `for-of` loop. It's only somewhat useful in the rare case of summing up numbers. | ||
|
||
Use `eslint-disable` comment if you really need to use it. | ||
|
||
This rule is not fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
array.reduce(reducer, initialValue); | ||
``` | ||
|
||
```js | ||
array.reduceRight(reducer, initialValue); | ||
``` | ||
|
||
```js | ||
array.reduce(reducer); | ||
``` | ||
|
||
```js | ||
[].reduce.call(array, reducer); | ||
``` | ||
|
||
```js | ||
[].reduce.apply(array, [reducer, initialValue]); | ||
``` | ||
|
||
```js | ||
Array.prototype.reduce.call(array, reducer); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
// eslint-disable-next-line | ||
array.reduce(reducer, initialValue); | ||
``` | ||
|
||
```js | ||
let result = initialValue; | ||
|
||
for (const element of array) { | ||
result += element; | ||
} | ||
``` |
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
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,78 @@ | ||
'use strict'; | ||
const methodSelector = require('./utils/method-selector'); | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
|
||
const MESSAGE_ID_REDUCE = 'reduce'; | ||
const MESSAGE_ID_REDUCE_RIGHT = 'reduceRight'; | ||
|
||
const ignoredFirstArgumentSelector = `:not(${ | ||
[ | ||
'[arguments.0.type="Literal"]', | ||
'[arguments.0.type="Identifier"][arguments.0.name="undefined"]' | ||
].join(',') | ||
})`; | ||
|
||
const PROTOTYPE_SELECTOR = [ | ||
methodSelector({names: ['call', 'apply']}), | ||
ignoredFirstArgumentSelector, | ||
'[callee.object.type="MemberExpression"]', | ||
'[callee.object.computed=false]', | ||
`:matches(${ | ||
['reduce', 'reduceRight'].map(method => `[callee.object.property.name="${method}"]`).join(', ') | ||
})`, | ||
'[callee.object.property.type="Identifier"]', | ||
`:matches(${ | ||
[ | ||
// `[].reduce` | ||
[ | ||
'type="ArrayExpression"', | ||
'elements.length=0' | ||
], | ||
// `Array.prototype.reduce` | ||
[ | ||
'type="MemberExpression"', | ||
'computed=false', | ||
'property.type="Identifier"', | ||
'property.name="prototype"', | ||
'object.type="Identifier"', | ||
'object.name="Array"' | ||
] | ||
].map( | ||
selectors => selectors | ||
.map(selector => `[callee.object.object.${selector}]`) | ||
.join('') | ||
).join(', ') | ||
})` | ||
].join(''); | ||
|
||
const METHOD_SELECTOR = [ | ||
methodSelector({names: ['reduce', 'reduceRight'], min: 1, max: 2}), | ||
ignoredFirstArgumentSelector | ||
].join(''); | ||
|
||
const create = context => { | ||
return { | ||
[METHOD_SELECTOR](node) { | ||
// For arr.reduce() | ||
context.report({node: node.callee.property, messageId: node.callee.property.name}); | ||
}, | ||
[PROTOTYPE_SELECTOR](node) { | ||
// For cases [].reduce.call() and Array.prototype.reduce.call() | ||
context.report({node: node.callee.object.property, messageId: node.callee.object.property.name}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
messages: { | ||
[MESSAGE_ID_REDUCE]: '`Array#reduce()` is not allowed', | ||
[MESSAGE_ID_REDUCE_RIGHT]: '`Array#reduceRight()` is not allowed' | ||
} | ||
} | ||
}; |
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
Oops, something went wrong.