From be9863a415dd82a55f0b990bd37894a079d7b5de Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Thu, 5 Dec 2024 15:18:14 +0000 Subject: [PATCH] test(linter): add more tests fo rules-of-hooks (#7683) closes #6651 --- crates/oxc_linter/src/rules/react/rules_of_hooks.rs | 8 +++++++- crates/oxc_linter/src/snapshots/react_rules_of_hooks.snap | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/oxc_linter/src/rules/react/rules_of_hooks.rs b/crates/oxc_linter/src/rules/react/rules_of_hooks.rs index dbdf678a8..6a719a89d 100644 --- a/crates/oxc_linter/src/rules/react/rules_of_hooks.rs +++ b/crates/oxc_linter/src/rules/react/rules_of_hooks.rs @@ -943,7 +943,11 @@ fn test() { const useSWRFn = immutable ? useSWRImutable : useSWR; return useSWRFn(options ? () => ['cloud', options.query.id, options.variables] : null, options ? () => fetcher(options) : null, configWithSuspense); - };" + };", + // https://github.com/oxc-project/oxc/issues/6651 + r"const MyComponent = makeComponent(() => { useHook(); });", + r"const MyComponent2 = makeComponent(function () { useHook(); });", + r"const MyComponent4 = makeComponent(function InnerComponent() { useHook(); });" ]; let fail = vec![ @@ -1577,6 +1581,8 @@ fn test() { // }); // } // " , + // https://github.com/oxc-project/oxc/issues/6651 + r"const MyComponent3 = makeComponent(function foo () { useHook(); });", ]; Tester::new(RulesOfHooks::NAME, RulesOfHooks::CATEGORY, pass, fail).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/react_rules_of_hooks.snap b/crates/oxc_linter/src/snapshots/react_rules_of_hooks.snap index e918a449a..de971c984 100644 --- a/crates/oxc_linter/src/snapshots/react_rules_of_hooks.snap +++ b/crates/oxc_linter/src/snapshots/react_rules_of_hooks.snap @@ -674,3 +674,9 @@ source: crates/oxc_linter/src/tester.rs · ─────────── 5 │ } ╰──── + + ⚠ eslint-plugin-react-hooks(rules-of-hooks): React Hook "useHook" is called in function "foo" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use". + ╭─[rules_of_hooks.tsx:1:45] + 1 │ const MyComponent3 = makeComponent(function foo () { useHook(); }); + · ─── + ╰────