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