Skip to content

Commit

Permalink
performance: replace optimistic isEndOfChain with running from start …
Browse files Browse the repository at this point in the history
…of chain
  • Loading branch information
ganimomer committed Jan 19, 2016
1 parent 841bb79 commit 9abe03e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 7 additions & 2 deletions lib/rules/no-commit.js
Expand Up @@ -14,8 +14,13 @@ module.exports = function (context) {

return {
CallExpression: function (node) {
if (lodashUtil.isEndOfChain(node) && lodashUtil.isCallToMethod(node, 'commit') && !lodashUtil.isCallToMethod(astUtil.getCaller(node), 'forEach')) {
context.report(node, 'Do not end chain with commit, except for side effects.');
if (lodashUtil.isLodashChainStart(node)) {
do {
node = node.parent.parent;
} while (astUtil.isMethodCall(node) && !lodashUtil.isCallToMethod(node, 'commit'));
if (lodashUtil.isCallToMethod(node, 'commit') && !lodashUtil.isCallToMethod(astUtil.getCaller(node), 'forEach')) {
context.report(node, 'Do not end chain with commit, except for side effects.');
}
}
}
};
Expand Down
15 changes: 9 additions & 6 deletions lib/rules/no-single-chain.js
Expand Up @@ -11,18 +11,21 @@ module.exports = function (context) {
var lodashUtil = require('../util/lodashUtil');
var astUtil = require('../util/astUtil');

function isCalledByChainStart(node) {
return lodashUtil.isLodashChainStart(astUtil.getCaller(node));
function isEndOfChain(node) {
return !astUtil.isObjectOfMethodCall(node);
}

function isChainBreakerAfterSingleMethod(node) {
return lodashUtil.isChainBreaker(node) && isCalledByChainStart(astUtil.getCaller(node));
function isBeforeChainBreaker(node) {
return lodashUtil.isChainBreaker(node.parent.parent);
}

return {
CallExpression: function (node) {
if (lodashUtil.isEndOfChain(node) && (isCalledByChainStart(node) || isChainBreakerAfterSingleMethod(node))) {
context.report(node.callee.property, 'Do not use chain syntax for single method');
if (lodashUtil.isLodashChainStart(node)) {
var firstCall = node.parent.parent;
if (astUtil.isMethodCall(firstCall) && (isEndOfChain(firstCall) || isBeforeChainBreaker(firstCall))) {
context.report(firstCall, 'Do not use chain syntax for single method');
}
}
}
};
Expand Down

0 comments on commit 9abe03e

Please sign in to comment.