Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename rule no-unnecessary-bind to callback-binding with explanation …
…about shared settings
- Loading branch information
Showing
6 changed files
with
115 additions
and
62 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,44 @@ | ||
# Callback binding | ||
|
||
In Lodash version 3, it was possible to pass an additional parameter to methods that require binding, `thisArg`. | ||
However, in Lodash 4, this option was removed in favor of regular binding, and still using the old method could cause unexpected results. | ||
|
||
## Rule Details | ||
|
||
This rule takes no arguments. However, it is affected by the lodash `version` defined in the config's [shared settings for Lodash](/README.md#shared-rule-settings). | ||
|
||
### Lodash Version 4 (default) | ||
In version 4, the following patterns are considered warnings: | ||
|
||
```js | ||
var r = _.filter(users, function (user) { | ||
return user.age > this.age; | ||
}, this); | ||
|
||
var r = _.reduce(numbers, multiply, 1, this); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
var r = _.filter(users, function (user) { | ||
return user.age > this.age; | ||
}.bind(this)); | ||
``` | ||
|
||
### Lodash version 3 | ||
|
||
In version 3, the following patterns are considered warnings: | ||
```js | ||
var r = _.filter(users, function (user) { | ||
return user.age > this.age; | ||
}, this); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
var r = _.filter(users, function (user) { | ||
return user.age > this.age; | ||
}.bind(this)); | ||
``` |
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
/** | ||
* @fileoverview Rule to disallow the use of a chain for a single method | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var lodashUtil = require('../util/lodashUtil'); | ||
var astUtil = require('../util/astUtil'); | ||
var settingsUtil = require('../util/settingsUtil'); | ||
|
||
var lodashPragma = settingsUtil.getLodashPragma(context); | ||
var majorVersion = settingsUtil.getMajorVersion(context); | ||
var transformerMethods = ['reduce', 'reduceRight', 'transform']; | ||
|
||
function isBound(node) { | ||
return node && node.type === 'CallExpression' && astUtil.getMethodName(node) === 'bind' && node.arguments.length === 1; | ||
} | ||
|
||
var callExpressionReporters = { | ||
3: function (node, iteratee) { | ||
if (isBound(iteratee)) { | ||
context.report(iteratee.callee.property, 'Unnecessary bind, pass `thisArg` to lodash method instead'); | ||
} | ||
}, | ||
4: function (node, iteratee) { | ||
var isTransformerMethod = transformerMethods.some(lodashUtil.isCallToMethod.bind(null, node)); | ||
var iterateeIndex = node.arguments.indexOf(iteratee); | ||
if ((isTransformerMethod && node.arguments[iterateeIndex + 2]) || (!isTransformerMethod && node.arguments[iterateeIndex + 1])) { | ||
context.report(iteratee, 'Do not use Lodash 3 thisArg, use binding instead'); | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
CallExpression: lodashUtil.getLodashMethodVisitor(lodashPragma, callExpressionReporters[majorVersion]) | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
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