Skip to content

Commit

Permalink
Update: allow object methods in callback-return (fixes eslint#4711)
Browse files Browse the repository at this point in the history
  • Loading branch information
fakewaffle committed Dec 15, 2015
1 parent db2f41c commit 6b6e524
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/rules/callback-return.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function foo() {
The rule takes a single option, which is an array of possible callback names.

```json
callback-return: [2, ["callback", "cb", "next"]]
callback-return: [2, ["callback", "cb", "next", "send.sucess"]]
```

### Gotchas
Expand Down
10 changes: 9 additions & 1 deletion lib/rules/callback-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ module.exports = function(context) {
* @returns {Boolean} Whether or not this function matches our callback name.
*/
function isCallback(node) {
return node.callee.type === "Identifier" && callbacks.indexOf(node.callee.name) > -1;
if (node.callee.type === "Identifier" && callbacks.indexOf(node.callee.name) > -1) {
return true;
}

if (node.callee.type === "MemberExpression" && callbacks.indexOf(node.callee.object.name + "." + node.callee.property.name) > -1) {
return true;
}

return false;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/callback-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ruleTester.run("callback-return", rule, {
"function x() { switch(x) { case 'a': return next(); } }",
"function x() { for(x = 0; x < 10; x++) { return next(); } }",
"function x() { while(x) { return next(); } }",
"function a(err) { if (err) { send.error (err); } }",

// callback() all you want outside of a function
"callback()",
Expand Down Expand Up @@ -234,6 +235,27 @@ ruleTester.run("callback-return", rule, {
}]
},

// object methods
{
code: "function a(err) { if (err) { send.error (err); } }",
options: [["send.error"]],
errors: [{
message: "Expected return with your callback function.",
line: 1,
column: 30,
nodeType: "CallExpression"
}]
},
{
code: "function a(err) { if (err) { send.error (err); } send.success(); }",
options: [["send.error", "send.success"]],
errors: [{
message: "Expected return with your callback function.",
line: 1,
column: 30,
nodeType: "CallExpression"
}]
},

// generally good behavior which we must not allow to keep the rule simple
{
Expand Down

0 comments on commit 6b6e524

Please sign in to comment.