forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Allow type cast expressions with refs (facebook#31871)
We report a false positive for the combination of a ref-accessing function placed inside an array which is they type-cast. Here we teach ref validation about type casts. I also tried other variants like `return ref as const` but those already worked. Closes facebook#31864
- Loading branch information
1 parent
de82912
commit 6a3d6a4
Showing
3 changed files
with
85 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
60 changes: 60 additions & 0 deletions
60
...ompiler/src/__tests__/fixtures/compiler/allow-ref-type-cast-in-render.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,60 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {useRef} from 'react'; | ||
|
||
function useArrayOfRef() { | ||
const ref = useRef(null); | ||
const callback = value => { | ||
ref.current = value; | ||
}; | ||
return [callback] as const; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: () => { | ||
useArrayOfRef(); | ||
return 'ok'; | ||
}, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useRef } from "react"; | ||
|
||
function useArrayOfRef() { | ||
const $ = _c(1); | ||
const ref = useRef(null); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
const callback = (value) => { | ||
ref.current = value; | ||
}; | ||
|
||
t0 = [callback]; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
return t0 as const; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: () => { | ||
useArrayOfRef(); | ||
return "ok"; | ||
}, | ||
|
||
params: [{}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) "ok" |
17 changes: 17 additions & 0 deletions
17
...el-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-type-cast-in-render.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,17 @@ | ||
import {useRef} from 'react'; | ||
|
||
function useArrayOfRef() { | ||
const ref = useRef(null); | ||
const callback = value => { | ||
ref.current = value; | ||
}; | ||
return [callback] as const; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: () => { | ||
useArrayOfRef(); | ||
return 'ok'; | ||
}, | ||
params: [{}], | ||
}; |