]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/prefer-promise-reject-errors.js
2 * @fileoverview restrict values that can be used as Promise rejection reasons
7 const astUtils
= require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 /** @type {import('../shared/types').Rule} */
19 description
: "require using Error objects as Promise rejection reasons",
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 astUtils
.isSpecificMemberAccess(node
.callee
, "Promise", "reject");
79 //----------------------------------------------------------------------
81 //----------------------------------------------------------------------
85 // Check `Promise.reject(value)` calls.
86 CallExpression(node
) {
87 if (isPromiseRejectCall(node
)) {
88 checkRejectCall(node
);
93 * Check for `new Promise((resolve, reject) => {})`, and check for reject() calls.
94 * This function is run on "NewExpression:exit" instead of "NewExpression" to ensure that
95 * the nodes in the expression already have the `parent` property.
97 "NewExpression:exit"(node
) {
99 node
.callee
.type
=== "Identifier" && node
.callee
.name
=== "Promise" &&
100 node
.arguments
.length
&& astUtils
.isFunction(node
.arguments
[0]) &&
101 node
.arguments
[0].params
.length
> 1 && node
.arguments
[0].params
[1].type
=== "Identifier"
103 context
.getDeclaredVariables(node
.arguments
[0])
106 * Find the first variable that matches the second parameter's name.
107 * If the first parameter has the same name as the second parameter, then the variable will actually
108 * be "declared" when the first parameter is evaluated, but then it will be immediately overwritten
109 * by the second parameter. It's not possible for an expression with the variable to be evaluated before
110 * the variable is overwritten, because functions with duplicate parameters cannot have destructuring or
111 * default assignments in their parameter lists. Therefore, it's not necessary to explicitly account for
114 .find(variable
=> variable
.name
=== node
.arguments
[0].params
[1].name
)
116 // Get the references to that variable.
119 // Only check the references that read the parameter's value.
120 .filter(ref
=> ref
.isRead())
122 // Only check the references that are used as the callee in a function call, e.g. `reject(foo)`.
123 .filter(ref
=> ref
.identifier
.parent
.type
=== "CallExpression" && ref
.identifier
=== ref
.identifier
.parent
.callee
)
125 // Check the argument of the function call to determine whether it's an Error.
126 .forEach(ref
=> checkRejectCall(ref
.identifier
.parent
));