typescript:S7503 has false positives wanting `await` keyword

It seems that lately the typescript:S7503 rule is expecting the await keyword where it shouldn’t be needed. I noticed such issues in my practice project.

I will start with the simpler examples, then I’ll show real ones from my project.

Fabricated examples for minimal reproducibility, reproducible in the SonarQube for VSCode extension:

The following is completely made up and therefore arguable, but let’s say you’re creating a function that is needed to be used by a function that requires an async function:

// Async arrow function has no 'await' expression.sonarqube(typescript:S7503)
export const getTextAsync = async (text: string) => text;

If I try to work around the issue, I will still run into issues:

// Unexpected `await` of a non-Promise (non-"Thenable") value.sonarqube(typescript:S4123)
export const getTextAsync2 = async (text: string) => await text;

// Prefer `return value` over `return Promise.resolve(value)`.sonarqube(typescript:S7746)
export const getTextAsync3 = async (text: string) => Promise.resolve(text);

And I just think it’s too limiting to require await where not necessary.

Or let’s say I create a wrapper function that wraps a regular function into an async function, then I get the same issue:

// Async arrow function 'asyncWrapper' has no 'await' expression.sonarqube(typescript:S7503)
export const asyncWrapper = async (func: () => string) => func();

Real examples simplified, reproducible in VSCode

This one is a pattern I actually have in my project. I think VSCode or Sonar is confused by the type definition of renderHook:

import { act, renderHook } from "@testing-library/react";

// Async arrow function has no 'await' expression.sonarqube(typescript:S7503)
export const renderNothing = async () =>
  (
    await act(async () =>
      renderHook(() => ({})),
    )
  ).result;

And this function has to be async in Next.js:

"use server";

// Async function 'AboutPage' has no 'await' expression.sonarqube(typescript:S7503)
export default async function AboutPage() {
  return (
    <>About</>
  )
};

Real examples, also reported by the SonarQube Cloud website:

The following are all server functions in Next.js, therefore have to have the async keyword:

import { NextResponse } from "next/server";

// Async arrow function 'GET' has no 'await' expression.sonarqube(typescript:S7503)
export const GET = async () =>
  NextResponse.json({
    status: "ok",
  });
"use server";

// Async function 'Home' has no 'await' expression.sonarqube(typescript:S7503)
function Home() {
  redirect("/tree");
}

See my practice project here:

Here’s another example from a NestJS project:

import { TransactionHost } from '@nestjs-cls/transactional';
import { TransactionalAdapterTypeOrm } from '@nestjs-cls/transactional-adapter-typeorm';
import { Injectable } from '@nestjs/common';
import { User } from './model/user.entity.js';

@Injectable()
export class UserRepository {
  // Async method 'repo' has no 'await' expression.sonarqube(typescript:S7503)
  private readonly repo = async () => this.txHost.tx.getRepository(User);

  // (The rest of the code is not important, only added for completeness.)
  constructor(private readonly txHost: TransactionHost<TransactionalAdapterTypeOrm>) {}

  readonly findByName = async (name: string): Promise<User | null> =>
    (await this.repo()).findOneBy({ name });

  readonly save = async (user: User) => (await this.repo()).save(user);
}

FYI, if someone is fighting with Prettier over the // NOSONAR comment so that it stays on the line where it needs to be, there is a workaround that trades off automated formatting, which is the use of a // prettier-ignore comment:

import { MigrationInterface, QueryRunner } from 'typeorm';
import { notUsedYetSoJustThrowAnError } from './util/migration-util.js';

export class MyMigration20261015090100 implements MigrationInterface {
  // Uninteresting part
  public async up(queryRunner: QueryRunner): Promise<void> {
    // ... (Omitted)
  }

  // prettier-ignore
  public async down(): Promise<void> { //  NOSONAR: Must conform to interface.
    notUsedYetSoJustThrowAnError();
  }
}

Edit: Alternatively, as I’m getting too many false positives, I’ll consider disabling the rule.

Hey @rkrisztian, thanks for sharing more examples and the workaround! This is already flagged for the team to look into.

Hi @rkrisztian,

Thank you for the detailed examples and follow-ups. We have addressed the S7503 reports for "use server" and MigrationInterface.down in https://sonarsource.atlassian.net/browse/JS-2623. The fix is merged but is not yet in a released analyzer build.

Some warnings in your post come from other rules, so I’ll keep this reply focused on S7503.

For the remaining S7503 examples, we do not yet see a requirement that makes the rule’s report incorrect. Could you share a small example that shows what fails when the code is changed to satisfy S7503? That would help us assess any further false positive.

Thanks again for helping us improve the rule.
-Francois

Dear Francois,

Thanks for the fixes so far.

Which examples have you found not to be about typescript:S7503? I’d like to verify.

For the examples were you found the rule remaining correct, have you tried adding await where it’s superfluous and seeing what happens? For example you might see warnings like this:

This “await” does not apply to a “Promise”. Remove it or fix the expression.sonarqube(typescript:S4123)

‘await’ has no effect on the type of this expression.ts(80007)

So it’s like one rule or linter is fighting with the other. And really, the problem is that the await is simply superfluous.

Hi @rkrisztian,

The warnings I meant were S4123 on getTextAsync2 and S7746 on getTextAsync3. I understand that you showed them as attempts to address S7503. They can appear to conflict when you need to keep async.

Adding await to a non-Promise value is not the answer. If returning a Promise is part of the function’s intended API, you can make that explicit in its type:

export const getTextAsync: (text: string) => Promise<string> =
  async text => text;

export const asyncWrapper: (func: () => string) => Promise<string> =
  async func => func();

Once an analyzer containing JS-2623 is available, S7503 will recognize these types and will not report those functions. If a Promise return is not needed, you can remove async instead.

I hope this clarifies how to address S7503 without adding an unnecessary await. Thank you again for sharing the examples.

-Francois

I see. Sorry for the misunderstanding.

And also sorry being late with this, but I just recognized that whatever the fix is, maybe it should be in agreement with how ESLint works (but I’m not sure, see below), because ESLint has a similar rule, and it works similarly:

// Async arrow function 'getTextAsync' has no 'await' expression. eslint@typescript-eslint/require-await
// Async arrow function 'getTextAsync' has no 'await' expression. sonarqube(typescript:S7503)
export const getTextAsync = async (text: string) => text;

// Async arrow function 'asyncWrapper' has no 'await' expression. eslint@typescript-eslint/require-await
// Async arrow function 'asyncWrapper' has no 'await' expression. sonarqube(typescript:S7503)
export const asyncWrapper = async (func: () => string) => func();

// Async arrow function 'getTextAsync2' has no 'await' expression. eslint@typescript-eslint/require-await
// Async arrow function 'getTextAsync2' has no 'await' expression. sonarqube(typescript:S7503)
export const getTextAsync2: (text: string) => Promise<string> = async (text) => text;

// Async arrow function 'asyncWrapper2' has no 'await' expression. eslint@typescript-eslint/require-await
// Async arrow function 'asyncWrapper2' has no 'await' expression. sonarqube(typescript:S7503)
export const asyncWrapper2: (func: () => string) => Promise<string> = async (func) => func();

So I thought, it’s still better if libraries work consistently the same way, so it means no surprises for projects that use both linter tools.

However, I quickly checked the ESLint issues whether they address the problem. I didn’t find much, except for this:

https://github.com/eslint/eslint/issues/16141

So because of technical limitations, the issue that interfaces require implementations to be async, the ESLint and SonarCloud solutions might differ anyway, but only if SonarCloud can solve the context issue. This problem is not as easy as I thought.

Still, as you can see, even ESLint thinks that giving a type to the function is not going to solve the problem. The function is still async, and it still returns a non-async value, but now has an explicit type. And I still find this solution verbose without doing much, because the explicit type information (I mean the Promise return type) is just superfluous.

Edit: I removed my words about promisify, it’s not relevant. I thought it would show clear intent for turning regular value into async, but it’s about callbacks, not returned values. And I guess no such function was invented because it’s simply not needed. Async functions can always return non-async values. They will just resolve immediately.

I don’t really know a trivial solution here.

Hi @rkrisztian,

Thank you for taking the time to work through these examples with us. Your feedback helped us identify one more improvement to S7503. “Has no await” can sound like a request to add one, while “Remove async” is too categorical when the function needs to keep it.

We created JS-2796 to improve the message, suggested fix, and description. The TypeScript guidance will help developers check for a missing await, decide whether async can safely be removed, or make an intended Promise-returning function type explicit.

I appreciate the care you put into the examples and follow-ups. They helped us make the next improvement clearer.

-Francois