-
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.
- Loading branch information
ido
committed
Aug 13, 2015
1 parent
f9d59a4
commit 06d6706
Showing
8 changed files
with
132 additions
and
12 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
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,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. |
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,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 | ||
]; |
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,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' | ||
}] | ||
} | ||
] | ||
}); |
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