Skip to content

Commit 00775d9

Browse files
committed
[compiler] Add todo fixtures for local reassignment in an async callback
ghstack-source-id: eca878f62a2149af76a72b59acd1820d0df86f30 Pull Request resolved: #30109
1 parent 4c9a2d2 commit 00775d9

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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+
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ const skipFilter = new Set([
487487
"todo.invalid-reassign-local-variable-in-jsx-callback",
488488
"todo.invalid-reassign-local-variable-in-hook-argument",
489489
"todo.invalid-reassign-local-variable-in-effect",
490+
"todo.invalid-reassign-local-variable-in-async-callback",
490491

491492
// bugs
492493
"bug-invalid-hoisting-functionexpr",

0 commit comments

Comments
 (0)