From 3f0afcbe471fdebab1b2df9c006d587f044aeb07 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Tue, 25 Jul 2023 15:56:46 +0100 Subject: [PATCH] [test] Failing test for using hooks from React namespace Forget assumes hooks are imported and used directly without the React namespace and generates incorrect code when if there's a namespace. --- .../_bug.hooks-with-React-namespace.expect.md | 30 +++++++++++++++++++ .../_bug.hooks-with-React-namespace.js | 4 +++ 2 files changed, 34 insertions(+) create mode 100644 compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/_bug.hooks-with-React-namespace.expect.md create mode 100644 compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/_bug.hooks-with-React-namespace.js diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/_bug.hooks-with-React-namespace.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/_bug.hooks-with-React-namespace.expect.md new file mode 100644 index 0000000000..2b2384dede --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/_bug.hooks-with-React-namespace.expect.md @@ -0,0 +1,30 @@ + +## Input + +```javascript +function Foo() { + const [x, setX] = React.useState(1); + return x; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Foo() { + const $ = useMemoCache(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = React.useState(1); + $[0] = t0; + } else { + t0 = $[0]; + } + const [x] = t0; + return x; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/_bug.hooks-with-React-namespace.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/_bug.hooks-with-React-namespace.js new file mode 100644 index 0000000000..41a3271cee --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/_bug.hooks-with-React-namespace.js @@ -0,0 +1,4 @@ +function Foo() { + const [x, setX] = React.useState(1); + return x; +}