/
prefer-array-some.js
48 lines (44 loc) · 1002 Bytes
/
prefer-array-some.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
}
};