-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Always error on async reassignments
Summary: Addresses the issue in #30109: any mutation of a local in an async function may occur after rendering has finished. ghstack-source-id: 5cf4b1db7ffff2db962d9b1fc3e9b37e10e3dff2 Pull Request resolved: #30111
- Loading branch information
Showing
5 changed files
with
54 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ures/compiler/error.invalid-reassign-local-variable-in-async-callback.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Component() { | ||
let value = null; | ||
const reassign = async () => { | ||
await foo().then((result) => { | ||
// Reassigning a local variable in an async function is *always* mutating | ||
// after render, so this should error regardless of where this ends up | ||
// getting called | ||
value = result; | ||
}); | ||
}; | ||
|
||
const onClick = async () => { | ||
await reassign(); | ||
}; | ||
return <div onClick={onClick}>Click</div>; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
6 | // after render, so this should error regardless of where this ends up | ||
7 | // getting called | ||
> 8 | value = result; | ||
| ^^^^^ InvalidReact: Reassigning a variable in an async function can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `value` cannot be reassigned after render (8:8) | ||
9 | }); | ||
10 | }; | ||
11 | | ||
``` | ||
File renamed without changes.
58 changes: 0 additions & 58 deletions
58
...tures/compiler/todo.invalid-reassign-local-variable-in-async-callback.expect.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters