eslint-plugin-lodash
Version:
Lodash specific linting rules for ESLint
67 lines (54 loc) • 2.57 kB
JavaScript
/**
* @fileoverview Rule to check that iteratees for all collection functions except forEach return a value;
*/
;
/**
* @fileoverview Rule to check that iteratees for all collection functions except forEach return a value;
*/
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
create: function create(context) {
var _require = require('../util/lodashUtil');
var getLodashMethodCallExpVisitor = _require.getLodashMethodCallExpVisitor;
var getLodashImportVisitors = _require.getLodashImportVisitors;
var _require2 = require('../util/methodDataUtil');
var getCollectionMethods = _require2.getCollectionMethods;
var _require3 = require('../util/astUtil');
var isFunctionDefinitionWithBlock = _require3.isFunctionDefinitionWithBlock;
var _require4 = require('../util/ruleUtil');
var combineVisitorObjects = _require4.combineVisitorObjects;
var collectionMethods = void 0;
var funcInfos = new Map();
var currFuncInfo = {};
return combineVisitorObjects({
'CallExpression:exit': getLodashMethodCallExpVisitor(context, function (node, iteratee, _ref) {
var method = _ref.method;
var version = _ref.version;
collectionMethods = collectionMethods || new Set(getCollectionMethods(version));
if (collectionMethods.has(method) && funcInfos.has(iteratee)) {
var _funcInfos$get = funcInfos.get(iteratee);
var hasReturn = _funcInfos$get.hasReturn;
if (isFunctionDefinitionWithBlock(iteratee) && !hasReturn) {
context.report(node, 'Do not use _.' + method + ' without returning a value');
}
}
}),
ReturnStatement: function ReturnStatement() {
currFuncInfo.hasReturn = true;
},
onCodePathStart: function onCodePathStart(codePath, node) {
currFuncInfo = {
upper: currFuncInfo,
codePath: codePath,
hasReturn: false
};
funcInfos.set(node, currFuncInfo);
},
onCodePathEnd: function onCodePathEnd() {
currFuncInfo = currFuncInfo.upper;
}
}, getLodashImportVisitors(context));
}
};