forked from wix-incubator/eslint-plugin-lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
137 additions
and
1 deletion.
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,36 @@ | ||
# Prefer get | ||
|
||
When writing an expression like `a && a.b && a.b.c` just to make sure the path exists, it is more readable to use the functions `_.get`, `_.set` and `_.has` instead. | ||
|
||
## Rule Details | ||
|
||
This rule takes one argument - the minimal depth (default is 3). | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
var isThree = a && a.b && a.b.c === 3; | ||
|
||
if (a && a.b && a.b.c) { | ||
// ... | ||
} | ||
|
||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
|
||
var isThree = _.get(a, 'b.c') === 3; | ||
|
||
if (_.has(a, 'b.c')) { | ||
// ... | ||
} | ||
|
||
``` | ||
|
||
|
||
## When Not To Use It | ||
|
||
If you do not want to enforce using `get`, you should not use 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,43 @@ | ||
/** | ||
* @fileoverview Rule to check if an "&&" experssion should be a call to _.get or _.has | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var DEFAULT_LENGTH = 3; | ||
var astUtil = require('../util/astUtil'); | ||
var ruleDepth = Math.max(parseInt(context.options[0], 10) || DEFAULT_LENGTH, 2); | ||
|
||
var expStates = []; | ||
function getState() { | ||
return expStates[expStates.length - 1] || {depth: 0}; | ||
} | ||
|
||
function shouldCheckDeeper(node, nodeRight, toCompare) { | ||
return node.operator === '&&' && nodeRight && (!toCompare || astUtil.isEquivalentExp(nodeRight, toCompare)); | ||
} | ||
|
||
return { | ||
LogicalExpression: function (node) { | ||
var state = getState(); | ||
var rightMemberExp = (node.right.type === 'MemberExpression' || state.depth > 0) ? node.right : node.right.left; | ||
|
||
if (shouldCheckDeeper(node, rightMemberExp, state.node)) { | ||
expStates.push({depth: state.depth + 1, node: rightMemberExp.object}); | ||
if (astUtil.isEquivalentExp(node.left, rightMemberExp.object) && state.depth >= ruleDepth - 2) { | ||
context.report(node, "Prefer _.get or _.has over an '&&' chain"); | ||
} | ||
} | ||
}, | ||
'LogicalExpression:exit': function (node) { | ||
var state = getState(); | ||
if (state && state.node === node.right.object) { | ||
expStates.pop(); | ||
} | ||
} | ||
}; | ||
}; |
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,41 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/prefer-get'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
var errors = [{message: 'Prefer _.get or _.has over an \'&&\' chain'}]; | ||
ruleTester.run('prefer-get', rule, { | ||
valid: [{ | ||
code: 'var x = _.get(a, "b.c");' | ||
}, { | ||
code: 'var x = _.has(a, "b.c");' | ||
}, { | ||
code: 'var x = a && a.b' | ||
}, { | ||
code: 'a && a.b && f()' | ||
}], | ||
invalid: [{ | ||
code: 'x = a && a.b && a.b.c === 8', | ||
errors: errors | ||
}, { | ||
code: 'x = a && a.b && a["b"].c && a.b.c.d', | ||
errors: errors | ||
}, { | ||
code: 'x = a && a.b', | ||
errors: errors, | ||
options: [2] | ||
}, { | ||
code: 'x = a && a.b && a.b.c && a.b.c.d', | ||
errors: errors, | ||
options: [2] | ||
}] | ||
}); |