-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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: a54ae2a7171fb42a5aad5b6537d6016e66778e4a Pull Request resolved: #29190
- Loading branch information
1 parent
912f820
commit 3f36fcd
Showing
8 changed files
with
301 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
87 changes: 87 additions & 0 deletions
87
...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,87 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { useState as _useState, useCallback, useEffect } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
function useState(value) { | ||
const [state, setState] = _useState(value); | ||
return [state, setState]; | ||
} | ||
|
||
function Component() { | ||
const [state, setState] = useState("hello"); | ||
|
||
return <div onClick={() => setState("goodbye")}>{state}</div>; | ||
} | ||
|
||
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) { | ||
const $ = _c(5); | ||
let t0; | ||
if ($[0] !== value) { | ||
t0 = _useState(value); | ||
$[0] = value; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
const [state, setState] = t0; | ||
let t1; | ||
if ($[2] !== state || $[3] !== setState) { | ||
t1 = [state, setState]; | ||
$[2] = state; | ||
$[3] = setState; | ||
$[4] = t1; | ||
} else { | ||
t1 = $[4]; | ||
} | ||
return t1; | ||
} | ||
|
||
function Component() { | ||
const $ = _c(5); | ||
const [state, setState] = useState("hello"); | ||
let t0; | ||
if ($[0] !== setState) { | ||
t0 = () => setState("goodbye"); | ||
$[0] = setState; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
let t1; | ||
if ($[2] !== t0 || $[3] !== state) { | ||
t1 = <div onClick={t0}>{state}</div>; | ||
$[2] = t0; | ||
$[3] = state; | ||
$[4] = t1; | ||
} else { | ||
t1 = $[4]; | ||
} | ||
return t1; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div>hello</div> |
18 changes: 18 additions & 0 deletions
18
...gin-react-compiler/src/__tests__/fixtures/compiler/globals-dont-resolve-local-useState.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,18 @@ | ||
import { useState as _useState, useCallback, useEffect } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
function useState(value) { | ||
const [state, setState] = _useState(value); | ||
return [state, setState]; | ||
} | ||
|
||
function Component() { | ||
const [state, setState] = useState("hello"); | ||
|
||
return <div onClick={() => setState("goodbye")}>{state}</div>; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; |
Oops, something went wrong.