eslint-plugin-lodash
Version:
Lodash specific linting rules for ESLint
71 lines (54 loc) • 2.91 kB
JavaScript
/**
* @fileoverview Rule to check if the identity shorthand can be used
*/
;
/**
* @fileoverview Rule to check if the identity shorthand can be used
*/
// ------------------------------------------------------------------------------
// 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: [{
enum: ['always', 'never']
}]
},
create: function create(context) {
var _map = ['get', 'matches', 'overSome'].map(function (m) {
return require('lodash/' + m);
});
var _map2 = _slicedToArray(_map, 3);
var get = _map2[0];
var matches = _map2[1];
var overSome = _map2[2];
var _require = require('../util/lodashUtil');
var methodSupportsShorthand = _require.methodSupportsShorthand;
var getShorthandVisitors = _require.getShorthandVisitors;
var _require2 = require('../util/astUtil');
var getFirstParamName = _require2.getFirstParamName;
var getValueReturnedInFirstStatement = _require2.getValueReturnedInFirstStatement;
var settings = require('../util/settingsUtil').getSettings(context);
function isExplicitIdentityFunction(iteratee) {
var firstParamName = getFirstParamName(iteratee);
return firstParamName && get(getValueReturnedInFirstStatement(iteratee), 'name') === firstParamName;
}
var isLodashIdentityFunction = matches({
type: 'MemberExpression',
object: { name: settings.pragma },
property: { name: 'identity' }
});
var canUseShorthand = overSome(isExplicitIdentityFunction, isLodashIdentityFunction);
function usesShorthand(node, iteratee, method) {
return methodSupportsShorthand(settings.version, method) && !iteratee;
}
return getShorthandVisitors(context, {
canUseShorthand: canUseShorthand,
usesShorthand: usesShorthand
}, {
always: 'Prefer omitting the iteratee over a function that returns its argument',
never: 'Do not use the identity shorthand syntax'
});
}
};