-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't include current field accesses in auto-deps
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 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
109 changes: 109 additions & 0 deletions
109
...react-compiler/src/__tests__/fixtures/compiler/nonreactive-ref-helper.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,109 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @inferEffectDependencies | ||
import {useEffect} from 'react'; | ||
import {makeObject_Primitives, print} from 'shared-runtime'; | ||
|
||
/** | ||
* Note that `obj` is currently added to the effect dependency array, even | ||
* though it's non-reactive due to memoization. | ||
* | ||
* This is a TODO in effect dependency inference. Note that we cannot simply | ||
* filter out non-reactive effect dependencies, as some non-reactive (by data | ||
* flow) values become reactive due to scope pruning. See the | ||
* `infer-effect-deps/pruned-nonreactive-obj` fixture for why this matters. | ||
* | ||
* Realizing that this `useEffect` should have an empty dependency array | ||
* requires effect dependency inference to be structured similarly to memo | ||
* dependency inference. | ||
* Pass 1: add all potential dependencies regardless of dataflow reactivity | ||
* Pass 2: (todo) prune non-reactive dependencies | ||
* | ||
* Note that instruction reordering should significantly reduce scope pruning | ||
*/ | ||
function NonReactiveDepInEffect() { | ||
const ref = useRefHelper(); | ||
const wrapped = useDeeperRefHelper(); | ||
useEffect(() => { | ||
print(ref.current); | ||
print(wrapped.foo.current); | ||
}); | ||
} | ||
|
||
function useRefHelper() { | ||
return useRef(0); | ||
} | ||
|
||
function useDeeperRefHelper() { | ||
return {foo: useRefHelper()}; | ||
} | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies | ||
import { useEffect } from "react"; | ||
import { makeObject_Primitives, print } from "shared-runtime"; | ||
|
||
/** | ||
* Note that `obj` is currently added to the effect dependency array, even | ||
* though it's non-reactive due to memoization. | ||
* | ||
* This is a TODO in effect dependency inference. Note that we cannot simply | ||
* filter out non-reactive effect dependencies, as some non-reactive (by data | ||
* flow) values become reactive due to scope pruning. See the | ||
* `infer-effect-deps/pruned-nonreactive-obj` fixture for why this matters. | ||
* | ||
* Realizing that this `useEffect` should have an empty dependency array | ||
* requires effect dependency inference to be structured similarly to memo | ||
* dependency inference. | ||
* Pass 1: add all potential dependencies regardless of dataflow reactivity | ||
* Pass 2: (todo) prune non-reactive dependencies | ||
* | ||
* Note that instruction reordering should significantly reduce scope pruning | ||
*/ | ||
function NonReactiveDepInEffect() { | ||
const $ = _c(3); | ||
const ref = useRefHelper(); | ||
const wrapped = useDeeperRefHelper(); | ||
let t0; | ||
if ($[0] !== ref.current || $[1] !== wrapped.foo.current) { | ||
t0 = () => { | ||
print(ref.current); | ||
print(wrapped.foo.current); | ||
}; | ||
$[0] = ref.current; | ||
$[1] = wrapped.foo.current; | ||
$[2] = t0; | ||
} else { | ||
t0 = $[2]; | ||
} | ||
useEffect(t0, [ref, wrapped.foo]); | ||
} | ||
|
||
function useRefHelper() { | ||
return useRef(0); | ||
} | ||
|
||
function useDeeperRefHelper() { | ||
const $ = _c(2); | ||
const t0 = useRefHelper(); | ||
let t1; | ||
if ($[0] !== t0) { | ||
t1 = { foo: t0 }; | ||
$[0] = t0; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
return t1; | ||
} | ||
|
||
``` | ||
### Eval output | ||
(kind: exception) Fixture not implemented |
37 changes: 37 additions & 0 deletions
37
...ges/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonreactive-ref-helper.js
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 @@ | ||
// @inferEffectDependencies | ||
import {useEffect} from 'react'; | ||
import {makeObject_Primitives, print} from 'shared-runtime'; | ||
|
||
/** | ||
* Note that `obj` is currently added to the effect dependency array, even | ||
* though it's non-reactive due to memoization. | ||
* | ||
* This is a TODO in effect dependency inference. Note that we cannot simply | ||
* filter out non-reactive effect dependencies, as some non-reactive (by data | ||
* flow) values become reactive due to scope pruning. See the | ||
* `infer-effect-deps/pruned-nonreactive-obj` fixture for why this matters. | ||
* | ||
* Realizing that this `useEffect` should have an empty dependency array | ||
* requires effect dependency inference to be structured similarly to memo | ||
* dependency inference. | ||
* Pass 1: add all potential dependencies regardless of dataflow reactivity | ||
* Pass 2: (todo) prune non-reactive dependencies | ||
* | ||
* Note that instruction reordering should significantly reduce scope pruning | ||
*/ | ||
function NonReactiveDepInEffect() { | ||
const ref = useRefHelper(); | ||
const wrapped = useDeeperRefHelper(); | ||
useEffect(() => { | ||
print(ref.current); | ||
print(wrapped.foo.current); | ||
}); | ||
} | ||
|
||
function useRefHelper() { | ||
return useRef(0); | ||
} | ||
|
||
function useDeeperRefHelper() { | ||
return {foo: useRefHelper()}; | ||
} |