Add test for function deps not treated as conditional
In the test, unconditionally reading props.bar.length will throw when props.bar is null.
Sathya Gunasekaran committed
Jan 15, 2024 at 12:44 UTC
6d133111a9a71363daff068f2f9408a704b5cbfb
2 files changed
+69
"b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.expect.md"
new
+57
@@ -0,0 +1,57 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ function getLength() {
7
+ return props.bar.length;
8
+ }
9
+
10
+ return props.bar && getLength();
11
+}
12
+
13
+export const FIXTURE_ENTRYPOINT = {
14
+ fn: Component,
15
+ params: [{ bar: [] }],
16
+};
17
+
18
+```
19
+
20
+## Code
21
+
22
+```javascript
23
+import { unstable_useMemoCache as useMemoCache } from "react";
24
+function Component(props) {
25
+ const $ = useMemoCache(5);
26
+ let t0;
27
+ if ($[0] !== props.bar.length) {
28
+ t0 = function getLength() {
29
+ return props.bar.length;
30
+ };
31
+ $[0] = props.bar.length;
32
+ $[1] = t0;
33
+ } else {
34
+ t0 = $[1];
35
+ }
36
+ const getLength = t0;
37
+ let t1;
38
+ if ($[2] !== props.bar || $[3] !== getLength) {
39
+ t1 = props.bar && getLength();
40
+ $[2] = props.bar;
41
+ $[3] = getLength;
42
+ $[4] = t1;
43
+ } else {
44
+ t1 = $[4];
45
+ }
46
+ return t1;
47
+}
48
+
49
+export const FIXTURE_ENTRYPOINT = {
50
+ fn: Component,
51
+ params: [{ bar: [] }],
52
+};
53
+
54
+```
55
+
56
+### Eval output
57
+(kind: ok) 0
\ No newline at end of file
"b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.js"
new
+12
@@ -0,0 +1,12 @@
1
+function Component(props) {
2
+ function getLength() {
3
+ return props.bar.length;
4
+ }
5
+
6
+ return props.bar && getLength();
7
+}
8
+
9
+export const FIXTURE_ENTRYPOINT = {
10
+ fn: Component,
11
+ params: [{ bar: [] }],
12
+};