Skip to content

Commit

Permalink
Add rule identity-shorthand
Browse files Browse the repository at this point in the history
(cherry picked from commit be0dce6)
  • Loading branch information
captbaritone authored and ganimomer committed Feb 21, 2016
1 parent 9e7d29e commit e1e5ee3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Out of the box, this plugin supports the use of Lodash v4. To use with Lodash v3
"rules": {
"lodash/prop-shorthand": [2, "always"],
"lodash/matches-shorthand": [2, "always", 3],
"lodash/identity-shorthand": [2, "always"],
"lodash/matches-prop-shorthand": [2, "always"],
"lodash/prefer-chain": [2, 3],
"lodash/preferred-alias": 2,
Expand Down Expand Up @@ -128,6 +129,7 @@ These rules are purely matters of style and are quite subjective.
* [prop-shorthand](docs/rules/prop-shorthand.md): Use/forbid property shorthand syntax.
* [matches-prop-shorthand](docs/rules/matches-prop-shorthand.md): Prefer matches property shorthand syntax
* [matches-shorthand](docs/rules/matches-shorthand.md): Prefer matches shorthand syntax
* [identity-shorthand](docs/rules/identity-shorthand.md): Prefer identity shorthand syntax
* [preferred-alias](docs/rules/preferred-alias.md): Prefer using main method names instead of aliases. (fixable)
* [prefer-chain](docs/rules/prefer-chain.md): Prefer a Lodash chain over nested Lodash calls
* [no-single-chain](docs/rules/no-single-chain.md): Prevent chaining syntax for single method, e.g. `_(x).map().value()`
Expand Down
29 changes: 29 additions & 0 deletions docs/rules/identity-shorthand.md
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.
56 changes: 56 additions & 0 deletions lib/rules/identity-shorthand.js
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']
}
];
57 changes: 57 additions & 0 deletions tests/lib/rules/identity-shorthand.js
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}]
}]
});

0 comments on commit e1e5ee3

Please sign in to comment.