sindresorhus/eslint-plugin-unicorn

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

Fechada

#2.149 aberto em 5 de jun. de 2023

 (4 comentários) (12 reações) (0 responsável)JavaScript (468 forks)user submission
bughelp wantedtypes

Métricas do repositório

Stars
 (5.022 estrelas)
Métricas de merge de PR
 (Mesclagem média 4h 30m) (26 fundiu PRs em 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.

Guia do colaborador