-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Ref validation repro for ImportSpecifier with renamed local
This was originally reported in reactwg/react-compiler#27. Adding a failing repro to capture this case.
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...ct-compiler/src/__tests__/fixtures/compiler/error.bug-import-as-local.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,71 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { | ||
useEffect, | ||
useRef, | ||
// @ts-expect-error | ||
experimental_useEffectEvent as useEffectEvent, | ||
} from 'react'; | ||
|
||
let id = 0; | ||
function uniqueId() { | ||
'use no memo'; | ||
return id++; | ||
} | ||
|
||
export function useCustomHook(src: string): void { | ||
const uidRef = useRef(uniqueId()); | ||
const destroyed = useRef(false); | ||
const getItem = (srcName, uid) => { | ||
return {srcName, uid}; | ||
}; | ||
|
||
const getItemEvent = useEffectEvent(() => { | ||
if (destroyed.current) return; | ||
|
||
getItem(src, uidRef.current); | ||
}); | ||
|
||
useEffect(() => { | ||
destroyed.current = false; | ||
getItemEvent(); | ||
}, []); | ||
} | ||
|
||
function Component() { | ||
useCustomHook('hello'); | ||
return <div>Hello</div>; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
isComponent: true, | ||
params: [{x: 1}], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
19 | }; | ||
20 | | ||
> 21 | const getItemEvent = useEffectEvent(() => { | ||
| ^^^^^^^ | ||
> 22 | if (destroyed.current) return; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
> 23 | | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
> 24 | getItem(src, uidRef.current); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
> 25 | }); | ||
| ^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (21:25) | ||
26 | | ||
27 | useEffect(() => { | ||
28 | destroyed.current = false; | ||
``` | ||
42 changes: 42 additions & 0 deletions
42
...babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-import-as-local.tsx
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,42 @@ | ||
import { | ||
useEffect, | ||
useRef, | ||
// @ts-expect-error | ||
experimental_useEffectEvent as useEffectEvent, | ||
} from 'react'; | ||
|
||
let id = 0; | ||
function uniqueId() { | ||
'use no memo'; | ||
return id++; | ||
} | ||
|
||
export function useCustomHook(src: string): void { | ||
const uidRef = useRef(uniqueId()); | ||
const destroyed = useRef(false); | ||
const getItem = (srcName, uid) => { | ||
return {srcName, uid}; | ||
}; | ||
|
||
const getItemEvent = useEffectEvent(() => { | ||
if (destroyed.current) return; | ||
|
||
getItem(src, uidRef.current); | ||
}); | ||
|
||
useEffect(() => { | ||
destroyed.current = false; | ||
getItemEvent(); | ||
}, []); | ||
} | ||
|
||
function Component() { | ||
useCustomHook('hello'); | ||
return <div>Hello</div>; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
isComponent: true, | ||
params: [{x: 1}], | ||
}; |