-
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.
add new rules prefer-invoke, prefer-map, prefer-wrapper-methods
- Loading branch information
Showing
10 changed files
with
266 additions
and
1 deletion.
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,30 @@ | ||
# Prefer invoke | ||
|
||
When using `_.map` with a method call of each item in the collection, it could improve readability by switching to `_.invoke` | ||
|
||
## Rule Details | ||
|
||
This rule takes no arguments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
_.map(arr, function(x) { return x.f(a, b)}); | ||
|
||
_(arr).filter(f).map(function(x) { return x.f()}).value(); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
|
||
var x = _.invoke(arr, 'f'); | ||
|
||
var x = _.invoke(collection, 'split', ':'); | ||
``` | ||
|
||
|
||
## When Not To Use It | ||
|
||
If you do not want to enforce using `_.invoke`, and prefer using `_.map` with a method call instead. |
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 @@ | ||
# Prefer map | ||
|
||
When using `_.forEach` that pushes into an array, it could improve readability to use `_.map` instead. | ||
|
||
## Rule Details | ||
|
||
This rule takes no arguments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
_.forEach(arr, function(x) { newArr.push(f(x))} | ||
|
||
``` | ||
The following patterns are not considered warnings: | ||
```js | ||
|
||
_.forEach(arr, function(x) { if (x.a) {a.push(x)}}) | ||
|
||
``` | ||
## When Not To Use It | ||
If you do not want to enforce using `map`, 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,31 @@ | ||
# Prefer wrapper methods | ||
|
||
When starting a chain with an initial value that contains a call to an array or string method, it could be better to move that method to the chain itself. | ||
|
||
## Rule Details | ||
|
||
This rule takes no arguments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
_(str.split(' ')).map(f).reduce(g); | ||
|
||
_.chain(str.split(' ')).map(f).reduce(g).value(); | ||
|
||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
|
||
_(str).split(' ').map(f).reduce(g); | ||
|
||
_.chain(str).split(' ').map(f).reduce(g).value(); | ||
``` | ||
|
||
|
||
## When Not To Use It | ||
|
||
If you do not want to enforce using `map`, 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,25 @@ | ||
/** | ||
* @fileoverview Rule to check if a call to map should be a call to invoke | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var lodashUtil = require('../util/lodashUtil'); | ||
var astUtil = require('../util/astUtil'); | ||
|
||
function isFunctionMethodCallOfParam(func) { | ||
return astUtil.isCallFromObject(astUtil.getValueReturnedInFirstLine(func), astUtil.getFirstParamName(func)); | ||
} | ||
|
||
return { | ||
CallExpression: function (node) { | ||
if (lodashUtil.isCallToMethod(node, 'map') && isFunctionMethodCallOfParam(lodashUtil.getLodashIteratee(node))) { | ||
context.report(node, 'Prefer _.invoke over map to a method call.'); | ||
} | ||
} | ||
}; | ||
}; |
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,27 @@ | ||
/** | ||
* @fileoverview Rule to check if a call to _.forEach should be a call to _.filter | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var lodashUtil = require('../util/lodashUtil'); | ||
var astUtil = require('../util/astUtil'); | ||
|
||
function onlyHasPush(func) { | ||
var firstLine = astUtil.getFirstFunctionLine(func); | ||
var exp = func.type === 'ArrowFunctionExpression' ? firstLine : firstLine.expression; | ||
return astUtil.getMethodName(exp) === 'push'; | ||
} | ||
|
||
return { | ||
CallExpression: function (node) { | ||
if (lodashUtil.isCallToMethod(node, 'forEach') && onlyHasPush(lodashUtil.getLodashIteratee(node))) { | ||
context.report(node, 'Prefer _.map over a _.forEach with a push to an array inside'); | ||
} | ||
} | ||
}; | ||
}; |
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,25 @@ | ||
/** | ||
* @fileoverview Rule to check if there's a method in the chain start that can be in the chain | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var lodashUtil = require('../util/lodashUtil'); | ||
var _ = require('lodash'); | ||
var WRAPPER_METHODS = ['concat', 'join', 'pop', 'push', 'reverse', 'shift', 'slice', 'sort', 'splice', 'unshift', 'replace', 'split']; | ||
function isCallToWrapperMethod(node) { | ||
return node && node.type === 'CallExpression' && _.includes(WRAPPER_METHODS, _.get(node, 'callee.property.name')); | ||
} | ||
|
||
return { | ||
CallExpression: function (node) { | ||
if (lodashUtil.isLodashChainStart(node) && isCallToWrapperMethod(node.arguments[0])) { | ||
context.report(node, 'Prefer {{name}} with wrapper method over inside the chain start.', {name: node.arguments[0].callee.property.name}); | ||
} | ||
} | ||
}; | ||
}; |
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,33 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/prefer-invoke'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
var errors = [{message: 'Prefer _.invoke over map to a method call.'}]; | ||
ruleTester.run('prefer-invoke', rule, { | ||
valid: [{ | ||
code: 'var x = _.invoke(arr, "f")' | ||
}, { | ||
code: 'var x = _.invoke(arr, "split", " ")' | ||
}], | ||
invalid: [{ | ||
code: '_.map(a, function(x) {return x.f()});', | ||
errors: errors | ||
}, { | ||
code: '_(a).filter(f).map(function(x) { return x.split(" ")})', | ||
errors: errors | ||
}, { | ||
code: '_.map(arr, x => x.f())', | ||
ecmaFeatures: {arrowFunctions: true}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/prefer-map'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
var ruleError = {message: 'Prefer _.map over a _.forEach with a push to an array inside'}; | ||
ruleTester.run('prefer-map', rule, { | ||
valid: [{ | ||
code: 'var x = _.map(arr, function(x) {return x + 7})' | ||
}, { | ||
code: '_.forEach(arr, function(x) { if (x.a) {a.push(x)}})' | ||
}], | ||
invalid: [{ | ||
code: '_(arr).forEach(function(x) { a.push(x)})', | ||
errors: [ruleError] | ||
}, { | ||
code: '_(arr).forEach(function(x) { a.push(f(x))})', | ||
errors: [ruleError] | ||
}, { | ||
code: '_(arr).forEach(x => a.push(f(x)))', | ||
ecmaFeatures: {arrowFunctions: true}, | ||
errors: [ruleError] | ||
}] | ||
}); |
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,27 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/prefer-wrapper-method'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
var errors = [{message: 'Prefer split with wrapper method over inside the chain start.'}]; | ||
ruleTester.run('prefer-wrapper-method', rule, { | ||
valid: [{ | ||
code: 'var x = _(str).split(c).map(f).reduce(g)' | ||
}], | ||
invalid: [{ | ||
code: '_(str.split(c)).map(f).reduce(g)', | ||
errors: errors | ||
}, { | ||
code: '_.chain(str.split(c)).map(f).reduce(g).value()', | ||
errors: errors | ||
}] | ||
}); |
I think you meant
prefer-wrapper-method