Skip to content

unicorn/prefer-top-level-await gives false positives with zod's .catch() #2840

@AmarMuric04

Description

@AmarMuric04

The rule is obviously meant for promises but it's firing even for the zod's .catch() method which is used to catch a value if no value is provided, completely unrelated to what the rule is created to do.

unicorn/prefer-top-level-await

constschema = z.object({
	searchQuery: z.string().catch(""),
// Prefer top-level await over using a promise chain.eslint[unicorn/prefer-top-level-await](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v62.0.0/docs/rules/prefer-top-level-await.md)
	type: z
		.union([z.literal("v1"), z.literal("v2"), z.literal("both")])
		.catch("both"), 
// Prefer top-level await over using a promise chain.eslint[unicorn/prefer-top-level-await](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v62.0.0/docs/rules/prefer-top-level-await.md)
	onlyIncentivized: z.boolean().catch(false),
// Prefer top-level await over using a promise chain.eslint[unicorn/prefer-top-level-await](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v62.0.0/docs/rules/prefer-top-level-await.md)
});

This schema is a very common theme with Tanstack Router's route schemas, so for any project using tanstack router this rule is unusable, or for most projects that use zod.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions