UNPKG

3.9 kBJavaScriptView Raw
1/**
2 * @fileoverview Disallows unnecessary `return await`
3 * @author Jordan Harband
4 */
5"use strict";
6
7const astUtils = require("./utils/ast-utils");
8
9//------------------------------------------------------------------------------
10// Rule Definition
11//------------------------------------------------------------------------------
12
13module.exports = {
14 meta: {
15 type: "suggestion",
16
17 docs: {
18 description: "disallow unnecessary `return await`",
19 category: "Best Practices",
20
21 recommended: false,
22
23 url: "https://eslint.org/docs/rules/no-return-await"
24 },
25
26 fixable: null,
27
28 schema: [
29 ],
30
31 messages: {
32 redundantUseOfAwait: "Redundant use of `await` on a return value."
33 }
34 },
35
36 create(context) {
37
38 /**
39 * Reports a found unnecessary `await` expression.
40 * @param {ASTNode} node The node representing the `await` expression to report
41 * @returns {void}
42 */
43 function reportUnnecessaryAwait(node) {
44 context.report({
45 node: context.getSourceCode().getFirstToken(node),
46 loc: node.loc,
47 messageId: "redundantUseOfAwait"
48 });
49 }
50
51 /**
52 * Determines whether a thrown error from this node will be caught/handled within this function rather than immediately halting
53 * this function. For example, a statement in a `try` block will always have an error handler. A statement in
54 * a `catch` block will only have an error handler if there is also a `finally` block.
55 * @param {ASTNode} node A node representing a location where an could be thrown
56 * @returns {boolean} `true` if a thrown error will be caught/handled in this function
57 */
58 function hasErrorHandler(node) {
59 let ancestor = node;
60
61 while (!astUtils.isFunction(ancestor) && ancestor.type !== "Program") {
62 if (ancestor.parent.type === "TryStatement" && (ancestor === ancestor.parent.block || ancestor === ancestor.parent.handler && ancestor.parent.finalizer)) {
63 return true;
64 }
65 ancestor = ancestor.parent;
66 }
67 return false;
68 }
69
70 /**
71 * Checks if a node is placed in tail call position. Once `return` arguments (or arrow function expressions) can be a complex expression,
72 * an `await` expression could or could not be unnecessary by the definition of this rule. So we're looking for `await` expressions that are in tail position.
73 * @param {ASTNode} node A node representing the `await` expression to check
74 * @returns {boolean} The checking result
75 */
76 function isInTailCallPosition(node) {
77 if (node.parent.type === "ArrowFunctionExpression") {
78 return true;
79 }
80 if (node.parent.type === "ReturnStatement") {
81 return !hasErrorHandler(node.parent);
82 }
83 if (node.parent.type === "ConditionalExpression" && (node === node.parent.consequent || node === node.parent.alternate)) {
84 return isInTailCallPosition(node.parent);
85 }
86 if (node.parent.type === "LogicalExpression" && node === node.parent.right) {
87 return isInTailCallPosition(node.parent);
88 }
89 if (node.parent.type === "SequenceExpression" && node === node.parent.expressions[node.parent.expressions.length - 1]) {
90 return isInTailCallPosition(node.parent);
91 }
92 return false;
93 }
94
95 return {
96 AwaitExpression(node) {
97 if (isInTailCallPosition(node) && !hasErrorHandler(node)) {
98 reportUnnecessaryAwait(node);
99 }
100 }
101 };
102 }
103};