Dont compile code w useMemoCache
Skips compilation of code that has a reference to `useMemoCache()`, as a last-resort to avoid double-compilation of code. This is meant as a quick way to unblock since we're still seeing some double compilation issues when syncing internally.
Joe Savona committed
Sep 18, 2023 at 09:46 UTC
b02f5a5e0e88b4b48de88228e21ddf88ace2434e
3 files changed
+83
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+22
@@ -357,6 +357,9 @@ function shouldVisitNode(
357
>,
358
pass: CompilerPass
359
): boolean {
360
+ if (hasUseMemoCacheCall(fn)) {
361
+ return false;
362
+ }
363
if (fn.node.body.type === "BlockStatement") {
364
// Opt-outs disable compilation regardless of mode
365
if (hasAnyUseNoForgetDirectives(fn.node.body.directives)) {
@@ -392,6 +395,25 @@ function shouldVisitNode(
395
}
396
}
397
398
+function hasUseMemoCacheCall(
399
+ fn: NodePath<
400
+ t.FunctionDeclaration | t.FunctionExpression | t.ArrowFunctionExpression
401
+ >
402
+): boolean {
403
+ let hasUseMemoCache = false;
404
+ fn.traverse({
405
+ Identifier(path) {
406
+ if (
407
+ path.node.name === "useMemoCache" ||
408
+ path.node.name === "unstable_useMemoCache"
409
+ ) {
410
+ hasUseMemoCache = true;
411
+ }
412
+ },
413
+ });
414
+ return hasUseMemoCache;
415
+}
416
+
417
function isHookName(s: string): boolean {
418
return /^use[A-Z0-9]/.test(s);
419
}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/skip-useMemoCache.expect.md
new
+45
@@ -0,0 +1,45 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ const $ = useMemoCache();
7
+ let x;
8
+ if ($[0] === undefined) {
9
+ x = [props.value];
10
+ $[0] = x;
11
+ } else {
12
+ x = $[0];
13
+ }
14
+ return x;
15
+}
16
+
17
+export const FIXTURE_ENTRYPOINT = {
18
+ fn: Component,
19
+ params: [{ value: 42 }],
20
+};
21
+
22
+```
23
+
24
+## Code
25
+
26
+```javascript
27
+function Component(props) {
28
+ const $ = useMemoCache();
29
+ let x;
30
+ if ($[0] === undefined) {
31
+ x = [props.value];
32
+ $[0] = x;
33
+ } else {
34
+ x = $[0];
35
+ }
36
+ return x;
37
+}
38
+
39
+export const FIXTURE_ENTRYPOINT = {
40
+ fn: Component,
41
+ params: [{ value: 42 }],
42
+};
43
+
44
+```
45
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/skip-useMemoCache.js
new
+16
@@ -0,0 +1,16 @@
1
+function Component(props) {
2
+ const $ = useMemoCache();
3
+ let x;
4
+ if ($[0] === undefined) {
5
+ x = [props.value];
6
+ $[0] = x;
7
+ } else {
8
+ x = $[0];
9
+ }
10
+ return x;
11
+}
12
+
13
+export const FIXTURE_ENTRYPOINT = {
14
+ fn: Component,
15
+ params: [{ value: 42 }],
16
+};