-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Consistent chain | ||
|
||
When chaining methods, it's often better to use only Lodash methods or wrappers. | ||
|
||
## Rule Details | ||
|
||
This rule takes no arguments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
var userNames = _.filter(users, {active: true}).map(function (user) { | ||
return user.name.givenName; | ||
}); | ||
|
||
var userNames = _(users).map("name.givenName").value().reduce(function (res, cur) { | ||
return res + " " + cur; | ||
}); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
var userNames = users.map(function(user) { | ||
return user.name; | ||
}); | ||
|
||
var userNames = _(users).filter({active: true}).map("name").value(); | ||
``` | ||
|
||
|
||
## When Not To Use It | ||
|
||
If you do not want to enforce using chains composed of Lodash functions, you should not use this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @fileoverview Rule to check if there's a JS native method in the lodash chain | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var lodashUtil = require('../util/lodashUtil'); | ||
var astUtil = require('../util/astUtil'); | ||
|
||
var REPORT_MESSAGE = 'Prefer lodash chain'; | ||
|
||
return { | ||
CallExpression: function (node) { | ||
var isLodashWrapperMethod = lodashUtil.isLodashWrapperMethod(node); | ||
if (!lodashUtil.isLodashCall(node) && (lodashUtil.canBeLodashMethod(astUtil.getMethodName(node)) || isLodashWrapperMethod)) { | ||
var caller = astUtil.getCaller(node); | ||
if (lodashUtil.isLodashCall(caller) && astUtil.getMethodName(caller) !== 'chain' && !isLodashWrapperMethod || | ||
lodashUtil.isLodashWrapper(astUtil.getCaller(caller)) && lodashUtil.isChainBreaker(caller)) { | ||
context.report(node, REPORT_MESSAGE, {method: astUtil.getMethodName(node)}); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/prefer-lodash-chain'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
var errors = [{message: 'Prefer lodash chain'}]; | ||
ruleTester.run('prefer-lodash-method', rule, { | ||
valid: [ | ||
'var userNames = users.map(function(user) { return user.name; });', | ||
'var userNames = _(users).filter({active: true}).map("name").value();', | ||
'var userNames = _(users).map("name.givenName").reduce(function (res, cur) { return res + " " + cur; });', | ||
'var userNames = users.map(cb1).filter(cb2);', | ||
'var userNames = _(users).map(cb1).reduce(cb2).map(cb3);', | ||
'var userNames = _(users).map("name.givenName").join(" ");', | ||
'var userNames = _.map(users, "name.givenName").join(" ");' | ||
], | ||
invalid: [ | ||
'var userNames = _.filter(users, cb1).map(cb2);', | ||
'var userNames = _(users).map("name.givenName").value().join(" ");', | ||
'var userNames = _.filter(users, {active: true}).map(function (user) { return user.name.givenName; });', | ||
'var userNames = _(users).map("name.givenName").value().reduce(cb);', | ||
'var userNames = _(users).map(cb1).filter(cb2).map(cb3).value().reduce(cb4);', | ||
'var userNames = _(users).map("name.givenName").value().reduce(function (res, cur) { return res + " " + cur; });', | ||
'var userNames = _.chain(users).filter("active").map("name.givenName").value().reduce(cb);' | ||
].map(function (code) { | ||
return {code: code, errors: errors}; | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters