UNPKG

eslint-plugin-lodash

Version:

Lodash specific linting rules for ESLint

52 lines (40 loc) 1.96 kB
/** * @fileoverview Rule to disallow the use of a chain for a single method */ 'use strict'; /** * @fileoverview Rule to disallow the use of a chain for a single method */ // ------------------------------------------------------------------------------ // Rule Definition // ------------------------------------------------------------------------------ module.exports = { create: function create(context) { var _require = require('../util/lodashUtil'); var getLodashMethodVisitors = _require.getLodashMethodVisitors; var _require2 = require('../util/astUtil'); var getMethodName = _require2.getMethodName; var _require$getSettings = require('../util/settingsUtil').getSettings(context); var version = _require$getSettings.version; var transformerMethods = ['reduce', 'reduceRight', 'transform']; function isBound(node) { return node && node.type === 'CallExpression' && getMethodName(node) === 'bind' && node.arguments.length === 1; } var callExpressionReporters = { 3: function _(node, iteratee) { if (isBound(iteratee)) { context.report(iteratee.callee.property, 'Unnecessary bind, pass `thisArg` to lodash method instead'); } }, 4: function _(node, iteratee, _ref) { var method = _ref.method; var isTransformerMethod = transformerMethods.includes(method); var iterateeIndex = node.arguments.indexOf(iteratee); if (iterateeIndex !== -1 && (isTransformerMethod && node.arguments[iterateeIndex + 2] || !isTransformerMethod && node.arguments[iterateeIndex + 1])) { context.report(iteratee, 'Do not use Lodash 3 thisArg, use binding instead'); } } }; return getLodashMethodVisitors(context, callExpressionReporters[version]); } };