sindresorhus/eslint-plugin-unicorn

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

Chiusa

#2149 aperta il 5 giu 2023

 (4 commenti) (12 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
bughelp wantedtypes

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

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.

Guida contributor