From 1b141d322912886af681965ce0e0eb7b5d8d6c65 Mon Sep 17 00:00:00 2001 From: Mattis Abrahamsson Date: Mon, 5 Aug 2024 14:01:18 +0200 Subject: [PATCH] [no-use-prefix-for-non-hook]: Fix allow call expression assignment --- src/rules/noUsePrefixForNonHook.ts | 8 ++++++++ src/tests/noUsePrefixForNonHook.test.ts | 1 + 2 files changed, 9 insertions(+) diff --git a/src/rules/noUsePrefixForNonHook.ts b/src/rules/noUsePrefixForNonHook.ts index 7d51ad5..2ff5e68 100644 --- a/src/rules/noUsePrefixForNonHook.ts +++ b/src/rules/noUsePrefixForNonHook.ts @@ -176,6 +176,14 @@ export const noUsePrefixForNonHook = ESLintUtils.RuleCreator.withoutDocs({ return; } + // Check if the variable is assigned a function, we accept functions named as hooks or not, since it's quite common to create a hook with a factory function, such as `createStore` from `zustand` + if ( + declaration.init && + declaration.init.type === "CallExpression" + ) { + return; + } + context.report({ node, messageId: "noUsePrefixForNonHook", diff --git a/src/tests/noUsePrefixForNonHook.test.ts b/src/tests/noUsePrefixForNonHook.test.ts index 4bc544a..6060f80 100644 --- a/src/tests/noUsePrefixForNonHook.test.ts +++ b/src/tests/noUsePrefixForNonHook.test.ts @@ -31,6 +31,7 @@ ruleTester.run("noUsePrefixForNonHook", noUsePrefixForNonHook, { "const user = null;", "const myVariable = null;", "const data = useUserData();", + "const useStore = createStore();", ], invalid: [ {