[babel] Rename useMemoCacheSource to enableUseMemoCachePolyfill
Lauren Tan committed
Nov 1, 2023 at 13:28 UTC
c289d098d787db3a27ce0e70846a58e57fd58cbf
8 files changed
+28
-37
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts
+3
-6
@@ -143,10 +143,7 @@ export function updateUseMemoCacheImport(
143
return;
144
}
145
146
- if (
147
- options.useMemoCacheSource === "react" ||
148
- options.useMemoCacheSource === null
149
- ) {
146
+ if (options.enableUseMemoCachePolyfill === false) {
147
// If Forget did successfully compile inject/update an import of
148
// `import {unstable_useMemoCache as useMemoCache} from 'react'` and rename
149
// `React.unstable_useMemoCache(n)` to `useMemoCache(n)`;
@@ -160,9 +157,9 @@ export function updateUseMemoCacheImport(
157
} else {
158
addUseMemoCacheImportDeclaration(program, "react");
159
}
163
- } else if (typeof options.useMemoCacheSource === "string") {
160
+ } else {
161
// import useMemoCache from userspace module
165
- addUseMemoCacheImportDeclaration(program, options.useMemoCacheSource);
162
+ addUseMemoCacheImportDeclaration(program, "react-forget-runtime");
163
}
164
}
165
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts
+4
-4
@@ -111,15 +111,15 @@ export type PluginOptions = {
111
compilationMode: CompilationMode;
112
113
/**
114
- * If specified, Forget will import `useMemoCache` from this module instead of React. Use this if
114
+ * If enabled, Forget will import `useMemoCache` from a polyfill instead of React. Use this if
115
* you are for whatever reason unable to use an experimental version of React.
116
*
117
* ```
118
* // If specified:
119
- * import {unstable_useMemoCache} from 'useMemoCache_DO_NOT_USE';
119
+ * import {unstable_useMemoCache} from 'react-forget-runtime';
120
* ```
121
*/
122
- useMemoCacheSource: string | null;
122
+ enableUseMemoCachePolyfill: boolean;
123
};
124
125
export type CompilationMode =
@@ -167,7 +167,7 @@ export const defaultOptions: PluginOptions = {
167
gating: null,
168
instrumentForget: null,
169
noEmit: false,
170
- useMemoCacheSource: null,
170
+ enableUseMemoCachePolyfill: false,
171
} as const;
172
173
export function parsePluginOptions(obj: unknown): PluginOptions {
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/userspace-use-memo-cache.expect.md
+2
-2
@@ -2,7 +2,7 @@
2
## Input
3
4
```javascript
5
-// @useMemoCacheSource
5
+// @enableUseMemoCachePolyfill
6
function Component(props) {
7
const [x, setX] = useState(1);
8
let y;
@@ -29,7 +29,7 @@ export const FIXTURE_ENTRYPOINT = {
29
## Code
30
31
```javascript
32
-import { unstable_useMemoCache as useMemoCache } from "shared-runtime"; // @useMemoCacheSource
32
+import { unstable_useMemoCache as useMemoCache } from "react-forget-runtime"; // @enableUseMemoCachePolyfill
33
function Component(props) {
34
const $ = useMemoCache(5);
35
const [x, setX] = useState(1);
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/userspace-use-memo-cache.js
+1
-1
@@ -1,4 +1,4 @@
1
-// @useMemoCacheSource
1
+// @enableUseMemoCachePolyfill
2
function Component(props) {
3
const [x, setX] = useState(1);
4
let y;
compiler/packages/fixture-test-utils/src/compiler-utils.ts
+4
-4
@@ -24,7 +24,7 @@ export function transformFixtureInput(
24
let instrumentForget = null;
25
let enableEmitFreeze = null;
26
let compilationMode: CompilationMode = "all";
27
- let useMemoCacheSource = null;
27
+ let enableUseMemoCachePolyfill = false;
28
29
if (firstLine.indexOf("@compilationMode(annotation)") !== -1) {
30
assert(
@@ -59,8 +59,8 @@ export function transformFixtureInput(
59
importSpecifierName: "makeReadOnly",
60
};
61
}
62
- if (firstLine.includes("@useMemoCacheSource")) {
63
- useMemoCacheSource = "shared-runtime";
62
+ if (firstLine.includes("@enableUseMemoCachePolyfill")) {
63
+ enableUseMemoCachePolyfill = true;
64
}
65
const config = parseConfigPragmaFn(firstLine);
66
const result = pluginFn(
@@ -108,7 +108,7 @@ export function transformFixtureInput(
108
instrumentForget,
109
panicThreshold: "ALL_ERRORS",
110
noEmit: false,
111
- useMemoCacheSource,
111
+ enableUseMemoCachePolyfill,
112
},
113
includeAst
114
);
compiler/packages/react-forget-runtime/src/index.ts
+11
-7
@@ -18,15 +18,19 @@ const {
18
19
type MemoCache = Array<number | typeof $empty>;
20
21
-export const $empty = Symbol.for("react.memo_cache_sentinel");
22
-
21
+const $empty = Symbol.for("react.memo_cache_sentinel");
22
+/**
23
+ * DANGER: this hook is NEVER meant to be called directly!
24
+ **/
25
export function unstable_useMemoCache(size: number) {
26
"use no forget";
25
- const $ = new Array(size);
26
- for (let ii = 0; ii < size; ii++) {
27
- $[ii] = $empty;
28
- }
29
- return useRef($).current;
27
+ return React.useState(() => {
28
+ const $ = new Array(size);
29
+ for (let ii = 0; ii < size; ii++) {
30
+ $[ii] = $empty;
31
+ }
32
+ return $;
33
+ })[0];
34
}
35
36
export function $read(memoCache: MemoCache, index: number) {
compiler/packages/sprout/src/SproutTodoFilter.ts
+3
-1
@@ -463,9 +463,11 @@ const skipFilter = new Set([
463
"todo.useContext-mutate-context-in-callback",
464
"loop-unused-let",
465
466
-
466
// Bug in Forget output
467
"todo-rename-source-variables",
468
+
469
+ // Tested e2e in forget-feedback repo
470
+ "userspace-use-memo-cache",
471
]);
472
473
export default skipFilter;
compiler/packages/sprout/src/shared-runtime.ts
-12
@@ -191,15 +191,3 @@ export const ObjectWithHooks = {
191
return 0;
192
},
193
};
194
-
195
-const $empty = Symbol.for("react.memo_cache_sentinel");
196
-export function unstable_useMemoCache(size: number) {
197
- "use no forget";
198
- return React.useState(() => {
199
- const $ = new Array(size);
200
- for (let ii = 0; ii < size; ii++) {
201
- $[ii] = $empty;
202
- }
203
- return $;
204
- })[0];
205
-}