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: