From 9cf06c2a5c605a0fc3745d21e4b95db9cbae6a4d Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Fri, 16 May 2025 17:15:21 -0700 Subject: [PATCH] [compiler] Fix error message for custom hooks We were printing "Custom" instead of "hook". --- .../src/Validation/ValidateHooksUsage.ts | 2 +- .../error.bail.rules-of-hooks-3d692676194b.expect.md | 2 +- .../error.bail.rules-of-hooks-8503ca76d6f8.expect.md | 2 +- ...-in-nested-function-expression-object-expression.expect.md | 2 +- .../error.invalid-hook-in-nested-object-method.expect.md | 2 +- ...rror.invalid.invalid-rules-of-hooks-0a1dbff27ba0.expect.md | 2 +- ...rror.invalid.invalid-rules-of-hooks-0de1224ce64b.expect.md | 4 ++-- ...rror.invalid.invalid-rules-of-hooks-449a37146a83.expect.md | 2 +- ...rror.invalid.invalid-rules-of-hooks-76a74b4666e9.expect.md | 2 +- ...rror.invalid.invalid-rules-of-hooks-d842d36db450.expect.md | 2 +- ...rror.invalid.invalid-rules-of-hooks-d952b82c2597.expect.md | 2 +- .../transform-fire/error.invalid-nested-use-effect.expect.md | 2 +- 12 files changed, 13 insertions(+), 13 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts index e90f33c74076c..b28228339ce5f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts @@ -452,7 +452,7 @@ function visitFunctionExpression(errors: CompilerError, fn: HIRFunction): void { reason: 'Hooks must be called at the top level in the body of a function component or custom hook, and may not be called within function expressions. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)', loc: callee.loc, - description: `Cannot call ${hookKind} within a function component`, + description: `Cannot call ${hookKind === 'Custom' ? 'hook' : hookKind} within a function expression`, suggestions: null, }), ); diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rules-of-hooks/error.bail.rules-of-hooks-3d692676194b.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rules-of-hooks/error.bail.rules-of-hooks-3d692676194b.expect.md index 04808379b7f14..ffd91cc8a2a85 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rules-of-hooks/error.bail.rules-of-hooks-3d692676194b.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rules-of-hooks/error.bail.rules-of-hooks-3d692676194b.expect.md @@ -23,7 +23,7 @@ const ComponentWithHookInsideCallback = React.forwardRef((props, ref) => { 6 | const ComponentWithHookInsideCallback = React.forwardRef((props, ref) => { 7 | useEffect(() => { > 8 | useHookInsideCallback(); - | ^^^^^^^^^^^^^^^^^^^^^ InvalidReact: Hooks must be called at the top level in the body of a function component or custom hook, and may not be called within function expressions. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning). Cannot call Custom within a function component (8:8) + | ^^^^^^^^^^^^^^^^^^^^^ InvalidReact: Hooks must be called at the top level in the body of a function component or custom hook, and may not be called within function expressions. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning). Cannot call hook within a function expression (8:8) 9 | }); 10 | return