Repository: eslint/eslint
File: lib/rules/no-return-await.js (JavaScript)

Code before:
        ],

        messages: {
            removeAwait: "Remove await after return statement.",
            redundantUseOfAwait: "Redundant use of `await` on a return value."
        }
    },

    create(context) {

        /**
         * Reports a found unnecessary `await` expression.
         * @param {ASTNode} node The node representing the `await` expression to report
         * @returns {void}
         */
        function reportUnnecessaryAwait(node) {
            context.report({
                node: context.getSourceCode().getFirstToken(node),
                loc: node.loc,
                messageId: "redundantUseOfAwait",
                suggest: [
                    {
                        messageId: "removeAwait",
                        data: { type: "block" },
                        fix(fixer) {
                            const range = [node.range[0], node.range[0] + 6];

                            return fixer.replaceTextRange(range, "");
                        }
                    }
                ]

            });
        }

        /**
         * Determines whether a thrown error from this node will be caught/handled within this function rather than immediately halting
         * this function. For example, a statement in a `try` block will always have an error handler. A statement in
         * a `catch` block will only have an error handler if there is also a `finally` block.
         * @param {ASTNode} node A node representing a location where an could be thrown
         * @returns {boolean} `true` if a thrown error will be caught/handled in this function
         */
        function hasErrorHandler(node) {
            let ancestor = node;

            while (!astUtils.isFunction(ancestor) && ancestor.type !== "Program") {
                if (ancestor.parent.type === "TryStatement" && (ancestor === ancestor.parent.block || ancestor === ancestor.parent.handler && ancestor.parent.finalizer)) {
                    return true;
                }
                ancestor = ancestor.parent;
            }

Review comment (suggestion):
This could remove a non-whitespace character right after the `await`, for example:

```js
async () => {
    return await(foo());
}

```

after the suggestions is applied:

```js
async () => {
    return foo()); // Parsing error: Unexpected token )
}

```

Code after:
        ],

        messages: {
            removeAwait: "Remove redundant `await`.",
            redundantUseOfAwait: "Redundant use of `await` on a return value."
        }
    },

    create(context) {

        /**
         * Reports a found unnecessary `await` expression.
         * @param {ASTNode} node The node representing the `await` expression to report
         * @returns {void}
         */
        function reportUnnecessaryAwait(node) {
            context.report({
                node: context.getSourceCode().getFirstToken(node),
                loc: node.loc,
                messageId: "redundantUseOfAwait",
                suggest: [
                    {
                        messageId: "removeAwait",
                        fix(fixer) {
                            const sourceCode = context.getSourceCode();
                            const [awaitToken, tokenAfterAwait] = sourceCode.getFirstTokens(node, 2);

                            const areAwaitAndAwaitedExpressionOnTheSameLine = awaitToken.loc.start.line === tokenAfterAwait.loc.start.line;

                            if (!areAwaitAndAwaitedExpressionOnTheSameLine) {
                                return null;
                            }

                            const [startOfAwait, endOfAwait] = awaitToken.range;

                            const characterAfterAwait = sourceCode.text[endOfAwait];
                            const trimLength = characterAfterAwait === " " ? 1 : 0;

                            const range = [startOfAwait, endOfAwait + trimLength];

                            return fixer.removeRange(range);
                        }
                    }
                ]

            });
        }

        /**
         * Determines whether a thrown error from this node will be caught/handled within this function rather than immediately halting
         * this function. For example, a statement in a `try` block will always have an error handler. A statement in