-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compiler: only resolve globals and react imports
Updates Environment#getGlobalDeclaration() to only resolve "globals" if they are a true global or an import from react/react-dom. We still keep the logic to resolve hook-like names as custom hooks. Notably, this means that a local `Array` reference won't get confused with our Array global declaration, a local `useState` (or import from something other than React) won't get confused as `React.useState()`, etc. Still working on more tests. ghstack-source-id: 0c26e76d41512db0976de16bd33475723d6b99f6 Pull Request resolved: #29190
- Loading branch information
1 parent
912f820
commit c93d4b3
Showing
8 changed files
with
392 additions
and
53 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
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
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
152 changes: 152 additions & 0 deletions
152
...r/src/__tests__/fixtures/compiler/globals-dont-resolve-local-useState.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,152 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { useState as _useState, useCallback, useEffect } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
function useState(value) { | ||
"use no memo"; // opt-out because we want to force resetting the setState function | ||
const [state, _setState] = _useState(value); | ||
const setState = useCallback( | ||
(...args) => { | ||
console.log(...args); | ||
return _setState(...args); | ||
}, | ||
// explicitly reset the callback when state changes | ||
[state] | ||
); | ||
if (setState.state === undefined) { | ||
setState.state = state; | ||
} | ||
return [state, setState]; | ||
} | ||
|
||
function Component() { | ||
const [state, setState] = useState("hello"); | ||
console.log(state, setState.state); | ||
|
||
const callback = useCallback(() => { | ||
setState("goodbye"); | ||
}, [setState]); | ||
|
||
useEffect(() => { | ||
callback(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<ValidateMemoization inputs={[setState]} output={callback} /> | ||
{state} | ||
</> | ||
); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useState as _useState, useCallback, useEffect } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
function useState(value) { | ||
"use no memo"; // opt-out because we want to force resetting the setState function | ||
const [state, _setState] = _useState(value); | ||
const setState = useCallback( | ||
(...args) => { | ||
console.log(...args); | ||
return _setState(...args); | ||
}, | ||
// explicitly reset the callback when state changes | ||
[state] | ||
); | ||
if (setState.state === undefined) { | ||
setState.state = state; | ||
} | ||
return [state, setState]; | ||
} | ||
|
||
function Component() { | ||
const $ = _c(13); | ||
const [state, setState] = useState("hello"); | ||
console.log(state, setState.state); | ||
let t0; | ||
if ($[0] !== setState) { | ||
t0 = () => { | ||
setState("goodbye"); | ||
}; | ||
$[0] = setState; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
const callback = t0; | ||
let t1; | ||
if ($[2] !== callback) { | ||
t1 = () => { | ||
callback(); | ||
}; | ||
$[2] = callback; | ||
$[3] = t1; | ||
} else { | ||
t1 = $[3]; | ||
} | ||
let t2; | ||
if ($[4] === Symbol.for("react.memo_cache_sentinel")) { | ||
t2 = []; | ||
$[4] = t2; | ||
} else { | ||
t2 = $[4]; | ||
} | ||
useEffect(t1, t2); | ||
let t3; | ||
if ($[5] !== setState) { | ||
t3 = [setState]; | ||
$[5] = setState; | ||
$[6] = t3; | ||
} else { | ||
t3 = $[6]; | ||
} | ||
let t4; | ||
if ($[7] !== t3 || $[8] !== callback) { | ||
t4 = <ValidateMemoization inputs={t3} output={callback} />; | ||
$[7] = t3; | ||
$[8] = callback; | ||
$[9] = t4; | ||
} else { | ||
t4 = $[9]; | ||
} | ||
let t5; | ||
if ($[10] !== t4 || $[11] !== state) { | ||
t5 = ( | ||
<> | ||
{t4} | ||
{state} | ||
</> | ||
); | ||
$[10] = t4; | ||
$[11] = state; | ||
$[12] = t5; | ||
} else { | ||
t5 = $[12]; | ||
} | ||
return t5; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: exception) Output identity changed but inputs did not | ||
logs: ['hello','hello','goodbye','hello','hello','goodbye'] |
Oops, something went wrong.