From 15c0f4f54863de6690e5012ca75f4762fd5af887 Mon Sep 17 00:00:00 2001 From: Anix Date: Fri, 20 Mar 2020 11:35:50 +0000 Subject: [PATCH] Fix: allow assign exp for callback-return --- docs/rules/callback-return.md | 9 +++++++++ lib/rules/callback-return.js | 16 +++++++++++++--- tests/lib/rules/callback-return.js | 21 +++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/docs/rules/callback-return.md b/docs/rules/callback-return.md index ba27e23340d..3e8f3b17ec7 100644 --- a/docs/rules/callback-return.md +++ b/docs/rules/callback-return.md @@ -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 diff --git a/lib/rules/callback-return.js b/lib/rules/callback-return.js index c5263cde46b..1d2a9a60b13 100644 --- a/lib/rules/callback-return.js +++ b/lib/rules/callback-return.js @@ -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; @@ -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") { @@ -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") { @@ -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" }); } diff --git a/tests/lib/rules/callback-return.js b/tests/lib/rules/callback-return.js index 1f17ef6cc24..f8b2ddfffbc 100644 --- a/tests/lib/rules/callback-return.js +++ b/tests/lib/rules/callback-return.js @@ -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()",