UNPKG

5.49 kBJavaScriptView Raw
1/**
2 * @fileoverview restrict values that can be used as Promise rejection reasons
3 * @author Teddy Katz
4 */
5"use strict";
6
7const astUtils = require("../ast-utils");
8
9//------------------------------------------------------------------------------
10// Rule Definition
11//------------------------------------------------------------------------------
12
13module.exports = {
14 meta: {
15 docs: {
16 description: "require using Error objects as Promise rejection reasons",
17 category: "Best Practices",
18 recommended: false,
19 url: "https://eslint.org/docs/rules/prefer-promise-reject-errors"
20 },
21 fixable: null,
22 schema: [
23 {
24 type: "object",
25 properties: {
26 allowEmptyReject: { type: "boolean" }
27 },
28 additionalProperties: false
29 }
30 ]
31 },
32
33 create(context) {
34
35 const ALLOW_EMPTY_REJECT = context.options.length && context.options[0].allowEmptyReject;
36
37 //----------------------------------------------------------------------
38 // Helpers
39 //----------------------------------------------------------------------
40
41 /**
42 * Checks the argument of a reject() or Promise.reject() CallExpression, and reports it if it can't be an Error
43 * @param {ASTNode} callExpression A CallExpression node which is used to reject a Promise
44 * @returns {void}
45 */
46 function checkRejectCall(callExpression) {
47 if (!callExpression.arguments.length && ALLOW_EMPTY_REJECT) {
48 return;
49 }
50 if (
51 !callExpression.arguments.length ||
52 !astUtils.couldBeError(callExpression.arguments[0]) ||
53 callExpression.arguments[0].type === "Identifier" && callExpression.arguments[0].name === "undefined"
54 ) {
55 context.report({
56 node: callExpression,
57 message: "Expected the Promise rejection reason to be an Error."
58 });
59 }
60 }
61
62 /**
63 * Determines whether a function call is a Promise.reject() call
64 * @param {ASTNode} node A CallExpression node
65 * @returns {boolean} `true` if the call is a Promise.reject() call
66 */
67 function isPromiseRejectCall(node) {
68 return node.callee.type === "MemberExpression" &&
69 node.callee.object.type === "Identifier" && node.callee.object.name === "Promise" &&
70 node.callee.property.type === "Identifier" && node.callee.property.name === "reject";
71 }
72
73 //----------------------------------------------------------------------
74 // Public
75 //----------------------------------------------------------------------
76
77 return {
78
79 // Check `Promise.reject(value)` calls.
80 CallExpression(node) {
81 if (isPromiseRejectCall(node)) {
82 checkRejectCall(node);
83 }
84 },
85
86 /*
87 * Check for `new Promise((resolve, reject) => {})`, and check for reject() calls.
88 * This function is run on "NewExpression:exit" instead of "NewExpression" to ensure that
89 * the nodes in the expression already have the `parent` property.
90 */
91 "NewExpression:exit"(node) {
92 if (
93 node.callee.type === "Identifier" && node.callee.name === "Promise" &&
94 node.arguments.length && astUtils.isFunction(node.arguments[0]) &&
95 node.arguments[0].params.length > 1 && node.arguments[0].params[1].type === "Identifier"
96 ) {
97 context.getDeclaredVariables(node.arguments[0])
98
99 /*
100 * Find the first variable that matches the second parameter's name.
101 * If the first parameter has the same name as the second parameter, then the variable will actually
102 * be "declared" when the first parameter is evaluated, but then it will be immediately overwritten
103 * by the second parameter. It's not possible for an expression with the variable to be evaluated before
104 * the variable is overwritten, because functions with duplicate parameters cannot have destructuring or
105 * default assignments in their parameter lists. Therefore, it's not necessary to explicitly account for
106 * this case.
107 */
108 .find(variable => variable.name === node.arguments[0].params[1].name)
109
110 // Get the references to that variable.
111 .references
112
113 // Only check the references that read the parameter's value.
114 .filter(ref => ref.isRead())
115
116 // Only check the references that are used as the callee in a function call, e.g. `reject(foo)`.
117 .filter(ref => ref.identifier.parent.type === "CallExpression" && ref.identifier === ref.identifier.parent.callee)
118
119 // Check the argument of the function call to determine whether it's an Error.
120 .forEach(ref => checkRejectCall(ref.identifier.parent));
121 }
122 }
123 };
124 }
125};