UNPKG

eslint-plugin-lodash

Version:

Lodash specific linting rules for ESLint

117 lines (96 loc) 4.75 kB
/** * @fileoverview Rule to check if there's a method in the chain start that can be in the chain */ 'use strict'; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); module.exports = { meta: { schema: [{ type: 'object', properties: { ignoredMethods: { type: 'array', items: { type: 'string' } }, ignoredObjects: { type: 'array', items: { type: 'string' } } } }] }, create: function create(context) { var _require = require('../util/lodashUtil'); var isNativeCollectionMethodCall = _require.isNativeCollectionMethodCall; var getLodashImportVisitors = _require.getLodashImportVisitors; var getLodashMethodCallExpVisitor = _require.getLodashMethodCallExpVisitor; var _require2 = require('../util/ruleUtil'); var combineVisitorObjects = _require2.combineVisitorObjects; var _require3 = require('../util/astUtil'); var getMethodName = _require3.getMethodName; var getCaller = _require3.getCaller; var _map = ['get', 'includes', 'matches', 'some', 'map'].map(function (m) { return require('lodash/' + m); }); var _map2 = _slicedToArray(_map, 5); var get = _map2[0]; var includes = _map2[1]; var matches = _map2[2]; var some = _map2[3]; var map = _map2[4]; var ignoredMethods = get(context, ['options', 0, 'ignoreMethods'], []); var ignoredObjects = get(context, ['options', 0, 'ignoreObjects'], []); var usingLodash = new Set(); function isNonNullObjectCreate(callerName, methodName, arg) { return callerName === 'Object' && methodName === 'create' && get(arg, 'value') !== null; } function isStaticNativeMethodCall(node) { var staticMethods = { Object: ['assign', 'keys', 'values'], Array: ['isArray'] }; var callerName = get(node, 'callee.object.name'); var methodName = getMethodName(node); return callerName in staticMethods && includes(staticMethods[callerName], methodName) || isNonNullObjectCreate(callerName, methodName, node.arguments[0]); } function isNativeStringMethodCall(node) { return includes(['endsWith', 'includes', 'padEnd', 'padStart', 'startsWith'], getMethodName(node)); } function canUseLodash(node) { return isNativeCollectionMethodCall(node) || isStaticNativeMethodCall(node) || isNativeStringMethodCall(node); } function getTextOfNode(node) { if (node) { if (node.type === 'Identifier') { return node.name; } return context.getSourceCode().getText(node); } } function someMatch(patterns, str) { return str && some(patterns, function (pattern) { return str.match(pattern); }); } function shouldIgnore(node) { return someMatch(ignoredMethods, getMethodName(node)) || someMatch(ignoredObjects, getTextOfNode(getCaller(node))); } return combineVisitorObjects({ CallExpression: getLodashMethodCallExpVisitor(context, function (node) { usingLodash.add(node); }), 'CallExpression:exit': function CallExpressionExit(node) { if (!usingLodash.has(node) && !shouldIgnore(node) && canUseLodash(node)) { context.report(node, 'Prefer \'_.' + getMethodName(node) + '\' over the native function.'); } } }, getLodashImportVisitors(context)); } };