@samitouri / QOS-React-2 / commits / 0052f2e591

TryStatement: enable sprout on new tests

Enables sprout for all the new try/catch fixtures in this stack. I added new helpers and tried to make sure we're testing the most interesting codepath of each fixture. This is where property testing would help, since we could test multiple paths with a single block of code, but for now this seems like a good balance of coverage.

Joe Savona committed Sep 7, 2023 at 16:50 UTC 0052f2e591622fec493752b37af54e82a5752dac
12 files changed +153 -40
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-mutate-outer-value.expect.md
+32 -14
@@ -2,52 +2,70 @@
2 ## Input
3
4 ```javascript
5 +const { shallowCopy, throwErrorWithMessage } = require("shared-runtime");
6 +
7 function Component(props) {
8 const x = [];
9 try {
8 - x.push(foo());
10 + x.push(throwErrorWithMessage("oops"));
11 } catch {
10 - x.push(bar());
12 + x.push(shallowCopy({ a: props.a }));
13 }
14 return x;
15 }
16
17 +export const FIXTURE_ENTRYPOINT = {
18 + fn: Component,
19 + params: [{ a: 1 }],
20 +};
21 +
22 ```
23
24 ## Code
25
26 ```javascript
27 import { unstable_useMemoCache as useMemoCache } from "react";
28 +const { shallowCopy, throwErrorWithMessage } = require("shared-runtime");
29 +
30 function Component(props) {
22 - const $ = useMemoCache(3);
31 + const $ = useMemoCache(5);
32 + const c_0 = $[0] !== props.a;
33 let x;
24 - if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
34 + if (c_0) {
35 x = [];
36 try {
37 let t0;
28 - if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
29 - t0 = foo();
30 - $[1] = t0;
38 + if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
39 + t0 = throwErrorWithMessage("oops");
40 + $[2] = t0;
41 } else {
32 - t0 = $[1];
42 + t0 = $[2];
43 }
44 x.push(t0);
45 } catch {
46 + const c_3 = $[3] !== props.a;
47 let t1;
37 - if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
38 - t1 = bar();
39 - $[2] = t1;
48 + if (c_3) {
49 + t1 = shallowCopy({ a: props.a });
50 + $[3] = props.a;
51 + $[4] = t1;
52 } else {
41 - t1 = $[2];
53 + t1 = $[4];
54 }
55 x.push(t1);
56 }
45 - $[0] = x;
57 + $[0] = props.a;
58 + $[1] = x;
59 } else {
47 - x = $[0];
60 + x = $[1];
61 }
62 return x;
63 }
64
65 +export const FIXTURE_ENTRYPOINT = {
66 + fn: Component,
67 + params: [{ a: 1 }],
68 +};
69 +
70 ```
71
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-mutate-outer-value.js
+9 -2
@@ -1,9 +1,16 @@
1 +const { shallowCopy, throwErrorWithMessage } = require("shared-runtime");
2 +
3 function Component(props) {
4 const x = [];
5 try {
4 - x.push(foo());
6 + x.push(throwErrorWithMessage("oops"));
7 } catch {
6 - x.push(bar());
8 + x.push(shallowCopy({ a: props.a }));
9 }
10 return x;
11 }
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: Component,
15 + params: [{ a: 1 }],
16 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-catch-param.expect.md
+16 -2
@@ -2,11 +2,13 @@
2 ## Input
3
4 ```javascript
5 +const { throwInput } = require("shared-runtime");
6 +
7 function Component(props) {
8 let x = [];
9 try {
10 // foo could throw its argument...
9 - foo(x);
11 + throwInput(x);
12 } catch (e) {
13 // ... in which case this could be mutating `x`!
14 e.push(null);
@@ -15,19 +17,26 @@ function Component(props) {
17 return x;
18 }
19
20 +export const FIXTURE_ENTRYPOINT = {
21 + fn: Component,
22 + params: [{}],
23 +};
24 +
25 ```
26
27 ## Code
28
29 ```javascript
30 import { unstable_useMemoCache as useMemoCache } from "react";
31 +const { throwInput } = require("shared-runtime");
32 +
33 function Component(props) {
34 const $ = useMemoCache(1);
35 let x;
36 if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
37 x = [];
38 try {
30 - foo(x);
39 + throwInput(x);
40 } catch (t22) {
41 const e = t22;
42
@@ -41,5 +50,10 @@ function Component(props) {
50 return x;
51 }
52
53 +export const FIXTURE_ENTRYPOINT = {
54 + fn: Component,
55 + params: [{}],
56 +};
57 +
58 ```
59
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-catch-param.js
+8 -1
@@ -1,8 +1,10 @@
1 +const { throwInput } = require("shared-runtime");
2 +
3 function Component(props) {
4 let x = [];
5 try {
6 // foo could throw its argument...
5 - foo(x);
7 + throwInput(x);
8 } catch (e) {
9 // ... in which case this could be mutating `x`!
10 e.push(null);
@@ -10,3 +12,8 @@ function Component(props) {
12 }
13 return x;
14 }
15 +
16 +export const FIXTURE_ENTRYPOINT = {
17 + fn: Component,
18 + params: [{}],
19 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-return.expect.md
+20 -5
@@ -2,39 +2,49 @@
2 ## Input
3
4 ```javascript
5 +const { shallowCopy, throwInput } = require("shared-runtime");
6 +
7 // @debug
8 function Component(props) {
9 let x = [];
10 try {
9 - const y = foo();
11 + const y = shallowCopy({});
12 if (y == null) {
13 return;
14 }
13 - x.push(bar(y));
15 + x.push(throwInput(y));
16 } catch {
17 return null;
18 }
19 return x;
20 }
21
22 +export const FIXTURE_ENTRYPOINT = {
23 + fn: Component,
24 + params: [{}],
25 +};
26 +
27 ```
28
29 ## Code
30
31 ```javascript
25 -import { unstable_useMemoCache as useMemoCache } from "react"; // @debug
32 +import { unstable_useMemoCache as useMemoCache } from "react";
33 +const { shallowCopy, throwInput } = require("shared-runtime");
34 +
35 +// @debug
36 function Component(props) {
37 const $ = useMemoCache(1);
38 let x;
39 if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
40 x = [];
41 try {
32 - const y = foo();
42 + const y = shallowCopy({});
43 if (y == null) {
44 return;
45 }
46
37 - x.push(bar(y));
47 + x.push(throwInput(y));
48 } catch {
49 return null;
50 }
@@ -45,5 +55,10 @@ function Component(props) {
55 return x;
56 }
57
58 +export const FIXTURE_ENTRYPOINT = {
59 + fn: Component,
60 + params: [{}],
61 +};
62 +
63 ```
64
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-return.js
+9 -2
@@ -1,14 +1,21 @@
1 +const { shallowCopy, throwInput } = require("shared-runtime");
2 +
3 // @debug
4 function Component(props) {
5 let x = [];
6 try {
5 - const y = foo();
7 + const y = shallowCopy({});
8 if (y == null) {
9 return;
10 }
9 - x.push(bar(y));
11 + x.push(throwInput(y));
12 } catch {
13 return null;
14 }
15 return x;
16 }
17 +
18 +export const FIXTURE_ENTRYPOINT = {
19 + fn: Component,
20 + params: [{}],
21 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-within-mutable-range.expect.md
+18 -4
@@ -2,23 +2,32 @@
2 ## Input
3
4 ```javascript
5 +const { throwErrorWithMessage, shallowCopy } = require("shared-runtime");
6 +
7 function Component(props) {
8 const x = [];
9 try {
8 - x.push(foo());
10 + x.push(throwErrorWithMessage("oops"));
11 } catch {
10 - x.push(bar());
12 + x.push(shallowCopy({}));
13 }
14 x.push(props.value); // extend the mutable range to include the try/catch
15 return x;
16 }
17
18 +export const FIXTURE_ENTRYPOINT = {
19 + fn: Component,
20 + params: [{}],
21 +};
22 +
23 ```
24
25 ## Code
26
27 ```javascript
28 import { unstable_useMemoCache as useMemoCache } from "react";
29 +const { throwErrorWithMessage, shallowCopy } = require("shared-runtime");
30 +
31 function Component(props) {
32 const $ = useMemoCache(4);
33 const c_0 = $[0] !== props.value;
@@ -28,7 +37,7 @@ function Component(props) {
37 try {
38 let t0;
39 if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
31 - t0 = foo();
40 + t0 = throwErrorWithMessage("oops");
41 $[2] = t0;
42 } else {
43 t0 = $[2];
@@ -37,7 +46,7 @@ function Component(props) {
46 } catch {
47 let t1;
48 if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
40 - t1 = bar();
49 + t1 = shallowCopy({});
50 $[3] = t1;
51 } else {
52 t1 = $[3];
@@ -54,5 +63,10 @@ function Component(props) {
63 return x;
64 }
65
66 +export const FIXTURE_ENTRYPOINT = {
67 + fn: Component,
68 + params: [{}],
69 +};
70 +
71 ```
72
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-within-mutable-range.js
+9 -2
@@ -1,10 +1,17 @@
1 +const { throwErrorWithMessage, shallowCopy } = require("shared-runtime");
2 +
3 function Component(props) {
4 const x = [];
5 try {
4 - x.push(foo());
6 + x.push(throwErrorWithMessage("oops"));
7 } catch {
6 - x.push(bar());
8 + x.push(shallowCopy({}));
9 }
10 x.push(props.value); // extend the mutable range to include the try/catch
11 return x;
12 }
13 +
14 +export const FIXTURE_ENTRYPOINT = {
15 + fn: Component,
16 + params: [{}],
17 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch.expect.md
+16 -2
@@ -2,29 +2,38 @@
2 ## Input
3
4 ```javascript
5 +const { throwErrorWithMessage } = require("shared-runtime");
6 +
7 function Component(props) {
8 let x;
9 try {
8 - x = foo();
10 + x = throwErrorWithMessage("oops");
11 } catch {
12 x = null;
13 }
14 return x;
15 }
16
17 +export const FIXTURE_ENTRYPOINT = {
18 + fn: Component,
19 + params: [{}],
20 +};
21 +
22 ```
23
24 ## Code
25
26 ```javascript
27 import { unstable_useMemoCache as useMemoCache } from "react";
28 +const { throwErrorWithMessage } = require("shared-runtime");
29 +
30 function Component(props) {
31 const $ = useMemoCache(1);
32 let x = undefined;
33 try {
34 let t0;
35 if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
27 - t0 = foo();
36 + t0 = throwErrorWithMessage("oops");
37 $[0] = t0;
38 } else {
39 t0 = $[0];
@@ -36,5 +45,10 @@ function Component(props) {
45 return x;
46 }
47
48 +export const FIXTURE_ENTRYPOINT = {
49 + fn: Component,
50 + params: [{}],
51 +};
52 +
53 ```
54
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch.js
+8 -1
@@ -1,9 +1,16 @@
1 +const { throwErrorWithMessage } = require("shared-runtime");
2 +
3 function Component(props) {
4 let x;
5 try {
4 - x = foo();
6 + x = throwErrorWithMessage("oops");
7 } catch {
8 x = null;
9 }
10 return x;
11 }
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: Component,
15 + params: [{}],
16 +};
compiler/packages/sprout/src/SproutTodoFilter.ts
-5
@@ -412,11 +412,6 @@ const skipFilter = new Set([
412 "fbt-call-complex-param-value",
413 "fbt-template-string-same-scope",
414 "component-declaration-basic.flow",
415 - "try-catch-mutate-outer-value",
416 - "try-catch-within-mutable-range",
417 - "try-catch",
418 - "try-catch-with-return",
419 - "try-catch-with-catch-param",
415
416 // TODO: 🌲
417 "forest-basic",
compiler/packages/sprout/src/shared-runtime.ts
+8
@@ -73,6 +73,14 @@ export function sum(...args: Array<number>): number {
73 return args.reduce((result, arg) => result + arg, 0);
74 }
75
76 +export function throwErrorWithMessage(message: string): never {
77 + throw new Error(message);
78 +}
79 +
80 +export function throwInput(x: Object): never {
81 + throw x;
82 +}
83 +
84 /**
85 * React Components
86 */