]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-promise-executor-return.js
2 * @fileoverview Rule to disallow returning values from Promise executor functions
3 * @author Milos Djermanovic
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const { findVariable
} = require("eslint-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const functionTypesToCheck
= new Set(["ArrowFunctionExpression", "FunctionExpression"]);
21 * Determines whether the given identifier node is a reference to a global variable.
22 * @param {ASTNode} node `Identifier` node to check.
23 * @param {Scope} scope Scope to which the node belongs.
24 * @returns {boolean} True if the identifier is a reference to a global variable.
26 function isGlobalReference(node
, scope
) {
27 const variable
= findVariable(scope
, node
);
29 return variable
!== null && variable
.scope
.type
=== "global" && variable
.defs
.length
=== 0;
33 * Finds function's outer scope.
34 * @param {Scope} scope Function's own scope.
35 * @returns {Scope} Function's outer scope.
37 function getOuterScope(scope
) {
38 const upper
= scope
.upper
;
40 if (upper
.type
=== "function-expression-name") {
47 * Determines whether the given function node is used as a Promise executor.
48 * @param {ASTNode} node The node to check.
49 * @param {Scope} scope Function's own scope.
50 * @returns {boolean} `true` if the node is a Promise executor.
52 function isPromiseExecutor(node
, scope
) {
53 const parent
= node
.parent
;
55 return parent
.type
=== "NewExpression" &&
56 parent
.arguments
[0] === node
&&
57 parent
.callee
.type
=== "Identifier" &&
58 parent
.callee
.name
=== "Promise" &&
59 isGlobalReference(parent
.callee
, getOuterScope(scope
));
62 //------------------------------------------------------------------------------
64 //------------------------------------------------------------------------------
71 description
: "disallow returning values from Promise executor functions",
72 category
: "Possible Errors",
74 url
: "https://eslint.org/docs/rules/no-promise-executor-return"
80 returnsValue
: "Return values from promise executor functions cannot be read."
89 * Reports the given node.
90 * @param {ASTNode} node Node to report.
93 function report(node
) {
94 context
.report({ node
, messageId
: "returnsValue" });
99 onCodePathStart(_
, node
) {
102 shouldCheck
: functionTypesToCheck
.has(node
.type
) && isPromiseExecutor(node
, context
.getScope())
105 if (funcInfo
.shouldCheck
&& node
.type
=== "ArrowFunctionExpression" && node
.expression
) {
111 funcInfo
= funcInfo
.upper
;
114 ReturnStatement(node
) {
115 if (funcInfo
.shouldCheck
&& node
.argument
) {