sindresorhus/eslint-plugin-unicorn

`prefer-top-level-await` falsely flagging zod `.catch` calls as promises

Closed

#2,149 opened on Jun 5, 2023

 (4 comments) (12 reactions) (0 assignees)JavaScript (468 forks)user submission
bughelp wantedtypes

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

In our Typescript app, we frequently use the zod .catch helper (see here) to add fallback behavior to schemas used to validate API data. These schema setups are being incorrectly flagged by the prefer-top-level-await rule as promises that should be given a top-level await instead.

Example:

import z from "zod";

const someSchema = z.string().catch("");

This rule just appears to error based on name, so this issue exists for any method called catch. These also both error:

const objectWithCatch = {
  catch: () => undefined,
};

objectWithCatch.catch(); // errors

const getCatchMethod = () => ({
  catch: () => undefined,
});

getCatchMethod().catch();

It would be great to pull typing information into this rule to make sure that the catch in question actually pertains to a promise.

Contributor guide