-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
10 changed files
with
311 additions
and
67 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,27 @@ | ||
# Prefer `.some(…)` over `.find(…)`. | ||
|
||
Prefer using [`Array#some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) over [`Array#find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) when ensuring at least one element in the array passes a given check. | ||
|
||
## Fail | ||
|
||
```js | ||
if (array.find(element => element === '🦄')) { | ||
// … | ||
} | ||
``` | ||
|
||
```js | ||
const foo = array.find(element => element === '🦄') ? bar : baz; | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
if (array.some(element => element === '🦄')) { | ||
// … | ||
} | ||
``` | ||
|
||
```js | ||
const foo = array.find(element => element === '🦄') || bar; | ||
``` |
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,48 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const methodSelector = require('./utils/method-selector'); | ||
const {isBooleanNode} = require('./utils/boolean'); | ||
|
||
const MESSAGE_ID_ERROR = 'error'; | ||
const MESSAGE_ID_SUGGESTION = 'suggestion'; | ||
const messages = { | ||
[MESSAGE_ID_ERROR]: 'Prefer `.some(…)` over `.find(…)`.', | ||
[MESSAGE_ID_SUGGESTION]: 'Replace `.find(…)` with `.some(…)`.' | ||
}; | ||
|
||
const arrayFindCallSelector = methodSelector({ | ||
name: 'find', | ||
min: 1, | ||
max: 2 | ||
}); | ||
|
||
const create = context => { | ||
return { | ||
[arrayFindCallSelector](node) { | ||
if (isBooleanNode(node)) { | ||
node = node.callee.property; | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_ERROR, | ||
suggest: [ | ||
{ | ||
messageId: MESSAGE_ID_SUGGESTION, | ||
fix: fixer => fixer.replaceText(node, 'some') | ||
} | ||
] | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
messages | ||
} | ||
}; |
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,83 @@ | ||
'use strict'; | ||
|
||
const isLogicNot = node => | ||
node && | ||
node.type === 'UnaryExpression' && | ||
node.operator === '!'; | ||
const isLogicNotArgument = node => | ||
isLogicNot(node.parent) && | ||
node.parent.argument === node; | ||
const isBooleanCallArgument = node => | ||
isBooleanCall(node.parent) && | ||
node.parent.arguments[0] === node; | ||
const isBooleanCall = node => | ||
node && | ||
node.type === 'CallExpression' && | ||
node.callee && | ||
node.callee.type === 'Identifier' && | ||
node.callee.name === 'Boolean' && | ||
node.arguments.length === 1; | ||
|
||
/** | ||
Check if the value of node is a `boolean`. | ||
@param {Node} node | ||
@returns {boolean} | ||
*/ | ||
function isBooleanNode(node) { | ||
if ( | ||
isLogicNot(node) || | ||
isLogicNotArgument(node) || | ||
isBooleanCall(node) || | ||
isBooleanCallArgument(node) | ||
) { | ||
return true; | ||
} | ||
|
||
const {parent} = node; | ||
if ( | ||
( | ||
parent.type === 'IfStatement' || | ||
parent.type === 'ConditionalExpression' || | ||
parent.type === 'WhileStatement' || | ||
parent.type === 'DoWhileStatement' || | ||
parent.type === 'ForStatement' | ||
) && | ||
parent.test === node | ||
) { | ||
return true; | ||
} | ||
|
||
if (parent.type === 'LogicalExpression') { | ||
return isBooleanNode(parent); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
Get the boolean type-casting ancestor. | ||
@typedef {{ node: Node, isNegative: boolean }} Result | ||
@param {Node} node | ||
@returns {Result} | ||
*/ | ||
function getBooleanAncestor(node) { | ||
let isNegative = false; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
if (isLogicNotArgument(node)) { | ||
isNegative = !isNegative; | ||
node = node.parent; | ||
} else if (isBooleanCallArgument(node)) { | ||
node = node.parent; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return {node, isNegative}; | ||
} | ||
|
||
module.exports = {isBooleanNode, getBooleanAncestor}; |
Oops, something went wrong.