Skip to content

Commit

Permalink
performance: replace optimistic isLodashWrapper with running from sta…
Browse files Browse the repository at this point in the history
…rt of chain
  • Loading branch information
ganimomer committed Jan 19, 2016
1 parent 9abe03e commit 9bca10c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
12 changes: 8 additions & 4 deletions lib/rules/no-double-unwrap.js
Expand Up @@ -12,10 +12,14 @@ module.exports = function (context) {
var astUtil = require('../util/astUtil');
return {
CallExpression: function (node) {
var caller = astUtil.getCaller(node);
if (lodashUtil.isChainBreaker(node) && !lodashUtil.isChainable(caller) && !lodashUtil.isExplicitMethodChaining(caller) &&
lodashUtil.isLodashWrapper(astUtil.getCaller(caller))) {
context.report(node, 'Do not use .value() after chain-ending method {{method}}', {method: astUtil.getMethodName(caller)});
if (lodashUtil.isImplicitChainStart(node)) {
do {
node = node.parent.parent;
} while (astUtil.isMethodCall(node) && !lodashUtil.isChainBreaker(node));
var caller = astUtil.getCaller(node);
if (astUtil.isMethodCall(node) && !lodashUtil.isChainable(caller)) {
context.report(node, 'Do not use .value() after chain-ending method {{method}}', {method: astUtil.getMethodName(caller)});
}
}
}
};
Expand Down
19 changes: 13 additions & 6 deletions lib/rules/prefer-lodash-chain.js
Expand Up @@ -15,12 +15,19 @@ module.exports = function (context) {

return {
CallExpression: function (node) {
var isLodashWrapperMethod = lodashUtil.isLodashWrapperMethod(node);
if (!lodashUtil.isLodashCall(node) && (lodashUtil.isNativeCollectionMethodCall(node) || isLodashWrapperMethod)) {
var caller = astUtil.getCaller(node);
if (lodashUtil.isLodashCall(caller) && astUtil.getMethodName(caller) !== 'chain' && !isLodashWrapperMethod ||
lodashUtil.isLodashWrapper(astUtil.getCaller(caller)) && lodashUtil.isChainBreaker(caller)) {
context.report(node, REPORT_MESSAGE, {method: astUtil.getMethodName(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);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/rules/prefer-lodash-chain.js
Expand Up @@ -21,7 +21,8 @@ ruleTester.run('prefer-lodash-chain', rule, {
'var userNames = users.map(cb1).filter(cb2);',
'var userNames = _(users).map(cb1).reduce(cb2).map(cb3);',
'var userNames = _(users).map("name.givenName").join(" ");',
'var userNames = _.map(users, "name.givenName").join(" ");'
'var userNames = _.map(users, "name.givenName").join(" ");',
'var userNames = _(users).filter("active").map("name.givenName").value().toString();'
],
invalid: [
'var userNames = _.filter(users, cb1).map(cb2);',
Expand Down

0 comments on commit 9bca10c

Please sign in to comment.