]> git.proxmox.com Git - pve-eslint.git/blame - eslint/lib/rules/no-promise-executor-return.js
import 8.41.0 source
[pve-eslint.git] / eslint / lib / rules / no-promise-executor-return.js
CommitLineData
6f036462
TL
1/**
2 * @fileoverview Rule to disallow returning values from Promise executor functions
3 * @author Milos Djermanovic
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
f2a92ac6 12const { findVariable } = require("@eslint-community/eslint-utils");
6f036462
TL
13
14//------------------------------------------------------------------------------
15// Helpers
16//------------------------------------------------------------------------------
17
18const functionTypesToCheck = new Set(["ArrowFunctionExpression", "FunctionExpression"]);
19
20/**
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.
25 */
26function isGlobalReference(node, scope) {
27 const variable = findVariable(scope, node);
28
29 return variable !== null && variable.scope.type === "global" && variable.defs.length === 0;
30}
31
32/**
33 * Finds function's outer scope.
34 * @param {Scope} scope Function's own scope.
35 * @returns {Scope} Function's outer scope.
36 */
37function getOuterScope(scope) {
38 const upper = scope.upper;
39
40 if (upper.type === "function-expression-name") {
41 return upper.upper;
42 }
43 return upper;
44}
45
46/**
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.
51 */
52function isPromiseExecutor(node, scope) {
53 const parent = node.parent;
54
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));
60}
61
62//------------------------------------------------------------------------------
63// Rule Definition
64//------------------------------------------------------------------------------
65
34eeec05 66/** @type {import('../shared/types').Rule} */
6f036462
TL
67module.exports = {
68 meta: {
69 type: "problem",
70
71 docs: {
8f9d1d4d 72 description: "Disallow returning values from Promise executor functions",
6f036462 73 recommended: false,
f2a92ac6 74 url: "https://eslint.org/docs/latest/rules/no-promise-executor-return"
6f036462
TL
75 },
76
77 schema: [],
78
79 messages: {
80 returnsValue: "Return values from promise executor functions cannot be read."
81 }
82 },
83
84 create(context) {
85
86 let funcInfo = null;
f2a92ac6 87 const sourceCode = context.sourceCode;
6f036462
TL
88
89 /**
90 * Reports the given node.
91 * @param {ASTNode} node Node to report.
92 * @returns {void}
93 */
94 function report(node) {
95 context.report({ node, messageId: "returnsValue" });
96 }
97
98 return {
99
100 onCodePathStart(_, node) {
101 funcInfo = {
102 upper: funcInfo,
f2a92ac6 103 shouldCheck: functionTypesToCheck.has(node.type) && isPromiseExecutor(node, sourceCode.getScope(node))
6f036462
TL
104 };
105
106 if (funcInfo.shouldCheck && node.type === "ArrowFunctionExpression" && node.expression) {
107 report(node.body);
108 }
109 },
110
111 onCodePathEnd() {
112 funcInfo = funcInfo.upper;
113 },
114
115 ReturnStatement(node) {
116 if (funcInfo.shouldCheck && node.argument) {
117 report(node);
118 }
119 }
120 };
121 }
122};