Skip to content

Commit

Permalink
add matches prop rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ido committed Aug 13, 2015
1 parent f9d59a4 commit 06d6706
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"no-unused-vars": 2,
"no-use-before-define": 2,
// Stylistic Issues
"indent": [2, 2, {"SwitchCase": 1}],
"indent": [2, 4, {"SwitchCase": 1}],
"brace-style": 2,
"camelcase": 0,
"comma-spacing": 2,
Expand Down Expand Up @@ -140,7 +140,7 @@
"wrap-regex": 0,
// Legacy
"max-depth": 0,
"max-len": [2, 120],
"max-len": [2, 200],
"max-params": 0,
"max-statements": 0,
"no-plusplus": 0
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Finally, enable all of the rules that you would like to use.
{
"rules": {
"lodash3/prop-shorthand": 1,
"lodash3/matches-prop-shorthand": 1,
"lodash3/prefer-chain": 1,
"lodash3/preferred-alias": 1
}
Expand All @@ -41,7 +42,10 @@ Finally, enable all of the rules that you would like to use.

# List of supported rules

* [prop-shorthand](docs/rules/prop-shorthand.md): Prefer shorthand pluck syntax
* [prop-shorthand](docs/rules/prop-shorthand.md): Prefer property shorthand syntax
* [prop-shorthand](docs/rules/matches-prop-shorthand.md): Prefer matches property shorthand syntax
* [prop-shorthand](docs/rules/preferred-alias.md): Preferred aliases
* [prop-shorthand](docs/rules/prefer-chain.md): Prefer chain



Expand Down
24 changes: 24 additions & 0 deletions docs/rules/matches-prop-shorthand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Matches property shorthand

When using certain method in lodash such as filter, code can be shorter and more readable when using the `_.matchesProperty` callback shorthand. This rule will enforce using shorthand when possible to keep consistency in your code.

## Rule Details

This rule takes no arguments.

The following patterns are considered warnings:

```js
var result = _.filter(users, function (i) { return i.id === 3; });
```

The following patterns are not considered warnings:

```js
var result = _.filter(users, 'id', 3);
```


## When Not To Use It

If you do not want to enforce `_.matchesProperty` callback shorthand, then you can disable this rule.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module.exports = {
rules: {
'preferred-alias': require('./lib/rules/preferred-alias'),
'prefer-chain': require('./lib/rules/prefer-chain'),
'prop-shorthand': require('./lib/rules/prop-shorthand')
'prop-shorthand': require('./lib/rules/prop-shorthand'),
'matches-prop-shorthand': require('./lib/rules/matches-prop-shorthand')
},
rulesConfig: {
'preferred-alias': 0,
'prefer-chain': 0,
'prop-shorthand': 0
'prop-shorthand': 0,
'matches-prop-shorthand': 0
}
};
52 changes: 52 additions & 0 deletions lib/rules/matches-prop-shorthand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @fileoverview Rule to check if the property shorthand can be used
*/
'use strict';

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = function (context) {
var _ = require('lodash');
//var aliases = require('../util/aliases');

var matchesProperty = ['find', 'detect', 'filter', 'select', 'reject'];

function isEq(left, right, itemName) {
return left.type === 'MemberExpression' &&
left.object &&
left.object.name === itemName;
}

function shouldPreferPluck(func) {
if (func.params.length) {
var itemName = func.params[0].name;
var ret = func.body.body[0];
return ret && ret.type === 'ReturnStatement' &&
ret.argument.type === 'BinaryExpression' &&
(isEq(ret.argument.left, ret.argument.right, itemName) || isEq(ret.argument.right, ret.argument.left, itemName));
}
return false;
}

function isLodashCollectionFunction(node) {
return _.get(node, 'callee.object.name') === '_' && _.includes(matchesProperty, _.get(node, 'callee.property.name'));
}

return {
CallExpression: function (node) {
try {
if (isLodashCollectionFunction(node) && node.arguments.length > 1 && node.arguments[1].type === 'FunctionExpression' && shouldPreferPluck(node.arguments[1])) {
context.report(node.callee.property, 'Prefer matches property syntax');
}
} catch (e) {
context.report(node, 'Error executing rule: ' + e);
}
}
};
};

module.exports.schema = [
// JSON Schema for rule options goes here
];
11 changes: 6 additions & 5 deletions lib/rules/prop-shorthand.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* @fileoverview Rule to check if the file is importing a module from another package which is forbidden
* @fileoverview Rule to check if the property shorthand can be used
*/
'use strict';

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

module.exports = function (context) {
var _ = require('lodash');
var property = ['map'];

function shouldPreferPluck(func) {
if (func.params.length) {
Expand All @@ -20,7 +21,7 @@ module.exports = function (context) {
}

function isLodashCollectionFunction(node) {
return _.get(node, 'callee.object.name') === '_' && _.get(node, 'callee.property.name') === 'map';
return _.get(node, 'callee.object.name') === '_' && _.includes(property, _.get(node, 'callee.property.name'));
}

function isLodashChainStart(node) {
Expand All @@ -41,7 +42,7 @@ module.exports = function (context) {

//}
} else if (isLodashCollectionFunction(node) && node.arguments.length > 1 && node.arguments[1].type === 'FunctionExpression' && shouldPreferPluck(node.arguments[1])) {
context.report(node.callee.property, 'Prefer pluck syntax');
context.report(node.callee.property, 'Prefer property shorthand syntax');
}
} catch (e) {
context.report(node, 'Error executing rule: ' + e);
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/matches-prop-shorthand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

var rule = require('../../../lib/rules/matches-prop-shorthand');
var RuleTester = require('eslint').RuleTester;

// require('babel-eslint');

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run('matches-prop-shorthand', rule, {
valid: [{
code: [
'var isPublic = _.find([], function (i) { return x.id; });'
].join('\n')
}, {
code: [
'var isPublic = _.map([], function (i) { return i.id + "?"; });'
].join('\n')
}],

invalid: [{
code: [
'var isPublic = _.find([], function (i) { return i.id === 3; });'
].join('\n'),
errors: [{
message: 'Prefer matches property syntax'
}]
}
]
});
4 changes: 2 additions & 2 deletions tests/lib/rules/prop-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var RuleTester = require('eslint').RuleTester;
// ------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run('preferred-alias', rule, {
ruleTester.run('prop-shorthand', rule, {

valid: [{
code: [
Expand All @@ -31,7 +31,7 @@ ruleTester.run('preferred-alias', rule, {
'var isPublic = _.map([], function (i) { return i.id; });'
].join('\n'),
errors: [{
message: "Prefer pluck syntax1"
message: 'Prefer property shorthand syntax'
}]
}
]
Expand Down

0 comments on commit 06d6706

Please sign in to comment.