-
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.
Showing
4 changed files
with
144 additions
and
0 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,29 @@ | ||
# Identity shorthand | ||
|
||
When using certain method in lodash such as max, it's possible to use the | ||
`_.identity` callback shorthand. This rule will enforce whether or not to use | ||
shorthand when possible to keep consistency in your code. | ||
|
||
## Rule Details | ||
|
||
This rule takes one argument, when to use shorthand: `always` or `never` (default is always). | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
/*eslint lodash/identity-shorthand: [2, "always"] | ||
var topScore = _.max(scores, function (score) { | ||
return score; | ||
}); | ||
``` | ||
The following patterns are not considered warnings: | ||
```js | ||
/*eslint lodash/identity-shorthand: [2, "never"] | ||
var topScore = _.max(scores); | ||
``` | ||
## When Not To Use It | ||
If you do not want to enforce whether or not to use the `_.identity` callback shorthand, then you can disable 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,56 @@ | ||
/** | ||
* @fileoverview Rule to check if the identity shorthand can be used | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
|
||
module.exports = function (context) { | ||
var _ = require('lodash'); | ||
var lodashUtil = require('../util/lodashUtil'); | ||
var astUtil = require('../util/astUtil'); | ||
var settings = require('../util/settingsUtil').getSettings(context); | ||
|
||
var supportsProp = require('../util/methodDataUtil').getPropShorthandMethods(settings.version); | ||
function canUseIdentityShorthand(node) { | ||
var ret = astUtil.getValueReturnedInFirstLine(node); | ||
return ret && astUtil.getFirstParamName(node) === ret.name; | ||
} | ||
|
||
function methodSupportsShorthand(node) { | ||
return _.includes(supportsProp, astUtil.getMethodName(node)); | ||
} | ||
|
||
function usesIdentityShorthand(node) { | ||
return node.arguments.length === 1; | ||
} | ||
|
||
var callExpressionReporters = { | ||
always: function (node, iteratee) { | ||
if (methodSupportsShorthand(node) && canUseIdentityShorthand(iteratee)) { | ||
context.report({ | ||
node: iteratee, | ||
message: 'Prefer omitting the iteratee over a function that returns its argument' | ||
}); | ||
} | ||
}, | ||
never: function (node) { | ||
if (usesIdentityShorthand(node)) { | ||
context.report(node.callee.property, 'Do not use the identity shorthand syntax'); | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
CallExpression: lodashUtil.getLodashMethodVisitor(settings, callExpressionReporters[context.options[0] || 'always']) | ||
}; | ||
}; | ||
|
||
module.exports.schema = [ | ||
{ | ||
enum: ['always', 'never'] | ||
} | ||
]; |
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,57 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/identity-shorthand'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
|
||
var messages = { | ||
always: 'Prefer omitting the iteratee over a function that returns its argument', | ||
never: 'Do not use the identity shorthand syntax' | ||
}; | ||
|
||
ruleTester.run('identity-shorthand', rule, { | ||
valid: [ | ||
'var ids = _.map([], function (i) { return x; });', | ||
'var ids = _.map([], function (i) { return i + "?"; });', | ||
'var r = _.map([], function() { return; })', | ||
'var ids = _.map([]);', | ||
{ | ||
code: 'var r = _.map([], function(x) { return x; })', | ||
options: ['never'] | ||
}, { | ||
code: 'var r = _.map([], x => x)', | ||
options: ['never'], | ||
ecmaFeatures: {arrowFunctions: true} | ||
} | ||
], | ||
invalid: [{ | ||
code: 'var ids = _.map([], function (i) { return i; });', | ||
errors: [{message: messages.always, column: 21}] | ||
}, { | ||
code: 'var r = _.map([], x => x);', | ||
ecmaFeatures: {arrowFunctions: true}, | ||
errors: [{message: messages.always, column: 19}] | ||
}, { | ||
code: 'var ids = _.chain([]).map(function (i) { return i; }).value();', | ||
errors: [{message: messages.always, column: 27}] | ||
}, { | ||
code: 'var ids = _([]).map(function (i) { return i; });', | ||
errors: [{message: messages.always, column: 21}] | ||
}, { | ||
code: 'var ids = _.map(arr);', | ||
options: ['never'], | ||
errors: [{message: messages.never, column: 13}] | ||
}, { | ||
code: 'var ids = _(arr).map("x").map("y").map(function (i) { return i; });', | ||
errors: [{message: messages.always, column: 40}] | ||
}] | ||
}); |