Skip to content

Commit

Permalink
Fix: allow assign exp for callback-return
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Mar 20, 2020
1 parent dbe357d commit 15c0f4f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
9 changes: 9 additions & 0 deletions docs/rules/callback-return.md
Expand Up @@ -52,6 +52,15 @@ function foo(err, callback) {
}
callback();
}


function foo(err, cb){
var bar = cb()
}

function fn(obj, next){
obj = next()
}
```

### Supplied callback names
Expand Down
16 changes: 13 additions & 3 deletions lib/rules/callback-return.js
Expand Up @@ -48,7 +48,7 @@ module.exports = {
if (!node.parent) {
return null;
}
if (types.indexOf(node.parent.type) === -1) {
if (!~types.indexOf(node.parent.type)) {
return findClosestParentOfType(node.parent, types);
}
return node.parent;
Expand Down Expand Up @@ -126,7 +126,7 @@ module.exports = {
}

// find the closest block, return or loop
const closestBlock = findClosestParentOfType(node, ["BlockStatement", "ReturnStatement", "ArrowFunctionExpression"]) || {};
const closestBlock = findClosestParentOfType(node, ["BlockStatement", "ReturnStatement", "ArrowFunctionExpression", "AssignmentExpression", "VariableDeclaration"]) || {};

// if our parent is a return we know we're ok
if (closestBlock.type === "ReturnStatement") {
Expand All @@ -138,6 +138,16 @@ module.exports = {
return;
}

// if its assignmentExpression like foo = callback(), it should not report
if (closestBlock.type === "AssignmentExpression") {
return;
}

// if its VariableDeclaration like const foo = callback(), it should not report
if (closestBlock.type === "VariableDeclaration") {
return;
}

// block statements are part of functions and most if statements
if (closestBlock.type === "BlockStatement") {

Expand Down Expand Up @@ -171,7 +181,7 @@ module.exports = {
}

// as long as you're the child of a function at this point you should be asked to return
if (findClosestParentOfType(node, ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"])) {
if (findClosestParentOfType(node, ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression", "AssignmentExpression", "VariableDeclaration"])) {
context.report({ node, messageId: "missingReturn" });
}

Expand Down
21 changes: 21 additions & 0 deletions tests/lib/rules/callback-return.js
Expand Up @@ -38,6 +38,27 @@ ruleTester.run("callback-return", rule, {
"function x() { for(x = 0; x < 10; x++) { return next(); } }",
"function x() { while(x) { return next(); } }",
"function a(err) { if (err) { obj.method (err); } }",
"var fn = function(cb) { var bar = cb() }",
{
code: "function a(err, callback) { const foo = callback() }",
parserOptions: { ecmaVersion: 6 }
},
{
code: "const fn = (next) => bar = next() ",
parserOptions: { ecmaVersion: 6 }
},
{
code: `const mutate = (obj, key, callback) => {
obj[key] = callback(obj[key]);
};`,
parserOptions: { ecmaVersion: 6 }
},
{
code: `const mutate = (callback) => {
var foo = callback();
};`,
parserOptions: { ecmaVersion: 6 }
},

// callback() all you want outside of a function
"callback()",
Expand Down

0 comments on commit 15c0f4f

Please sign in to comment.