UNPKG

eslint-plugin-lodash

Version:

Lodash specific linting rules for ESLint

59 lines (47 loc) 1.91 kB
/** * @fileoverview Rule to check if an indexOfComparison should be a call to _.includes */ 'use strict'; /** * @fileoverview Rule to check if an indexOfComparison should be a call to _.includes */ //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = { meta: { schema: [{ type: 'object', properties: { includeNative: { type: 'boolean' } } }] }, create: function create(context) { var includeNative = context.options[0] && context.options[0].includeNative; var _require = require('../util/astUtil'); var getExpressionComparedToInt = _require.getExpressionComparedToInt; var isIndexOfCall = _require.isIndexOfCall; var _require2 = require('../util/lodashUtil'); var getLodashMethodVisitors = _require2.getLodashMethodVisitors; var _require3 = require('../util/methodDataUtil'); var isAliasOfMethod = _require3.isAliasOfMethod; var visitors = getLodashMethodVisitors(context, function (node, iteratee, _ref) { var method = _ref.method; var version = _ref.version; if (isAliasOfMethod(version, 'indexOf', method) && node === getExpressionComparedToInt(node.parent, -1, true)) { context.report(node, 'Prefer _.includes over indexOf comparison to -1'); } }); if (includeNative) { visitors.BinaryExpression = function (node) { if (isIndexOfCall(getExpressionComparedToInt(node, -1, true))) { context.report(node, 'Prefer _.includes over indexOf comparison to -1'); } }; } return visitors; } };