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

Code before:
        schema: [
        ],

        messages: {
            removeAwait: "Remove `await` after `return`.",
            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 areAwaitAndAwaitedExpressionOnTheSameLine = node.loc.start.line === node.argument.loc.start.line;

                            if (!areAwaitAndAwaitedExpressionOnTheSameLine) {
                                return null;
                            }

                            const sourceCodeText = context.getSourceCode().text;

                            const startOfAwait = node.range[0];
                            const endOfAwait = startOfAwait + "await".length;

                            const characterAfterAwait = sourceCodeText.slice(endOfAwait, endOfAwait + 1);
                            const trimLength = characterAfterAwait === " " ? 1 : 0;

                            const range = [startOfAwait, endOfAwait + trimLength];

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

            });
        }

        /**
         * Determines whether a thrown error from this node will be caught/handled within this function rather than immediately halting

Review comment (suggestion):
We can safely fix the code if the `argument` is parenthesized and the opening parenthesis is on the same line as `await`.

For example:

```js
async () => {
    return await (
        foo()
    )
};

```

In this example, however, `node` and `node.argument` do not start on the same line, so per the current condition the suggestion will not be provided.

We could do something like this:

```js
const awaitToken = sourceCode.getFirstToken(node);
const tokenAfterAwait = sourceCode.getTokenAfter(awaitToken);
const areAwaitAndAwaitedExpressionOnTheSameLine = awaitToken.loc.start.line === tokenAfterAwait.loc.start.line;
```

(`sourceCode` is now needed in multiple places, so we could declare `const sourceCode = context.getSourceCode();` in `create(context)`).

Code after:
        schema: [
        ],

        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