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
39 const ALLOW_EMPTY_REJECT = context.options.length && context.options[0].allowEmptyReject;
41 //----------------------------------------------------------------------
43 //----------------------------------------------------------------------
46 * Checks the argument of a reject() or Promise.reject() CallExpression, and reports it if it can't be an Error
47 * @param {ASTNode} callExpression A CallExpression node which is used to reject a Promise
50 function checkRejectCall(callExpression) {
51 if (!callExpression.arguments.length && ALLOW_EMPTY_REJECT) {
55 !callExpression.arguments.length ||
56 !astUtils.couldBeError(callExpression.arguments[0]) ||
57 callExpression.arguments[0].type === "Identifier" && callExpression.arguments[0].name === "undefined"
61 message: "Expected the Promise rejection reason to be an Error."
67 * Determines whether a function call is a Promise.reject() call
68 * @param {ASTNode} node A CallExpression node
69 * @returns {boolean} `true` if the call is a Promise.reject() call
71 function isPromiseRejectCall(node) {
72 return node.callee.type === "MemberExpression" &&
73 node.callee.object.type === "Identifier" && node.callee.object.name === "Promise" &&
74 node.callee.property.type === "Identifier" && node.callee.property.name === "reject";
77 //----------------------------------------------------------------------
79 //----------------------------------------------------------------------
83 // Check `Promise.reject(value)` calls.
84 CallExpression(node) {
85 if (isPromiseRejectCall(node)) {
86 checkRejectCall(node);
91 * Check for `new Promise((resolve, reject) => {})`, and check for reject() calls.
92 * This function is run on "NewExpression:exit" instead of "NewExpression" to ensure that
93 * the nodes in the expression already have the `parent` property.
95 "NewExpression:exit"(node) {
97 node.callee.type === "Identifier" && node.callee.name === "Promise" &&
98 node.arguments.length && astUtils.isFunction(node.arguments[0]) &&
99 node.arguments[0].params.length > 1 && node.arguments[0].params[1].type === "Identifier"
101 context.getDeclaredVariables(node.arguments[0])
104 * Find the first variable that matches the second parameter's name.
105 * If the first parameter has the same name as the second parameter, then the variable will actually
106 * be "declared" when the first parameter is evaluated, but then it will be immediately overwritten
107 * by the second parameter. It's not possible for an expression with the variable to be evaluated before
108 * the variable is overwritten, because functions with duplicate parameters cannot have destructuring or
109 * default assignments in their parameter lists. Therefore, it's not necessary to explicitly account for
112 .find(variable => variable.name === node.arguments[0].params[1].name)
114 // Get the references to that variable.
117 // Only check the references that read the parameter's value.
118 .filter(ref => ref.isRead())
120 // Only check the references that are used as the callee in a function call, e.g. `reject(foo)`.
121 .filter(ref => ref.identifier.parent.type === "CallExpression" && ref.identifier === ref.identifier.parent.callee)
123 // Check the argument of the function call to determine whether it's an Error.
124 .forEach(ref => checkRejectCall(ref.identifier.parent));