@samitouri / QOS-React-1 / commits / 00775d94dc

[compiler] Add todo fixtures for local reassignment in an async callback

ghstack-source-id: eca878f62a2149af76a72b59acd1820d0df86f30 Pull Request resolved: https://github.com/facebook/react/pull/30109

Joe Savona committed Jun 26, 2024 at 16:06 UTC 00775d94dc32c5136787e3ca60065b9a5e2df9b8
3 files changed +75
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.invalid-reassign-local-variable-in-async-callback.expect.md new
+58
@@ -0,0 +1,58 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component() {
6 + let value = null;
7 + const reassign = async () => {
8 + await foo().then((result) => {
9 + // Reassigning a local variable in an async function is *always* mutating
10 + // after render, so this should error regardless of where this ends up
11 + // getting called
12 + value = result;
13 + });
14 + };
15 +
16 + const onClick = async () => {
17 + await reassign();
18 + };
19 + return <div onClick={onClick}>Click</div>;
20 +}
21 +
22 +```
23 +
24 +## Code
25 +
26 +```javascript
27 +import { c as _c } from "react/compiler-runtime";
28 +function Component() {
29 + const $ = _c(2);
30 + let value;
31 + if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
32 + value = null;
33 + $[0] = value;
34 + } else {
35 + value = $[0];
36 + }
37 + let t0;
38 + if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
39 + const reassign = async () => {
40 + await foo().then((result) => {
41 + value = result;
42 + });
43 + };
44 +
45 + const onClick = async () => {
46 + await reassign();
47 + };
48 +
49 + t0 = <div onClick={onClick}>Click</div>;
50 + $[1] = t0;
51 + } else {
52 + t0 = $[1];
53 + }
54 + return t0;
55 +}
56 +
57 +```
58 +
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.invalid-reassign-local-variable-in-async-callback.js new
+16
@@ -0,0 +1,16 @@
1 +function Component() {
2 + let value = null;
3 + const reassign = async () => {
4 + await foo().then((result) => {
5 + // Reassigning a local variable in an async function is *always* mutating
6 + // after render, so this should error regardless of where this ends up
7 + // getting called
8 + value = result;
9 + });
10 + };
11 +
12 + const onClick = async () => {
13 + await reassign();
14 + };
15 + return <div onClick={onClick}>Click</div>;
16 +}
compiler/packages/snap/src/SproutTodoFilter.ts
+1
@@ -487,6 +487,7 @@ const skipFilter = new Set([
487 "todo.invalid-reassign-local-variable-in-jsx-callback",
488 "todo.invalid-reassign-local-variable-in-hook-argument",
489 "todo.invalid-reassign-local-variable-in-effect",
490 + "todo.invalid-reassign-local-variable-in-async-callback",
491
492 // bugs
493 "bug-invalid-hoisting-functionexpr",