-
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. I tried to write a proper fixture test to test that we react to changes to a custom setState setter function, but I think there may be an issue with snap and how it handles re-renders from effects. I think the tests are good here but open to feedback if we want to go down the rabbit hole of figuring out a proper snap test for this. ghstack-source-id: 5e9a8f6e0d23659c72a9d041e8d394b83d6e526d Pull Request resolved: #29190
- Loading branch information
1 parent
4a3e365
commit 5cec297
Showing
16 changed files
with
536 additions
and
56 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
30 changes: 30 additions & 0 deletions
30
.../fixtures/compiler/error.invalid-conditional-call-aliased-hook-import.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,30 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { useFragment as readFragment } from "shared-runtime"; | ||
|
||
function Component(props) { | ||
let data; | ||
if (props.cond) { | ||
data = readFragment(); | ||
} | ||
return data; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
4 | let data; | ||
5 | if (props.cond) { | ||
> 6 | data = readFragment(); | ||
| ^^^^^^^^^^^^ InvalidReact: Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning) (6:6) | ||
7 | } | ||
8 | return data; | ||
9 | } | ||
``` | ||
9 changes: 9 additions & 0 deletions
9
...ler/src/__tests__/fixtures/compiler/error.invalid-conditional-call-aliased-hook-import.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,9 @@ | ||
import { useFragment as readFragment } from "shared-runtime"; | ||
|
||
function Component(props) { | ||
let data; | ||
if (props.cond) { | ||
data = readFragment(); | ||
} | ||
return data; | ||
} |
30 changes: 30 additions & 0 deletions
30
..._/fixtures/compiler/error.invalid-conditional-call-aliased-react-hook.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,30 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { useState as state } from "react"; | ||
|
||
function Component(props) { | ||
let s; | ||
if (props.cond) { | ||
[s] = state(); | ||
} | ||
return s; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
4 | let s; | ||
5 | if (props.cond) { | ||
> 6 | [s] = state(); | ||
| ^^^^^ InvalidReact: Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning) (6:6) | ||
7 | } | ||
8 | return s; | ||
9 | } | ||
``` | ||
9 changes: 9 additions & 0 deletions
9
...iler/src/__tests__/fixtures/compiler/error.invalid-conditional-call-aliased-react-hook.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,9 @@ | ||
import { useState as state } from "react"; | ||
|
||
function Component(props) { | ||
let s; | ||
if (props.cond) { | ||
[s] = state(); | ||
} | ||
return s; | ||
} |
30 changes: 30 additions & 0 deletions
30
...res/compiler/error.invalid-conditional-call-non-hook-imported-as-hook.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,30 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { makeArray as useArray } from "other"; | ||
|
||
function Component(props) { | ||
let data; | ||
if (props.cond) { | ||
data = useArray(); | ||
} | ||
return data; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
4 | let data; | ||
5 | if (props.cond) { | ||
> 6 | data = useArray(); | ||
| ^^^^^^^^ InvalidReact: Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning) (6:6) | ||
7 | } | ||
8 | return data; | ||
9 | } | ||
``` | ||
9 changes: 9 additions & 0 deletions
9
...c/__tests__/fixtures/compiler/error.invalid-conditional-call-non-hook-imported-as-hook.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,9 @@ | ||
import { makeArray as useArray } from "other"; | ||
|
||
function Component(props) { | ||
let data; | ||
if (props.cond) { | ||
data = useArray(); | ||
} | ||
return data; | ||
} |
Oops, something went wrong.