-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Prefer reject | ||
|
||
When using _.filter with a negative condition, it could improve readability by switching to _.reject | ||
|
||
## Rule Details | ||
|
||
This rule takes no arguments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
_.filter(arr, function(x) { return x.a !== b}); | ||
|
||
_.filter(arr, function(x) {return !x.isSomething}) | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
|
||
var x = _.filter(arr, function(x) {return !x.a && p}); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ganimomer
Author
Contributor
|
||
|
||
var x = _.filter(arr, function(x) {return !f(x)}; // The function f could take multiple arguments, e.g. parseInt | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ganimomer
Author
Contributor
|
||
``` | ||
## When Not To Use It | ||
If you do not want to enforce using `_.reject`, you should not use this rule. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @fileoverview Rule to check if a call to filter should be a call to reject | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var esUtil = require('../util/esUtil'); | ||
var aliases = require('../util/aliases'); | ||
|
||
function isFilter(node) { | ||
return aliases.isAliasOfMethod('filter', esUtil.getMethodName(node)); | ||
} | ||
|
||
function isParamNegation(exp, firstParamName) { | ||
return exp && exp.type === 'UnaryExpression' && exp.operator === '!' && esUtil.isMemberExpOfArg(exp.argument, firstParamName); | ||
} | ||
|
||
function isParamNotEqEq(exp, firstParamName) { | ||
return exp && exp.type === 'BinaryExpression' && exp.operator === '!==' && | ||
(esUtil.isMemberExpOfArg(exp.left, firstParamName) || esUtil.isMemberExpOfArg(exp.right, firstParamName)); | ||
} | ||
|
||
function isNegativeExpressionFunction(func) { | ||
var firstLine = esUtil.getFirstFunctionLine(func); | ||
var firstParamName = esUtil.getFirstParamName(func); | ||
return esUtil.isReturnStatement(firstLine) && | ||
(isParamNegation(firstLine.argument, firstParamName) || | ||
isParamNotEqEq(firstLine.argument, firstParamName)); | ||
} | ||
|
||
return { | ||
CallExpression: function (node) { | ||
try { | ||
if (isFilter(node) && isNegativeExpressionFunction(esUtil.getLodashIteratee(node))) { | ||
context.report(node, 'Prefer _.reject over negative condition'); | ||
} | ||
} catch (e) { | ||
context.report(node, 'Error executing rule: ' + e + ' ' + e.stack); | ||
} | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/prefer-reject'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
var ruleError = {message: 'Prefer _.reject over negative condition'}; | ||
ruleTester.run('prefer-reject', rule, { | ||
valid: [{ | ||
code: 'var x = _.filter(arr, function(x) {return !x.a && p})' | ||
}], | ||
invalid: [{ | ||
code: '_(arr).map(f).filter(function(x) {return !x.isSomething})', | ||
errors: [ruleError] | ||
}, { | ||
code: '_.filter(arr, function(x) { return x.a !== b})', | ||
errors: [ruleError] | ||
}, { | ||
code: '_.filter(arr, function(x) {return !x.isSomething})', | ||
errors: [ruleError] | ||
}] | ||
}); |
I think you meant
reject