]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/callback-return.js
532faee45187ca005cbf22f9295f8a9e66301260
2 * @fileoverview Enforce return after a callback.
3 * @author Jamund Ferguson
4 * @deprecated in ESLint v7.0.0
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 /** @type {import('../shared/types').Rule} */
22 description
: "require `return` statements after callbacks",
24 url
: "https://eslint.org/docs/rules/callback-return"
29 items
: { type
: "string" }
33 missingReturn
: "Expected return with your callback function."
39 const callbacks
= context
.options
[0] || ["callback", "cb", "next"],
40 sourceCode
= context
.getSourceCode();
42 //--------------------------------------------------------------------------
44 //--------------------------------------------------------------------------
47 * Find the closest parent matching a list of types.
48 * @param {ASTNode} node The node whose parents we are searching
49 * @param {Array} types The node types to match
50 * @returns {ASTNode} The matched node or undefined.
52 function findClosestParentOfType(node
, types
) {
56 if (types
.indexOf(node
.parent
.type
) === -1) {
57 return findClosestParentOfType(node
.parent
, types
);
63 * Check to see if a node contains only identifiers
64 * @param {ASTNode} node The node to check
65 * @returns {boolean} Whether or not the node contains only identifiers
67 function containsOnlyIdentifiers(node
) {
68 if (node
.type
=== "Identifier") {
72 if (node
.type
=== "MemberExpression") {
73 if (node
.object
.type
=== "Identifier") {
76 if (node
.object
.type
=== "MemberExpression") {
77 return containsOnlyIdentifiers(node
.object
);
85 * Check to see if a CallExpression is in our callback list.
86 * @param {ASTNode} node The node to check against our callback names list.
87 * @returns {boolean} Whether or not this function matches our callback name.
89 function isCallback(node
) {
90 return containsOnlyIdentifiers(node
.callee
) && callbacks
.indexOf(sourceCode
.getText(node
.callee
)) > -1;
94 * Determines whether or not the callback is part of a callback expression.
95 * @param {ASTNode} node The callback node
96 * @param {ASTNode} parentNode The expression node
97 * @returns {boolean} Whether or not this is part of a callback expression
99 function isCallbackExpression(node
, parentNode
) {
101 // ensure the parent node exists and is an expression
102 if (!parentNode
|| parentNode
.type
!== "ExpressionStatement") {
107 if (parentNode
.expression
=== node
) {
111 // special case for cb && cb() and similar
112 if (parentNode
.expression
.type
=== "BinaryExpression" || parentNode
.expression
.type
=== "LogicalExpression") {
113 if (parentNode
.expression
.right
=== node
) {
121 //--------------------------------------------------------------------------
123 //--------------------------------------------------------------------------
126 CallExpression(node
) {
128 // if we're not a callback we can return
129 if (!isCallback(node
)) {
133 // find the closest block, return or loop
134 const closestBlock
= findClosestParentOfType(node
, ["BlockStatement", "ReturnStatement", "ArrowFunctionExpression"]) || {};
136 // if our parent is a return we know we're ok
137 if (closestBlock
.type
=== "ReturnStatement") {
141 // arrow functions don't always have blocks and implicitly return
142 if (closestBlock
.type
=== "ArrowFunctionExpression") {
146 // block statements are part of functions and most if statements
147 if (closestBlock
.type
=== "BlockStatement") {
149 // find the last item in the block
150 const lastItem
= closestBlock
.body
[closestBlock
.body
.length
- 1];
152 // if the callback is the last thing in a block that might be ok
153 if (isCallbackExpression(node
, lastItem
)) {
155 const parentType
= closestBlock
.parent
.type
;
157 // but only if the block is part of a function
158 if (parentType
=== "FunctionExpression" ||
159 parentType
=== "FunctionDeclaration" ||
160 parentType
=== "ArrowFunctionExpression"
167 // ending a block with a return is also ok
168 if (lastItem
.type
=== "ReturnStatement") {
170 // but only if the callback is immediately before
171 if (isCallbackExpression(node
, closestBlock
.body
[closestBlock
.body
.length
- 2])) {
178 // as long as you're the child of a function at this point you should be asked to return
179 if (findClosestParentOfType(node
, ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"])) {
180 context
.report({ node
, messageId
: "missingReturn" });