eslint-plugin-lodash
Version:
Lodash specific linting rules for ESLint
74 lines (60 loc) • 2.56 kB
JavaScript
/**
* @fileoverview Rule to check if the macthesProperty shorthand can be used
*/
;
/**
* @fileoverview Rule to check if the macthesProperty shorthand can be used
*/
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
schema: [{
enum: ['always', 'never']
}, {
type: 'object',
properties: {
onlyLiterals: {
type: 'boolean'
}
}
}]
},
create: function create(context) {
var _require = require('../util/lodashUtil');
var isCallToLodashMethod = _require.isCallToLodashMethod;
var getShorthandVisitors = _require.getShorthandVisitors;
var _require2 = require('../util/astUtil');
var isEqEqEqToMemberOf = _require2.isEqEqEqToMemberOf;
var getValueReturnedInFirstStatement = _require2.getValueReturnedInFirstStatement;
var getFirstParamName = _require2.getFirstParamName;
var _require$getSettings = require('../util/settingsUtil').getSettings(context);
var version = _require$getSettings.version;
var onlyLiterals = context.options[1] && context.options[1].onlyLiterals;
function isFunctionDeclarationThatCanUseShorthand(func) {
return isEqEqEqToMemberOf(getValueReturnedInFirstStatement(func), getFirstParamName(func), { onlyLiterals: onlyLiterals });
}
function canUseShorthand(iteratee) {
return isFunctionDeclarationThatCanUseShorthand(iteratee) || isCallToLodashMethod(iteratee, 'matchesProperty', context);
}
function callHasExtraParamAfterIteratee(node, iteratee) {
return node.arguments[node.arguments.indexOf(iteratee) + 1];
}
var matchesPropertyChecks = {
3: function _(node, iteratee) {
return iteratee && iteratee.type === 'Literal' && callHasExtraParamAfterIteratee(node, iteratee);
},
4: function _(node, iteratee) {
return iteratee && iteratee.type === 'ArrayExpression';
}
};
return getShorthandVisitors(context, {
canUseShorthand: canUseShorthand,
usesShorthand: matchesPropertyChecks[version]
}, {
always: 'Prefer matches property syntax',
never: 'Do not use matches property syntax'
});
}
};