UNPKG

eslint-plugin-lodash

Version:

Lodash specific linting rules for ESLint

54 lines (42 loc) 1.76 kB
/** * @fileoverview Rule to check if the property shorthand can be used */ 'use strict'; /** * @fileoverview Rule to check if the property shorthand can be used */ // ------------------------------------------------------------------------------ // Rule Definition // ------------------------------------------------------------------------------ module.exports = { meta: { schema: [{ enum: ['always', 'never'] }] }, create: function create(context) { var _require = require('../util/lodashUtil'); var isCallToLodashMethod = _require.isCallToLodashMethod; var getShorthandVisitors = _require.getShorthandVisitors; var _require2 = require('../util/astUtil'); var isMemberExpOf = _require2.isMemberExpOf; var getValueReturnedInFirstStatement = _require2.getValueReturnedInFirstStatement; var getFirstParamName = _require2.getFirstParamName; function isExplicitParamFunction(func) { return isMemberExpOf(getValueReturnedInFirstStatement(func), getFirstParamName(func), { allowComputed: false }); } function canUseShorthand(iteratee) { return isCallToLodashMethod(iteratee, 'property', context) || isExplicitParamFunction(iteratee); } function usesShorthand(node, iteratee) { return iteratee && iteratee.type === 'Literal' && !node.arguments[node.arguments.indexOf(iteratee) + 1]; } return getShorthandVisitors(context, { canUseShorthand: canUseShorthand, usesShorthand: usesShorthand }, { always: 'Prefer property shorthand syntax', never: 'Do not use property shorthand syntax' }); } };