UNPKG

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