UNPKG

eslint-plugin-lodash

Version:

Lodash specific linting rules for ESLint

44 lines (33 loc) 1.59 kB
/** * @fileoverview Rule to check if a call to map and flatten should be a call to _.flatMap */ 'use strict'; /** * @fileoverview Rule to check if a call to map and flatten should be a call to _.flatMap */ //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = { create: function create(context) { var _require = require('../util/lodashUtil'); var getLodashMethodVisitors = _require.getLodashMethodVisitors; var isCallToMethod = _require.isCallToMethod; var isCallToLodashMethod = _require.isCallToLodashMethod; var _require2 = require('../util/astUtil'); var getCaller = _require2.getCaller; var _require3 = require('../util/methodDataUtil'); var isAliasOfMethod = _require3.isAliasOfMethod; function isChainedMapFlatten(callType, node, version) { return callType === 'chained' && isCallToMethod(getCaller(node), version, 'map'); } return getLodashMethodVisitors(context, function (node, iteratee, _ref) { var method = _ref.method; var version = _ref.version; var callType = _ref.callType; if (isAliasOfMethod(version, 'flatten', method) && (isChainedMapFlatten(callType, node, version) || isCallToLodashMethod(node.arguments[0], 'map', context))) { context.report(node, 'Prefer _.flatMap over consecutive map and flatten.'); } }); } };