]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/prefer-promise-reject-errors.js
56911b67adcc998461b5f4fff51b84382a566a95
2 * @fileoverview restrict values that can be used as Promise rejection reasons
7 const astUtils
= require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description
: "require using Error objects as Promise rejection reasons",
19 category
: "Best Practices",
21 url
: "https://eslint.org/docs/rules/prefer-promise-reject-errors"
30 allowEmptyReject
: { type
: "boolean", default: false }
32 additionalProperties
: false
37 rejectAnError
: "Expected the Promise rejection reason to be an Error."
43 const ALLOW_EMPTY_REJECT
= context
.options
.length
&& context
.options
[0].allowEmptyReject
;
45 //----------------------------------------------------------------------
47 //----------------------------------------------------------------------
50 * Checks the argument of a reject() or Promise.reject() CallExpression, and reports it if it can't be an Error
51 * @param {ASTNode} callExpression A CallExpression node which is used to reject a Promise
54 function checkRejectCall(callExpression
) {
55 if (!callExpression
.arguments
.length
&& ALLOW_EMPTY_REJECT
) {
59 !callExpression
.arguments
.length
||
60 !astUtils
.couldBeError(callExpression
.arguments
[0]) ||
61 callExpression
.arguments
[0].type
=== "Identifier" && callExpression
.arguments
[0].name
=== "undefined"
65 messageId
: "rejectAnError"
71 * Determines whether a function call is a Promise.reject() call
72 * @param {ASTNode} node A CallExpression node
73 * @returns {boolean} `true` if the call is a Promise.reject() call
75 function isPromiseRejectCall(node
) {
76 return node
.callee
.type
=== "MemberExpression" &&
77 node
.callee
.object
.type
=== "Identifier" && node
.callee
.object
.name
=== "Promise" &&
78 node
.callee
.property
.type
=== "Identifier" && node
.callee
.property
.name
=== "reject";
81 //----------------------------------------------------------------------
83 //----------------------------------------------------------------------
87 // Check `Promise.reject(value)` calls.
88 CallExpression(node
) {
89 if (isPromiseRejectCall(node
)) {
90 checkRejectCall(node
);
95 * Check for `new Promise((resolve, reject) => {})`, and check for reject() calls.
96 * This function is run on "NewExpression:exit" instead of "NewExpression" to ensure that
97 * the nodes in the expression already have the `parent` property.
99 "NewExpression:exit"(node
) {
101 node
.callee
.type
=== "Identifier" && node
.callee
.name
=== "Promise" &&
102 node
.arguments
.length
&& astUtils
.isFunction(node
.arguments
[0]) &&
103 node
.arguments
[0].params
.length
> 1 && node
.arguments
[0].params
[1].type
=== "Identifier"
105 context
.getDeclaredVariables(node
.arguments
[0])
108 * Find the first variable that matches the second parameter's name.
109 * If the first parameter has the same name as the second parameter, then the variable will actually
110 * be "declared" when the first parameter is evaluated, but then it will be immediately overwritten
111 * by the second parameter. It's not possible for an expression with the variable to be evaluated before
112 * the variable is overwritten, because functions with duplicate parameters cannot have destructuring or
113 * default assignments in their parameter lists. Therefore, it's not necessary to explicitly account for
116 .find(variable
=> variable
.name
=== node
.arguments
[0].params
[1].name
)
118 // Get the references to that variable.
121 // Only check the references that read the parameter's value.
122 .filter(ref
=> ref
.isRead())
124 // Only check the references that are used as the callee in a function call, e.g. `reject(foo)`.
125 .filter(ref
=> ref
.identifier
.parent
.type
=== "CallExpression" && ref
.identifier
=== ref
.identifier
.parent
.callee
)
127 // Check the argument of the function call to determine whether it's an Error.
128 .forEach(ref
=> checkRejectCall(ref
.identifier
.parent
));