eslint-plugin-lodash
Version:
Lodash specific linting rules for ESLint
55 lines (43 loc) • 2.1 kB
JavaScript
/**
* @fileoverview Rule to check if a call to filter should be a call to reject
*/
;
/**
* @fileoverview Rule to check if a call to filter should be a call to reject
*/
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
schema: [{
type: 'integer'
}]
},
create: function create(context) {
var _require = require('../util/lodashUtil');
var isCallToLodashMethod = _require.isCallToLodashMethod;
var getLodashMethodVisitors = _require.getLodashMethodVisitors;
var _require2 = require('../util/astUtil');
var getValueReturnedInFirstStatement = _require2.getValueReturnedInFirstStatement;
var getFirstParamName = _require2.getFirstParamName;
var isNegationOfMemberOf = _require2.isNegationOfMemberOf;
var isNotEqEqToMemberOf = _require2.isNotEqEqToMemberOf;
var _require3 = require('../util/methodDataUtil');
var isAliasOfMethod = _require3.isAliasOfMethod;
var DEFAULT_MAX_PROPERTY_PATH_LENGTH = 3;
var maxLength = parseInt(context.options[0], 10) || DEFAULT_MAX_PROPERTY_PATH_LENGTH;
function isNegativeExpressionFunction(func) {
var returnValue = getValueReturnedInFirstStatement(func);
var firstParamName = getFirstParamName(func);
return isNegationOfMemberOf(returnValue, firstParamName, { maxLength: maxLength }) || isNotEqEqToMemberOf(returnValue, firstParamName, { maxLength: maxLength }) || isCallToLodashMethod(func, 'negate', context);
}
return getLodashMethodVisitors(context, function (node, iteratee, _ref) {
var method = _ref.method;
var version = _ref.version;
if (isAliasOfMethod(version, 'filter', method) && isNegativeExpressionFunction(iteratee)) {
context.report(node, 'Prefer _.reject over negative condition');
}
});
}
};