]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-return-await.js
d1d89826856dc48d93a6beed10ae5ef40df8d1b6
2 * @fileoverview Disallows unnecessary `return await`
3 * @author Jordan Harband
7 const astUtils
= require ( "./utils/ast-utils" );
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description
: "disallow unnecessary `return await`" ,
19 category
: "Best Practices" ,
23 url
: "https://eslint.org/docs/rules/no-return-await"
32 redundantUseOfAwait
: "Redundant use of `await` on a return value."
39 * Reports a found unnecessary `await` expression.
40 * @param {ASTNode} node The node representing the `await` expression to report
43 function reportUnnecessaryAwait ( node
) {
45 node
: context
. getSourceCode (). getFirstToken ( node
),
47 messageId
: "redundantUseOfAwait"
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
58 function hasErrorHandler ( node
) {
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
)) {
65 ancestor
= ancestor
. parent
;
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
76 function isInTailCallPosition ( node
) {
77 if ( node
. parent
. type
=== "ArrowFunctionExpression" ) {
80 if ( node
. parent
. type
=== "ReturnStatement" ) {
81 return ! hasErrorHandler ( node
. parent
);
83 if ( node
. parent
. type
=== "ConditionalExpression" && ( node
=== node
. parent
. consequent
|| node
=== node
. parent
. alternate
)) {
84 return isInTailCallPosition ( node
. parent
);
86 if ( node
. parent
. type
=== "LogicalExpression" && node
=== node
. parent
. right
) {
87 return isInTailCallPosition ( node
. parent
);
89 if ( node
. parent
. type
=== "SequenceExpression" && node
=== node
. parent
. expressions
[ node
. parent
. expressions
. length
- 1 ]) {
90 return isInTailCallPosition ( node
. parent
);
96 AwaitExpression ( node
) {
97 if ( isInTailCallPosition ( node
) && ! hasErrorHandler ( node
)) {
98 reportUnnecessaryAwait ( node
);