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