-
Notifications
You must be signed in to change notification settings - Fork 65
/
prefer-map.js
27 lines (23 loc) · 1.03 KB
/
prefer-map.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
/**
* @fileoverview Rule to check if a call to _.forEach should be a call to _.filter
*/
'use strict';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function (context) {
var lodashUtil = require('../util/lodashUtil');
var astUtil = require('../util/astUtil');
function onlyHasPush(func) {
var firstLine = astUtil.getFirstFunctionLine(func);
var exp = func && func.type === 'ArrowFunctionExpression' ? firstLine : firstLine && firstLine.expression;
return astUtil.hasOnlyOneStatement(func) && astUtil.getMethodName(exp) === 'push';
}
return {
CallExpression: lodashUtil.getLodashMethodVisitor(function (node, iteratee) {
if (lodashUtil.isCallToMethod(node, 'forEach') && onlyHasPush(iteratee)) {
context.report(node, 'Prefer _.map over a _.forEach with a push to an array inside');
}
})
};
};