Skip to content

Commit

Permalink
add static methods and ES6 collection methods to prefer-lodash-method
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Nov 25, 2015
1 parent 23c7723 commit 7bcd3f2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/rules/prefer-lodash-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function (context) {
return {
CallExpression: function (node) {
var isLodashWrapperMethod = lodashUtil.isLodashWrapperMethod(node);
if (!lodashUtil.isLodashCall(node) && (lodashUtil.canBeLodashMethod(astUtil.getMethodName(node)) || isLodashWrapperMethod)) {
if (!lodashUtil.isLodashCall(node) && (lodashUtil.isNativeCollectionMethodCall(node) || isLodashWrapperMethod)) {
var caller = astUtil.getCaller(node);
if (lodashUtil.isLodashCall(caller) && astUtil.getMethodName(caller) !== 'chain' && !isLodashWrapperMethod ||
lodashUtil.isLodashWrapper(astUtil.getCaller(caller)) && lodashUtil.isChainBreaker(caller)) {
Expand Down
12 changes: 11 additions & 1 deletion lib/rules/prefer-lodash-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
//------------------------------------------------------------------------------

module.exports = function (context) {
var _ = require('lodash');
var lodashUtil = require('../util/lodashUtil');
var astUtil = require('../util/astUtil');

var REPORT_MESSAGE = 'Prefer \'_.{{method}}\' over the native function.';

function isStaticNativeMethodCall(node) {
var staticMethods = {
Object: ['assign', 'create', 'keys', 'values'],
Array: ['isArray']
};
var callerName = _.get(node, 'callee.object.name');
return (callerName in staticMethods) && _.includes(staticMethods[callerName], astUtil.getMethodName(node));
}

return {
CallExpression: function (node) {
if (!lodashUtil.isLodashCall(node) && !lodashUtil.isLodashWrapper(astUtil.getCaller(node)) && lodashUtil.canBeLodashMethod(astUtil.getMethodName(node))) {
if (!(lodashUtil.isLodashCall(node) || lodashUtil.isLodashWrapper(astUtil.getCaller(node))) && (lodashUtil.isNativeCollectionMethodCall(node) || isStaticNativeMethodCall(node))) {
context.report(node, REPORT_MESSAGE, {method: astUtil.getMethodName(node)});
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/util/lodashUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ function getIsTypeMethod(name) {
return _.includes(types, name) ? 'is' + _.capitalize(name) : null;
}

function canBeLodashMethod(name) {
return _.includes(['forEach', 'map', 'reduce', 'filter', 'every', 'some'], name);
function isNativeCollectionMethodCall(node) {
return _.includes(['every', 'fill', 'filter', 'find', 'findIndex', 'forEach', 'includes', 'indexOf', 'lastIndexOf', 'map', 'reduce', 'reduceRight', 'some'], astUtil.getMethodName(node));
}

function isLodashCollectionMethod(node) {
Expand All @@ -82,6 +82,6 @@ module.exports = {
isCallToMethod: isCallToMethod,
isLodashWrapperMethod: isLodashWrapperMethod,
getIsTypeMethod: getIsTypeMethod,
canBeLodashMethod: canBeLodashMethod,
isLodashCollectionMethod: isLodashCollectionMethod
isLodashCollectionMethod: isLodashCollectionMethod,
isNativeCollectionMethodCall: isNativeCollectionMethodCall
};
2 changes: 1 addition & 1 deletion tests/lib/rules/prefer-lodash-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var RuleTester = require('eslint').RuleTester;

var ruleTester = new RuleTester();
var errors = [{message: 'Prefer lodash chain'}];
ruleTester.run('prefer-lodash-method', rule, {
ruleTester.run('prefer-lodash-chain', rule, {
valid: [
'var userNames = users.map(function(user) { return user.name; });',
'var userNames = _(users).filter({active: true}).map("name").value();',
Expand Down
13 changes: 8 additions & 5 deletions tests/lib/rules/prefer-lodash-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ var RuleTester = require('eslint').RuleTester;
// ------------------------------------------------------------------------------

var ruleTester = new RuleTester();
var errors = [{message: 'Prefer \'_.map\' over the native function.'}];
ruleTester.run('prefer-lodash-method', rule, {
valid: [
'var x = _.map(arr, f)',
'var x = _(arr).map(f).reduce(g)',
'var x = _.chain(arr).map(f).reduce(g).value()'
'var x = _.chain(arr).map(f).reduce(g).value()',
'var x = _.keys(obj)'
],
invalid: [{
code: 'var x = a.map(function(x) {return x.f()});',
errors: errors
errors: [{message: 'Prefer \'_.map\' over the native function.'}]
}, {
code: 'var x = arr.map(x => x.f())',
code: 'var x = arr.filter(x => x.f())',
ecmaFeatures: {arrowFunctions: true},
errors: errors
errors: [{message: 'Prefer \'_.filter\' over the native function.'}]
}, {
code: 'var x = Object.keys(node)',
errors: [{message: "Prefer '_.keys' over the native function."}]
}]
});

0 comments on commit 7bcd3f2

Please sign in to comment.