-
Notifications
You must be signed in to change notification settings - Fork 65
/
prefer-lodash-chain.js
35 lines (31 loc) · 1.44 KB
/
prefer-lodash-chain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* @fileoverview Rule to check if there's a JS native method in the lodash chain
*/
'use strict';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function (context) {
var lodashUtil = require('../util/lodashUtil');
var astUtil = require('../util/astUtil');
var REPORT_MESSAGE = 'Prefer lodash chain';
return {
CallExpression: function (node) {
if (lodashUtil.isLodashChainStart(node)) {
do {
node = node.parent.parent;
} while (astUtil.isMethodCall(node) && !lodashUtil.isChainBreaker(node));
if (lodashUtil.isChainBreaker(node) && astUtil.isObjectOfMethodCall(node)) {
var callAfterChainBreak = node.parent.parent;
if (lodashUtil.isNativeCollectionMethodCall(callAfterChainBreak) || lodashUtil.isLodashWrapperMethod(callAfterChainBreak)) {
context.report(callAfterChainBreak, REPORT_MESSAGE);
}
}
} else if (lodashUtil.isLodashCall(node)) {
if (astUtil.isMethodCall(node.parent.parent) && (lodashUtil.isNativeCollectionMethodCall(node.parent.parent))) {
context.report(node.parent.parent, REPORT_MESSAGE);
}
}
}
};
};