/
prefer-reject.js
39 lines (32 loc) · 1.4 KB
/
prefer-reject.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
/**
* @fileoverview Rule to check if a call to filter should be a call to reject
*/
'use strict';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function (context) {
var lodashUtil = require('../util/lodashUtil');
var astUtil = require('../util/astUtil');
var lodashPragma = require('../util/settingsUtil').getLodashPragma(context);
var DEFAULT_MAX_PROPERTY_PATH_LENGTH = 3;
var maxPropertyPathLength = parseInt(context.options[0], 10) || DEFAULT_MAX_PROPERTY_PATH_LENGTH;
function isNegativeExpressionFunction(func) {
var returnValue = astUtil.getValueReturnedInFirstLine(func);
var firstParamName = astUtil.getFirstParamName(func);
return astUtil.isNegationOfParamMember(returnValue, firstParamName, maxPropertyPathLength) ||
astUtil.isNotEqEqToMemberOf(returnValue, firstParamName, maxPropertyPathLength);
}
return {
CallExpression: lodashUtil.getLodashMethodVisitor(lodashPragma, function (node, iteratee) {
if (lodashUtil.isCallToMethod(node, 'filter') && isNegativeExpressionFunction(iteratee)) {
context.report(node, 'Prefer _.reject over negative condition');
}
})
};
};
module.exports.schema = [
{
type: 'integer'
}
];