]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/callback-return.js
2 * @fileoverview Enforce return after a callback.
3 * @author Jamund Ferguson
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
20 description
: "require `return` statements after callbacks",
21 category
: "Node.js and CommonJS",
23 url
: "https://eslint.org/docs/rules/callback-return"
28 items
: { type
: "string" }
32 missingReturn
: "Expected return with your callback function."
38 const callbacks
= context
.options
[0] || ["callback", "cb", "next"],
39 sourceCode
= context
.getSourceCode();
41 //--------------------------------------------------------------------------
43 //--------------------------------------------------------------------------
46 * Find the closest parent matching a list of types.
47 * @param {ASTNode} node The node whose parents we are searching
48 * @param {Array} types The node types to match
49 * @returns {ASTNode} The matched node or undefined.
51 function findClosestParentOfType(node
, types
) {
55 if (types
.indexOf(node
.parent
.type
) === -1) {
56 return findClosestParentOfType(node
.parent
, types
);
62 * Check to see if a node contains only identifiers
63 * @param {ASTNode} node The node to check
64 * @returns {boolean} Whether or not the node contains only identifiers
66 function containsOnlyIdentifiers(node
) {
67 if (node
.type
=== "Identifier") {
71 if (node
.type
=== "MemberExpression") {
72 if (node
.object
.type
=== "Identifier") {
75 if (node
.object
.type
=== "MemberExpression") {
76 return containsOnlyIdentifiers(node
.object
);
84 * Check to see if a CallExpression is in our callback list.
85 * @param {ASTNode} node The node to check against our callback names list.
86 * @returns {boolean} Whether or not this function matches our callback name.
88 function isCallback(node
) {
89 return containsOnlyIdentifiers(node
.callee
) && callbacks
.indexOf(sourceCode
.getText(node
.callee
)) > -1;
93 * Determines whether or not the callback is part of a callback expression.
94 * @param {ASTNode} node The callback node
95 * @param {ASTNode} parentNode The expression node
96 * @returns {boolean} Whether or not this is part of a callback expression
98 function isCallbackExpression(node
, parentNode
) {
100 // ensure the parent node exists and is an expression
101 if (!parentNode
|| parentNode
.type
!== "ExpressionStatement") {
106 if (parentNode
.expression
=== node
) {
110 // special case for cb && cb() and similar
111 if (parentNode
.expression
.type
=== "BinaryExpression" || parentNode
.expression
.type
=== "LogicalExpression") {
112 if (parentNode
.expression
.right
=== node
) {
120 //--------------------------------------------------------------------------
122 //--------------------------------------------------------------------------
125 CallExpression(node
) {
127 // if we're not a callback we can return
128 if (!isCallback(node
)) {
132 // find the closest block, return or loop
133 const closestBlock
= findClosestParentOfType(node
, ["BlockStatement", "ReturnStatement", "ArrowFunctionExpression"]) || {};
135 // if our parent is a return we know we're ok
136 if (closestBlock
.type
=== "ReturnStatement") {
140 // arrow functions don't always have blocks and implicitly return
141 if (closestBlock
.type
=== "ArrowFunctionExpression") {
145 // block statements are part of functions and most if statements
146 if (closestBlock
.type
=== "BlockStatement") {
148 // find the last item in the block
149 const lastItem
= closestBlock
.body
[closestBlock
.body
.length
- 1];
151 // if the callback is the last thing in a block that might be ok
152 if (isCallbackExpression(node
, lastItem
)) {
154 const parentType
= closestBlock
.parent
.type
;
156 // but only if the block is part of a function
157 if (parentType
=== "FunctionExpression" ||
158 parentType
=== "FunctionDeclaration" ||
159 parentType
=== "ArrowFunctionExpression"
166 // ending a block with a return is also ok
167 if (lastItem
.type
=== "ReturnStatement") {
169 // but only if the callback is immediately before
170 if (isCallbackExpression(node
, closestBlock
.body
[closestBlock
.body
.length
- 2])) {
177 // as long as you're the child of a function at this point you should be asked to return
178 if (findClosestParentOfType(node
, ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"])) {
179 context
.report({ node
, messageId
: "missingReturn" });