-
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] Support for context variable loop iterators
Summary: Fixing a compiler todo ghstack-source-id: c4d9226b1745d003dc9945df1ac5c5a01712f909 Pull Request resolved: #31709
- Loading branch information
Showing
4 changed files
with
119 additions
and
60 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
31 changes: 0 additions & 31 deletions
31
.../fixtures/compiler/error.todo-for-loop-with-context-variable-iterator.expect.md
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
...c/__tests__/fixtures/compiler/for-loop-with-context-variable-iterator.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,78 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Component() { | ||
const data = useData(); | ||
const items = []; | ||
// NOTE: `i` is a context variable because it's reassigned and also referenced | ||
// within a closure, the `onClick` handler of each item | ||
for (let i = MIN; i <= MAX; i += INCREMENT) { | ||
items.push(<div key={i} onClick={() => data.set(i)} />); | ||
} | ||
return <>{items}</>; | ||
} | ||
|
||
const MIN = 0; | ||
const MAX = 3; | ||
const INCREMENT = 1; | ||
|
||
function useData() { | ||
return new Map(); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
params: [], | ||
fn: Component, | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
function Component() { | ||
const $ = _c(2); | ||
const data = useData(); | ||
let t0; | ||
if ($[0] !== data) { | ||
const items = []; | ||
for (let i = MIN; i <= MAX; i = i + INCREMENT, i) { | ||
items.push(<div key={i} onClick={() => data.set(i)} />); | ||
} | ||
|
||
t0 = <>{items}</>; | ||
$[0] = data; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
return t0; | ||
} | ||
|
||
const MIN = 0; | ||
const MAX = 3; | ||
const INCREMENT = 1; | ||
|
||
function useData() { | ||
const $ = _c(1); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = new Map(); | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
params: [], | ||
fn: Component, | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div></div><div></div><div></div><div></div> |
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