Skip to content

False positive: unicorn/prefer-top-level-await triggered by z.catch usage (Zod Mini) #2684

@J3m5

Description

@J3m5

Describe the bug

The rule unicorn/prefer-top-level-await reports a false positive when using z.catch from Zod Mini (https://github.com/colinhacks/zod). Example code:

const paramsSchema = z.catch(
  z.object({
    q: z.optional(z.string()),
    page: z._default(z.coerce.number(), 1),
  }),
  {
    q: undefined,
    page: 1,
  }
);

This triggers the error:

Prefer top-level await over using a promise chain. (unicorn/prefer-top-level-await)

However, z.catch is not a promise chain or async code, but a schema utility from Zod. This is a false positive.

To Reproduce

  1. Use Zod Mini's z.catch in a file with eslint-plugin-unicorn enabled.
  2. Observe the false positive error from unicorn/prefer-top-level-await.

Expected behavior

No error should be reported for this usage, as it is not related to promises or async code.

Environment

  • zod: ^3.25.67
  • eslint: ^9.29.0
  • eslint-plugin-unicorn: ^59.0.1
  • Node.js: 22.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtypesIssues that happen in TypeScript or that require types

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions