From 07ba52bc1a09d7650685fab1c0da01e208ea96b9 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Fri, 19 Apr 2024 15:16:15 -0700 Subject: [PATCH] Repro for false positive gating mode hoisting check ghstack-source-id: c4f5777b85751e9d5f0323384160adfba55d883f Pull Request resolved: https://github.com/facebook/react-forget/pull/2875 --- ...todo-gating-with-react-memo.flow.expect.md | 32 +++++++++++++++++++ .../error.todo-gating-with-react-memo.flow.js | 11 +++++++ 2 files changed, 43 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-gating-with-react-memo.flow.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-gating-with-react-memo.flow.js diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-gating-with-react-memo.flow.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-gating-with-react-memo.flow.expect.md new file mode 100644 index 0000000000..670125a747 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-gating-with-react-memo.flow.expect.md @@ -0,0 +1,32 @@ + +## Input + +```javascript +// @flow @gating +import { memo } from "react"; + +// TODO: this appears as a hoisted reference to Component, but it's a type not runtime reference! +type Props = React.ElementConfig; + +component Component(value: string) { + return
{value}
; +} + +export default memo(Component); + +``` + + +## Error + +``` + 5 | type Props = React.ElementConfig; + 6 | +> 7 | component Component(value: string) { + | ^^^^^^^^^ Invariant: Encountered a function used before its declaration, which breaks Forget's gating codegen due to hoisting. Rewrite the reference to Component to not rely on hoisting to fix this issue (7:7) + 8 | return
{value}
; + 9 | } + 10 | +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-gating-with-react-memo.flow.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-gating-with-react-memo.flow.js new file mode 100644 index 0000000000..6130717ec1 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-gating-with-react-memo.flow.js @@ -0,0 +1,11 @@ +// @flow @gating +import { memo } from "react"; + +// TODO: this appears as a hoisted reference to Component, but it's a type not runtime reference! +type Props = React.ElementConfig; + +component Component(value: string) { + return
{value}
; +} + +export default memo(Component);