@samitouri / QOS-React / commits / 7b58600fb9

[tests] Repros for bugs and todos

Mofei Zhang committed Sep 21, 2023 at 22:38 UTC 7b58600fb928dae935e85182c2142629af38dfe5
15 files changed +341
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.bug-lambda-array-access-member-expr.expect.md new
+30
@@ -0,0 +1,30 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { invoke } from "shared-runtime";
6 +
7 +function Foo() {
8 + const x = [{ value: 0 }, { value: 1 }, { value: 2 }];
9 + const foo = (param: number) => {
10 + return x[param].value;
11 + };
12 +
13 + return invoke(foo, 1);
14 +}
15 +
16 +export const FIXTURE_ENTRYPONT = {
17 + fn: Foo,
18 + params: [{}],
19 +};
20 +
21 +```
22 +
23 +
24 +## Error
25 +
26 +```
27 +[ReactForget] Todo: EnterSSA: Expected identifier to be defined before being used. Identifier param$10 is undefined (5:5)
28 +```
29 +
30 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.bug-lambda-array-access-member-expr.ts new
+15
@@ -0,0 +1,15 @@
1 +import { invoke } from "shared-runtime";
2 +
3 +function Foo() {
4 + const x = [{ value: 0 }, { value: 1 }, { value: 2 }];
5 + const foo = (param: number) => {
6 + return x[param].value;
7 + };
8 +
9 + return invoke(foo, 1);
10 +}
11 +
12 +export const FIXTURE_ENTRYPONT = {
13 + fn: Foo,
14 + params: [{}],
15 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.bug-ternary-value-in-jsx-attribute.expect.md new
+27
@@ -0,0 +1,27 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { RenderPropAsChild, StaticText1, StaticText2 } from "shared-runtime";
6 +
7 +function Component(props: { showText1: boolean }) {
8 + const Foo = props.showText1 ? StaticText1 : StaticText2;
9 +
10 + return <RenderPropAsChild items={[() => <Foo />]} />;
11 +}
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: Component,
15 + params: [{ showText1: false }],
16 +};
17 +
18 +```
19 +
20 +
21 +## Error
22 +
23 +```
24 +[ReactForget] Invariant: Expected value for identifier `28` to be initialized. (6:6)
25 +```
26 +
27 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.bug-ternary-value-in-jsx-attribute.tsx new
+12
@@ -0,0 +1,12 @@
1 +import { RenderPropAsChild, StaticText1, StaticText2 } from "shared-runtime";
2 +
3 +function Component(props: { showText1: boolean }) {
4 + const Foo = props.showText1 ? StaticText1 : StaticText2;
5 +
6 + return <RenderPropAsChild items={[() => <Foo />]} />;
7 +}
8 +
9 +export const FIXTURE_ENTRYPOINT = {
10 + fn: Component,
11 + params: [{ showText1: false }],
12 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.expect.md new
+33
@@ -0,0 +1,33 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function useFoo(setOne: boolean) {
6 + let x;
7 + let y;
8 + let z;
9 + if (setOne) {
10 + x = y = z = 1;
11 + } else {
12 + x = 2;
13 + y = 3;
14 + z = 5;
15 + }
16 + return { x, y, z };
17 +}
18 +
19 +export const FIXTURE_ENTRYPONT = {
20 + fn: useFoo,
21 + params: [true],
22 +};
23 +
24 +```
25 +
26 +
27 +## Error
28 +
29 +```
30 +[ReactForget] Invariant: Const declaration cannot be referenced as an expression (6:6)
31 +```
32 +
33 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.const-propagation-phi-nodes.ts new
+18
@@ -0,0 +1,18 @@
1 +function useFoo(setOne: boolean) {
2 + let x;
3 + let y;
4 + let z;
5 + if (setOne) {
6 + x = y = z = 1;
7 + } else {
8 + x = 2;
9 + y = 3;
10 + z = 5;
11 + }
12 + return { x, y, z };
13 +}
14 +
15 +export const FIXTURE_ENTRYPONT = {
16 + fn: useFoo,
17 + params: [true],
18 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.expect.md new
+41
@@ -0,0 +1,41 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function useFoo(props: {
6 + x?: string;
7 + y?: string;
8 + z?: string;
9 + doDestructure: boolean;
10 +}) {
11 + let x = null;
12 + let y = null;
13 + let z = null;
14 + const myList = [];
15 + if (props.doDestructure) {
16 + ({ x, y, z } = props);
17 +
18 + myList.push(z);
19 + }
20 + return {
21 + x,
22 + y,
23 + myList,
24 + };
25 +}
26 +
27 +export const FIXTURE_ENTRYPONT = {
28 + fn: useFoo,
29 + params: [{ x: "hello", y: "world", doDestructure: true }],
30 +};
31 +
32 +```
33 +
34 +
35 +## Error
36 +
37 +```
38 +[ReactForget] Invariant: Expected consistent kind for destructuring. Other places were 'Reassign' but 'store z$44' is const (12:12)
39 +```
40 +
41 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.destructure-in-branch-ssa.ts new
+26
@@ -0,0 +1,26 @@
1 +function useFoo(props: {
2 + x?: string;
3 + y?: string;
4 + z?: string;
5 + doDestructure: boolean;
6 +}) {
7 + let x = null;
8 + let y = null;
9 + let z = null;
10 + const myList = [];
11 + if (props.doDestructure) {
12 + ({ x, y, z } = props);
13 +
14 + myList.push(z);
15 + }
16 + return {
17 + x,
18 + y,
19 + myList,
20 + };
21 +}
22 +
23 +export const FIXTURE_ENTRYPONT = {
24 + fn: useFoo,
25 + params: [{ x: "hello", y: "world", doDestructure: true }],
26 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.empty-catch-statement.expect.md new
+26
@@ -0,0 +1,26 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { getNumber } from "shared-runtime";
6 +
7 +function useFoo() {
8 + try {
9 + return getNumber();
10 + } catch {}
11 +}
12 +export const FIXTURE_ENTRYPONT = {
13 + fn: useFoo,
14 + params: [],
15 +};
16 +
17 +```
18 +
19 +
20 +## Error
21 +
22 +```
23 +[ReactForget] Invariant: Cannot emit the same block twice: bb2
24 +```
25 +
26 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.empty-catch-statement.ts new
+11
@@ -0,0 +1,11 @@
1 +import { getNumber } from "shared-runtime";
2 +
3 +function useFoo() {
4 + try {
5 + return getNumber();
6 + } catch {}
7 +}
8 +export const FIXTURE_ENTRYPONT = {
9 + fn: useFoo,
10 + params: [],
11 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.optional-call-chain-in-logical-expr.expect.md new
+26
@@ -0,0 +1,26 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { useNoAlias } from "shared-runtime";
6 +
7 +function useFoo(props: { value: { x: string; y: string } | null }) {
8 + const value = props.value;
9 + return useNoAlias(value?.x, value?.y) ?? {};
10 +}
11 +
12 +export const FIXTURE_ENTRYPONT = {
13 + fn: useFoo,
14 + props: [{ value: null }],
15 +};
16 +
17 +```
18 +
19 +
20 +## Error
21 +
22 +```
23 +[ReactForget] Invariant: Unexpected terminal kind 'optional' for logical test block (5:5)
24 +```
25 +
26 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.optional-call-chain-in-logical-expr.ts new
+11
@@ -0,0 +1,11 @@
1 +import { useNoAlias } from "shared-runtime";
2 +
3 +function useFoo(props: { value: { x: string; y: string } | null }) {
4 + const value = props.value;
5 + return useNoAlias(value?.x, value?.y) ?? {};
6 +}
7 +
8 +export const FIXTURE_ENTRYPONT = {
9 + fn: useFoo,
10 + props: [{ value: null }],
11 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-recursive-references-inner-functions.expect.md new
+31
@@ -0,0 +1,31 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Foo({ value }: { value: number }) {
6 + const factorial = (x: number) => {
7 + if (x <= 1) {
8 + return 1;
9 + } else {
10 + return x * factorial(x - 1);
11 + }
12 + };
13 +
14 + return factorial(value);
15 +}
16 +
17 +export const FIXTURE_ENTRYPONT = {
18 + fn: Foo,
19 + params: [{ value: 3 }],
20 +};
21 +
22 +```
23 +
24 +
25 +## Error
26 +
27 +```
28 +[ReactForget] Todo: EnterSSA: Expected identifier to be defined before being used. Identifier factorial$3 is undefined (2:8)
29 +```
30 +
31 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-recursive-references-inner-functions.ts new
+16
@@ -0,0 +1,16 @@
1 +function Foo({ value }: { value: number }) {
2 + const factorial = (x: number) => {
3 + if (x <= 1) {
4 + return 1;
5 + } else {
6 + return x * factorial(x - 1);
7 + }
8 + };
9 +
10 + return factorial(value);
11 +}
12 +
13 +export const FIXTURE_ENTRYPONT = {
14 + fn: Foo,
15 + params: [{ value: 3 }],
16 +};
compiler/packages/sprout/src/shared-runtime.ts
+18
@@ -86,6 +86,13 @@ export function useNoAlias(...args: Array<any>): object {
86 return noAliasObject;
87 }
88
89 +export function invoke<T extends Array<any>, ReturnType>(
90 + fn: (...input: T) => ReturnType,
91 + ...params: T
92 +) {
93 + return fn(...params);
94 +}
95 +
96 /**
97 * React Components
98 */
@@ -104,6 +111,17 @@ export function StaticText2(props: { children?: Array<React.ReactNode> }) {
111 return React.createElement("div", null, "StaticText2", props.children);
112 }
113
114 +export function RenderPropAsChild(props: {
115 + items: Array<() => React.ReactNode>;
116 +}) {
117 + return React.createElement(
118 + "div",
119 + null,
120 + "HigherOrderComponent",
121 + props.items.map((item) => item())
122 + );
123 +}
124 +
125 export function Stringify(props: any) {
126 return toJSON(props);
127 }