@samitouri / QOS-React-2 / commits / 0966480d16

[be] More tests for object methods

Mofei Zhang committed Dec 12, 2023 at 17:44 UTC 0966480d169efa261d4116162c9a1dda15e80c5f
27 files changed +803 -40
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-literal-method-call-in-ternary-test.expect.md new
+58
@@ -0,0 +1,58 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import {
6 + createHookWrapper,
7 + identity,
8 + CONST_STRING0,
9 + CONST_STRING1,
10 +} from "shared-runtime";
11 +
12 +function useHook({ value }) {
13 + return {
14 + getValue() {
15 + return identity(value);
16 + },
17 + }.getValue()
18 + ? CONST_STRING0
19 + : CONST_STRING1;
20 +}
21 +
22 +export const FIXTURE_ENTRYPOINT = {
23 + fn: createHookWrapper(useHook),
24 + params: [{ value: 0 }],
25 +};
26 +
27 +```
28 +
29 +## Code
30 +
31 +```javascript
32 +import {
33 + createHookWrapper,
34 + identity,
35 + CONST_STRING0,
36 + CONST_STRING1,
37 +} from "shared-runtime";
38 +
39 +function useHook(t18) {
40 + const { value } = t18;
41 + return {
42 + getValue() {
43 + return identity(value);
44 + },
45 + }.getValue()
46 + ? CONST_STRING0
47 + : CONST_STRING1;
48 +}
49 +
50 +export const FIXTURE_ENTRYPOINT = {
51 + fn: createHookWrapper(useHook),
52 + params: [{ value: 0 }],
53 +};
54 +
55 +```
56 +
57 +### Eval output
58 +(kind: ok) <div>{"result":"global string 1","shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-literal-method-call-in-ternary-test.js new
+21
@@ -0,0 +1,21 @@
1 +import {
2 + createHookWrapper,
3 + identity,
4 + CONST_STRING0,
5 + CONST_STRING1,
6 +} from "shared-runtime";
7 +
8 +function useHook({ value }) {
9 + return {
10 + getValue() {
11 + return identity(value);
12 + },
13 + }.getValue()
14 + ? CONST_STRING0
15 + : CONST_STRING1;
16 +}
17 +
18 +export const FIXTURE_ENTRYPOINT = {
19 + fn: createHookWrapper(useHook),
20 + params: [{ value: 0 }],
21 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-literal-method-derived-in-ternary-consequent.expect.md new
+48
@@ -0,0 +1,48 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { identity, createHookWrapper } from "shared-runtime";
6 +
7 +function useHook({ isCond, value }) {
8 + return isCond
9 + ? identity({
10 + getValue() {
11 + return value;
12 + },
13 + })
14 + : 42;
15 +}
16 +
17 +export const FIXTURE_ENTRYPOINT = {
18 + fn: createHookWrapper(useHook),
19 + params: [{ isCond: true, value: 0 }],
20 +};
21 +
22 +```
23 +
24 +## Code
25 +
26 +```javascript
27 +import { identity, createHookWrapper } from "shared-runtime";
28 +
29 +function useHook(t17) {
30 + const { isCond, value } = t17;
31 + return isCond
32 + ? identity({
33 + getValue() {
34 + return value;
35 + },
36 + })
37 + : 42;
38 +}
39 +
40 +export const FIXTURE_ENTRYPOINT = {
41 + fn: createHookWrapper(useHook),
42 + params: [{ isCond: true, value: 0 }],
43 +};
44 +
45 +```
46 +
47 +### Eval output
48 +(kind: ok) <div>{"result":{"getValue":{"kind":"Function","result":0}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-literal-method-derived-in-ternary-consequent.js new
+16
@@ -0,0 +1,16 @@
1 +import { identity, createHookWrapper } from "shared-runtime";
2 +
3 +function useHook({ isCond, value }) {
4 + return isCond
5 + ? identity({
6 + getValue() {
7 + return value;
8 + },
9 + })
10 + : 42;
11 +}
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: createHookWrapper(useHook),
15 + params: [{ isCond: true, value: 0 }],
16 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-consequent.expect.md new
+48
@@ -0,0 +1,48 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { createHookWrapper } from "shared-runtime";
6 +
7 +function useHook({ isCond, value }) {
8 + return isCond
9 + ? {
10 + getValue() {
11 + return value;
12 + },
13 + }
14 + : 42;
15 +}
16 +
17 +export const FIXTURE_ENTRYPOINT = {
18 + fn: createHookWrapper(useHook),
19 + params: [{ isCond: true, value: 0 }],
20 +};
21 +
22 +```
23 +
24 +## Code
25 +
26 +```javascript
27 +import { createHookWrapper } from "shared-runtime";
28 +
29 +function useHook(t15) {
30 + const { isCond, value } = t15;
31 + return isCond
32 + ? {
33 + getValue() {
34 + return value;
35 + },
36 + }
37 + : 42;
38 +}
39 +
40 +export const FIXTURE_ENTRYPOINT = {
41 + fn: createHookWrapper(useHook),
42 + params: [{ isCond: true, value: 0 }],
43 +};
44 +
45 +```
46 +
47 +### Eval output
48 +(kind: ok) <div>{"result":{"getValue":{"kind":"Function","result":0}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-consequent.js new
+16
@@ -0,0 +1,16 @@
1 +import { createHookWrapper } from "shared-runtime";
2 +
3 +function useHook({ isCond, value }) {
4 + return isCond
5 + ? {
6 + getValue() {
7 + return value;
8 + },
9 + }
10 + : 42;
11 +}
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: createHookWrapper(useHook),
15 + params: [{ isCond: true, value: 0 }],
16 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-test.expect.md new
+56
@@ -0,0 +1,56 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import {
6 + createHookWrapper,
7 + CONST_STRING0,
8 + CONST_STRING1,
9 +} from "shared-runtime";
10 +
11 +function useHook({ value }) {
12 + return {
13 + getValue() {
14 + return identity(value);
15 + },
16 + }
17 + ? CONST_STRING0
18 + : CONST_STRING1;
19 +}
20 +
21 +export const FIXTURE_ENTRYPOINT = {
22 + fn: createHookWrapper(useHook),
23 + params: [{ value: 0 }],
24 +};
25 +
26 +```
27 +
28 +## Code
29 +
30 +```javascript
31 +import {
32 + createHookWrapper,
33 + CONST_STRING0,
34 + CONST_STRING1,
35 +} from "shared-runtime";
36 +
37 +function useHook(t16) {
38 + const { value } = t16;
39 + return {
40 + getValue() {
41 + return identity(value);
42 + },
43 + }
44 + ? CONST_STRING0
45 + : CONST_STRING1;
46 +}
47 +
48 +export const FIXTURE_ENTRYPOINT = {
49 + fn: createHookWrapper(useHook),
50 + params: [{ value: 0 }],
51 +};
52 +
53 +```
54 +
55 +### Eval output
56 +(kind: ok) <div>{"result":"global string 0","shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-test.js new
+20
@@ -0,0 +1,20 @@
1 +import {
2 + createHookWrapper,
3 + CONST_STRING0,
4 + CONST_STRING1,
5 +} from "shared-runtime";
6 +
7 +function useHook({ value }) {
8 + return {
9 + getValue() {
10 + return identity(value);
11 + },
12 + }
13 + ? CONST_STRING0
14 + : CONST_STRING1;
15 +}
16 +
17 +export const FIXTURE_ENTRYPOINT = {
18 + fn: createHookWrapper(useHook),
19 + params: [{ value: 0 }],
20 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-maybe-alias.expect.md new
+54
@@ -0,0 +1,54 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { createHookWrapper, setProperty } from "shared-runtime";
6 +function useHook(props) {
7 + const x = {
8 + getX() {
9 + return props;
10 + },
11 + };
12 + const y = {
13 + getY() {
14 + return "y";
15 + },
16 + };
17 + return setProperty(x, y);
18 +}
19 +
20 +export const FIXTURE_ENTRYPOINT = {
21 + fn: createHookWrapper(useHook),
22 + params: [{ value: 0 }],
23 +};
24 +
25 +```
26 +
27 +## Code
28 +
29 +```javascript
30 +import { createHookWrapper, setProperty } from "shared-runtime";
31 +function useHook(props) {
32 + const x = {
33 + getX() {
34 + return props;
35 + },
36 + };
37 +
38 + const y = {
39 + getY() {
40 + return "y";
41 + },
42 + };
43 + return setProperty(x, y);
44 +}
45 +
46 +export const FIXTURE_ENTRYPOINT = {
47 + fn: createHookWrapper(useHook),
48 + params: [{ value: 0 }],
49 +};
50 +
51 +```
52 +
53 +### Eval output
54 +(kind: ok) <div>{"result":{"getX":{"kind":"Function","result":{"value":0}},"wat0":{"getY":{"kind":"Function","result":"y"}}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-maybe-alias.js new
+19
@@ -0,0 +1,19 @@
1 +import { createHookWrapper, setProperty } from "shared-runtime";
2 +function useHook(props) {
3 + const x = {
4 + getX() {
5 + return props;
6 + },
7 + };
8 + const y = {
9 + getY() {
10 + return "y";
11 + },
12 + };
13 + return setProperty(x, y);
14 +}
15 +
16 +export const FIXTURE_ENTRYPOINT = {
17 + fn: createHookWrapper(useHook),
18 + params: [{ value: 0 }],
19 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md
+7 -7
@@ -2,9 +2,9 @@
2 ## Input
3
4 ```javascript
5 -import { mutate } from "shared-runtime";
5 +import { createHookWrapper, mutate } from "shared-runtime";
6
7 -function Component(a) {
7 +function useHook(a) {
8 const x = { a };
9 let obj = {
10 method() {
@@ -16,7 +16,7 @@ function Component(a) {
16 }
17
18 export const FIXTURE_ENTRYPOINT = {
19 - fn: Component,
19 + fn: createHookWrapper(useHook),
20 params: [{ x: 1 }],
21 };
22
@@ -25,9 +25,9 @@ export const FIXTURE_ENTRYPOINT = {
25 ## Code
26
27 ```javascript
28 -import { mutate } from "shared-runtime";
28 +import { createHookWrapper, mutate } from "shared-runtime";
29
30 -function Component(a) {
30 +function useHook(a) {
31 const x = { a };
32 const obj = {
33 method() {
@@ -39,11 +39,11 @@ function Component(a) {
39 }
40
41 export const FIXTURE_ENTRYPOINT = {
42 - fn: Component,
42 + fn: createHookWrapper(useHook),
43 params: [{ x: 1 }],
44 };
45
46 ```
47
48 ### Eval output
49 -(kind: ok) {"a":{"x":1},"wat0":"joe"}
\ No newline at end of file
49 +(kind: ok) <div>{"result":{"a":{"x":1},"wat0":"joe"},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-3.js
+3 -3
@@ -1,6 +1,6 @@
1 -import { mutate } from "shared-runtime";
1 +import { createHookWrapper, mutate } from "shared-runtime";
2
3 -function Component(a) {
3 +function useHook(a) {
4 const x = { a };
5 let obj = {
6 method() {
@@ -12,6 +12,6 @@ function Component(a) {
12 }
13
14 export const FIXTURE_ENTRYPOINT = {
15 - fn: Component,
15 + fn: createHookWrapper(useHook),
16 params: [{ x: 1 }],
17 };
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-aliased-mutate-after.expect.md new
+58
@@ -0,0 +1,58 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { createHookWrapper, mutate, mutateAndReturn } from "shared-runtime";
6 +function useHook({ value }) {
7 + const x = mutateAndReturn({ value });
8 + const obj = {
9 + getValue() {
10 + return value;
11 + },
12 + };
13 + mutate(x);
14 + return obj;
15 +}
16 +
17 +export const FIXTURE_ENTRYPOINT = {
18 + fn: createHookWrapper(useHook),
19 + params: [{ value: 0 }],
20 +};
21 +
22 +```
23 +
24 +## Code
25 +
26 +```javascript
27 +import { unstable_useMemoCache as useMemoCache } from "react";
28 +import { createHookWrapper, mutate, mutateAndReturn } from "shared-runtime";
29 +function useHook(t21) {
30 + const $ = useMemoCache(1);
31 + const { value } = t21;
32 + const x = mutateAndReturn({ value });
33 + let t0;
34 + if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
35 + t0 = {
36 + getValue() {
37 + return value;
38 + },
39 + };
40 + $[0] = t0;
41 + } else {
42 + t0 = $[0];
43 + }
44 + const obj = t0;
45 +
46 + mutate(x);
47 + return obj;
48 +}
49 +
50 +export const FIXTURE_ENTRYPOINT = {
51 + fn: createHookWrapper(useHook),
52 + params: [{ value: 0 }],
53 +};
54 +
55 +```
56 +
57 +### Eval output
58 +(kind: ok) <div>{"result":{"getValue":{"kind":"Function","result":0}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-aliased-mutate-after.js new
+16
@@ -0,0 +1,16 @@
1 +import { createHookWrapper, mutate, mutateAndReturn } from "shared-runtime";
2 +function useHook({ value }) {
3 + const x = mutateAndReturn({ value });
4 + const obj = {
5 + getValue() {
6 + return value;
7 + },
8 + };
9 + mutate(x);
10 + return obj;
11 +}
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: createHookWrapper(useHook),
15 + params: [{ value: 0 }],
16 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-derived-value.expect.md new
+63
@@ -0,0 +1,63 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { createHookWrapper, mutateAndReturn } from "shared-runtime";
6 +function useHook({ value }) {
7 + const x = mutateAndReturn({ value });
8 + const obj = {
9 + getValue() {
10 + return x;
11 + },
12 + };
13 + return obj;
14 +}
15 +
16 +export const FIXTURE_ENTRYPOINT = {
17 + fn: createHookWrapper(useHook),
18 + params: [{ value: 0 }],
19 +};
20 +
21 +```
22 +
23 +## Code
24 +
25 +```javascript
26 +import { unstable_useMemoCache as useMemoCache } from "react";
27 +import { createHookWrapper, mutateAndReturn } from "shared-runtime";
28 +function useHook(t18) {
29 + const $ = useMemoCache(3);
30 + const { value } = t18;
31 + let t0;
32 + if ($[0] !== value) {
33 + t0 = mutateAndReturn({ value });
34 + $[0] = value;
35 + $[1] = t0;
36 + } else {
37 + t0 = $[1];
38 + }
39 + const x = t0;
40 + let t1;
41 + if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
42 + t1 = {
43 + getValue() {
44 + return x;
45 + },
46 + };
47 + $[2] = t1;
48 + } else {
49 + t1 = $[2];
50 + }
51 + const obj = t1;
52 + return obj;
53 +}
54 +
55 +export const FIXTURE_ENTRYPOINT = {
56 + fn: createHookWrapper(useHook),
57 + params: [{ value: 0 }],
58 +};
59 +
60 +```
61 +
62 +### Eval output
63 +(kind: ok) <div>{"result":{"getValue":{"kind":"Function","result":{"value":0,"wat0":"joe"}}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-derived-value.js new
+15
@@ -0,0 +1,15 @@
1 +import { createHookWrapper, mutateAndReturn } from "shared-runtime";
2 +function useHook({ value }) {
3 + const x = mutateAndReturn({ value });
4 + const obj = {
5 + getValue() {
6 + return x;
7 + },
8 + };
9 + return obj;
10 +}
11 +
12 +export const FIXTURE_ENTRYPOINT = {
13 + fn: createHookWrapper(useHook),
14 + params: [{ value: 0 }],
15 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-hook-dep.expect.md new
+53
@@ -0,0 +1,53 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { createHookWrapper } from "shared-runtime";
6 +import { useState } from "react";
7 +function useFoo() {
8 + const [state, _setState] = useState(false);
9 + return {
10 + func() {
11 + return state;
12 + },
13 + };
14 +}
15 +
16 +export const FIXTURE_ENTRYPOINT = {
17 + fn: createHookWrapper(useFoo),
18 + params: [{}],
19 +};
20 +
21 +```
22 +
23 +## Code
24 +
25 +```javascript
26 +import { createHookWrapper } from "shared-runtime";
27 +import { useState, unstable_useMemoCache as useMemoCache } from "react";
28 +function useFoo() {
29 + const $ = useMemoCache(1);
30 + const [state] = useState(false);
31 + let t0;
32 + if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
33 + t0 = {
34 + func() {
35 + return state;
36 + },
37 + };
38 + $[0] = t0;
39 + } else {
40 + t0 = $[0];
41 + }
42 + return t0;
43 +}
44 +
45 +export const FIXTURE_ENTRYPOINT = {
46 + fn: createHookWrapper(useFoo),
47 + params: [{}],
48 +};
49 +
50 +```
51 +
52 +### Eval output
53 +(kind: ok) <div>{"result":{"func":{"kind":"Function","result":false}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-hook-dep.js new
+15
@@ -0,0 +1,15 @@
1 +import { createHookWrapper } from "shared-runtime";
2 +import { useState } from "react";
3 +function useFoo() {
4 + const [state, _setState] = useState(false);
5 + return {
6 + func() {
7 + return state;
8 + },
9 + };
10 +}
11 +
12 +export const FIXTURE_ENTRYPOINT = {
13 + fn: createHookWrapper(useFoo),
14 + params: [{}],
15 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-mutated-after.expect.md new
+49
@@ -0,0 +1,49 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { createHookWrapper, mutate, mutateAndReturn } from "shared-runtime";
6 +function useHook({ value }) {
7 + const x = mutateAndReturn({ value });
8 + const obj = {
9 + getValue() {
10 + return x;
11 + },
12 + };
13 + mutate(obj);
14 + return obj;
15 +}
16 +
17 +export const FIXTURE_ENTRYPOINT = {
18 + fn: createHookWrapper(useHook),
19 + params: [{ value: 0 }],
20 +};
21 +
22 +```
23 +
24 +## Code
25 +
26 +```javascript
27 +import { createHookWrapper, mutate, mutateAndReturn } from "shared-runtime";
28 +function useHook(t21) {
29 + const { value } = t21;
30 + const x = mutateAndReturn({ value });
31 + const obj = {
32 + getValue() {
33 + return x;
34 + },
35 + };
36 +
37 + mutate(obj);
38 + return obj;
39 +}
40 +
41 +export const FIXTURE_ENTRYPOINT = {
42 + fn: createHookWrapper(useHook),
43 + params: [{ value: 0 }],
44 +};
45 +
46 +```
47 +
48 +### Eval output
49 +(kind: ok) <div>{"result":{"getValue":{"kind":"Function","result":{"value":0,"wat0":"joe"}},"wat0":"joe"},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-method-shorthand-mutated-after.js new
+16
@@ -0,0 +1,16 @@
1 +import { createHookWrapper, mutate, mutateAndReturn } from "shared-runtime";
2 +function useHook({ value }) {
3 + const x = mutateAndReturn({ value });
4 + const obj = {
5 + getValue() {
6 + return x;
7 + },
8 + };
9 + mutate(obj);
10 + return obj;
11 +}
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: createHookWrapper(useHook),
15 + params: [{ value: 0 }],
16 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-shorthand-method-1.expect.md
+10 -9
@@ -2,8 +2,8 @@
2 ## Input
3
4 ```javascript
5 -// @debug
6 -function Component({ a, b }) {
5 +import { createHookWrapper } from "shared-runtime";
6 +function useHook({ a, b }) {
7 return {
8 x: function () {
9 return [a];
@@ -15,8 +15,8 @@ function Component({ a, b }) {
15 }
16
17 export const FIXTURE_ENTRYPOINT = {
18 - fn: Component,
19 - params: [{ x: 1 }, { a: 2 }],
18 + fn: createHookWrapper(useHook),
19 + params: [{ a: 1, b: 2 }],
20 };
21
22 ```
@@ -24,8 +24,9 @@ export const FIXTURE_ENTRYPOINT = {
24 ## Code
25
26 ```javascript
27 -import { unstable_useMemoCache as useMemoCache } from "react"; // @debug
28 -function Component(t16) {
27 +import { unstable_useMemoCache as useMemoCache } from "react";
28 +import { createHookWrapper } from "shared-runtime";
29 +function useHook(t16) {
30 const $ = useMemoCache(4);
31 const { a, b } = t16;
32 let t0;
@@ -55,11 +56,11 @@ function Component(t16) {
56 }
57
58 export const FIXTURE_ENTRYPOINT = {
58 - fn: Component,
59 - params: [{ x: 1 }, { a: 2 }],
59 + fn: createHookWrapper(useHook),
60 + params: [{ a: 1, b: 2 }],
61 };
62
63 ```
64
65 ### Eval output
65 -(kind: ok) {"x":"[[ function params=0 ]]","y":"[[ function params=0 ]]"}
\ No newline at end of file
66 +(kind: ok) <div>{"result":{"x":{"kind":"Function","result":[1]},"y":{"kind":"Function","result":[2]}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-shorthand-method-1.js
+4 -4
@@ -1,5 +1,5 @@
1 -// @debug
2 -function Component({ a, b }) {
1 +import { createHookWrapper } from "shared-runtime";
2 +function useHook({ a, b }) {
3 return {
4 x: function () {
5 return [a];
@@ -11,6 +11,6 @@ function Component({ a, b }) {
11 }
12
13 export const FIXTURE_ENTRYPOINT = {
14 - fn: Component,
15 - params: [{ x: 1 }, { a: 2 }],
14 + fn: createHookWrapper(useHook),
15 + params: [{ a: 1, b: 2 }],
16 };
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-shorthand-method-2.expect.md
+11 -7
@@ -2,7 +2,9 @@
2 ## Input
3
4 ```javascript
5 -function Component({ a, b, c }) {
5 +import { createHookWrapper } from "shared-runtime";
6 +
7 +function useHook({ a, b, c }) {
8 return {
9 x: [a],
10 y() {
@@ -13,8 +15,8 @@ function Component({ a, b, c }) {
15 }
16
17 export const FIXTURE_ENTRYPOINT = {
16 - fn: Component,
17 - params: [{ x: 1 }, { a: 2 }, { b: 2 }],
18 + fn: createHookWrapper(useHook),
19 + params: [{ a: 1, b: 2, c: 2 }],
20 };
21
22 ```
@@ -23,7 +25,9 @@ export const FIXTURE_ENTRYPOINT = {
25
26 ```javascript
27 import { unstable_useMemoCache as useMemoCache } from "react";
26 -function Component(t16) {
28 +import { createHookWrapper } from "shared-runtime";
29 +
30 +function useHook(t16) {
31 const $ = useMemoCache(7);
32 const { a, b, c } = t16;
33 let t0;
@@ -61,11 +65,11 @@ function Component(t16) {
65 }
66
67 export const FIXTURE_ENTRYPOINT = {
64 - fn: Component,
65 - params: [{ x: 1 }, { a: 2 }, { b: 2 }],
68 + fn: createHookWrapper(useHook),
69 + params: [{ a: 1, b: 2, c: 2 }],
70 };
71
72 ```
73
74 ### Eval output
71 -(kind: ok) {"x":[null],"y":"[[ function params=0 ]]","z":{}}
\ No newline at end of file
75 +(kind: ok) <div>{"result":{"x":[1],"y":{"kind":"Function","result":[2]},"z":{"c":2}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-shorthand-method-2.js
+5 -3
@@ -1,4 +1,6 @@
1 -function Component({ a, b, c }) {
1 +import { createHookWrapper } from "shared-runtime";
2 +
3 +function useHook({ a, b, c }) {
4 return {
5 x: [a],
6 y() {
@@ -9,6 +11,6 @@ function Component({ a, b, c }) {
11 }
12
13 export const FIXTURE_ENTRYPOINT = {
12 - fn: Component,
13 - params: [{ x: 1 }, { a: 2 }, { b: 2 }],
14 + fn: createHookWrapper(useHook),
15 + params: [{ a: 1, b: 2, c: 2 }],
16 };
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-shorthand-method-nested.expect.md new
+69
@@ -0,0 +1,69 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { useState } from "react";
6 +import { createHookWrapper } from "shared-runtime";
7 +
8 +function useHook({ value }) {
9 + const [state] = useState(false);
10 +
11 + return {
12 + getX() {
13 + return {
14 + a: [],
15 + getY() {
16 + return value;
17 + },
18 + state,
19 + };
20 + },
21 + };
22 +}
23 +
24 +export const FIXTURE_ENTRYPOINT = {
25 + fn: createHookWrapper(useHook),
26 + params: [{ value: 0 }],
27 +};
28 +
29 +```
30 +
31 +## Code
32 +
33 +```javascript
34 +import { useState, unstable_useMemoCache as useMemoCache } from "react";
35 +import { createHookWrapper } from "shared-runtime";
36 +
37 +function useHook(t21) {
38 + const $ = useMemoCache(1);
39 + const { value } = t21;
40 + const [state] = useState(false);
41 + let t0;
42 + if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
43 + t0 = {
44 + getX() {
45 + return {
46 + a: [],
47 + getY() {
48 + return value;
49 + },
50 + state,
51 + };
52 + },
53 + };
54 + $[0] = t0;
55 + } else {
56 + t0 = $[0];
57 + }
58 + return t0;
59 +}
60 +
61 +export const FIXTURE_ENTRYPOINT = {
62 + fn: createHookWrapper(useHook),
63 + params: [{ value: 0 }],
64 +};
65 +
66 +```
67 +
68 +### Eval output
69 +(kind: ok) <div>{"result":{"getX":{"kind":"Function","result":{"a":[],"getY":{"kind":"Function","result":0},"state":false}}},"shouldInvokeFns":true}</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/object-shorthand-method-nested.js new
+23
@@ -0,0 +1,23 @@
1 +import { useState } from "react";
2 +import { createHookWrapper } from "shared-runtime";
3 +
4 +function useHook({ value }) {
5 + const [state] = useState(false);
6 +
7 + return {
8 + getX() {
9 + return {
10 + a: [],
11 + getY() {
12 + return value;
13 + },
14 + state,
15 + };
16 + },
17 + };
18 +}
19 +
20 +export const FIXTURE_ENTRYPOINT = {
21 + fn: createHookWrapper(useHook),
22 + params: [{ value: 0 }],
23 +};
compiler/packages/sprout/src/shared-runtime.ts
+30 -7
@@ -6,7 +6,7 @@
6 */
7
8 import { IntlVariations, IntlViewerContext, init } from "fbt";
9 -import React from "react";
9 +import React, { FunctionComponent } from "react";
10
11 /**
12 * This file is meant for use by `runner-evaluator` and fixture tests.
@@ -81,7 +81,7 @@ export function mutateAndReturnNewValue<T>(arg: T): string {
81 export function setProperty(arg: any, property: any): void {
82 // don't mutate primitive
83 if (typeof arg === null || typeof arg !== "object") {
84 - return;
84 + return arg;
85 }
86
87 let count: number = 0;
@@ -90,7 +90,7 @@ export function setProperty(arg: any, property: any): void {
90 key = "wat" + count;
91 if (!Object.hasOwn(arg, key)) {
92 arg[key] = property;
93 - return;
93 + return arg;
94 }
95 count++;
96 }
@@ -195,16 +195,39 @@ export function RenderPropAsChild(props: {
195 }
196
197 export function Stringify(props: any): React.ReactElement {
198 - return React.createElement("div", null, toJSON(props));
198 + return React.createElement(
199 + "div",
200 + null,
201 + toJSON(props, props?.shouldInvokeFns)
202 + );
203 +}
204 +
205 +export function createHookWrapper<TProps, TRet>(
206 + useMaybeHook: (props: TProps) => TRet
207 +): FunctionComponent<TProps> {
208 + return function Component(props: TProps): React.ReactElement {
209 + const result = useMaybeHook(props);
210 + return Stringify({
211 + result: result,
212 + shouldInvokeFns: true,
213 + });
214 + };
215 }
216
217 // helper functions
202 -export function toJSON(value: any) {
218 +export function toJSON(value: any, invokeFns: boolean = false) {
219 const seen = new Map();
220
221 return JSON.stringify(value, (_key: string, val: any) => {
222 if (typeof val === "function") {
207 - return `[[ function params=${val.length} ]]`;
223 + if (val.length === 0 && invokeFns) {
224 + return {
225 + kind: "Function",
226 + result: val(),
227 + };
228 + } else {
229 + return `[[ function params=${val.length} ]]`;
230 + }
231 } else if (typeof val === "object") {
232 let id = seen.get(val);
233 if (id != null) {
@@ -235,5 +258,5 @@ export const ObjectWithHooks = {
258 },
259 useIdentity<T>(arg: T): T {
260 return arg;
238 - }
261 + },
262 };