diff --git a/rules/always-return.js b/rules/always-return.js index 549634f1..cfc41646 100644 --- a/rules/always-return.js +++ b/rules/always-return.js @@ -8,7 +8,7 @@ function isFunctionWithBlockStatement (node) { return false } -function isThenExpression (node) { +function isThenCallExpression (node) { return ( node.type === 'CallExpression' && node.callee.type === 'MemberExpression' && @@ -16,10 +16,19 @@ function isThenExpression (node) { ) } +function isFirstArgument (node) { + return ( + node.parent && + node.parent.arguments && + node.parent.arguments[0] === node + ) +} + function isInlineThenFunctionExpression (node) { return ( isFunctionWithBlockStatement(node) && - isThenExpression(node.parent) + isThenCallExpression(node.parent) && + isFirstArgument(node) ) } diff --git a/test/always-return.test.js b/test/always-return.test.js index dcebd818..e89b281b 100644 --- a/test/always-return.test.js +++ b/test/always-return.test.js @@ -23,7 +23,8 @@ ruleTester.run('always-return', rule, { { code: 'hey.then(x => { if (x) { return x; } throw new Error("no x"); })', parserOptions: parserOptions }, { code: 'hey.then(x => { var f = function() { }; return f; })', parserOptions: parserOptions }, { code: 'hey.then(x => { if (x) { return x; } else { return x; } })', parserOptions: parserOptions }, - { code: 'hey.then(x => { return x; var y = "unreachable"; })', parserOptions: parserOptions } + { code: 'hey.then(x => { return x; var y = "unreachable"; })', parserOptions: parserOptions }, + { code: 'hey.then(x => { return; }, err=>{ log(err); })', parserOptions: parserOptions } ], invalid: [