-
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] Make ref enforcement on by default
Summary: The change earlier in this stack makes it less safe to have ref enforcement disabled. This diff enables it by default. ghstack-source-id: bb58c509352bb4a392a87188b10894fb1cf86eca Pull Request resolved: #30716
- Loading branch information
Showing
23 changed files
with
299 additions
and
470 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
69 changes: 0 additions & 69 deletions
69
...mpiler/src/__tests__/fixtures/compiler/capture-ref-for-later-mutation.expect.md
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
.../src/__tests__/fixtures/compiler/error.capture-ref-for-later-mutation.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,50 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {useRef} from 'react'; | ||
import {addOne} from 'shared-runtime'; | ||
|
||
function useKeyCommand() { | ||
const currentPosition = useRef(0); | ||
const handleKey = direction => () => { | ||
const position = currentPosition.current; | ||
const nextPosition = direction === 'left' ? addOne(position) : position; | ||
currentPosition.current = nextPosition; | ||
}; | ||
const moveLeft = { | ||
handler: handleKey('left'), | ||
}; | ||
const moveRight = { | ||
handler: handleKey('right'), | ||
}; | ||
return [moveLeft, moveRight]; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useKeyCommand, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
10 | }; | ||
11 | const moveLeft = { | ||
> 12 | handler: handleKey('left'), | ||
| ^^^^^^^^^ InvalidReact: This function accesses a ref value (the `current` property), which may not be accessed during render. (https://react.dev/reference/react/useRef). Function mutate? $90[9:17]:TObject<BuiltInFunction> accesses a ref (12:12) | ||
InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (12:12) | ||
InvalidReact: This function accesses a ref value (the `current` property), which may not be accessed during render. (https://react.dev/reference/react/useRef). Function mutate? $96[14:17]:TObject<BuiltInFunction> accesses a ref (15:15) | ||
InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (15:15) | ||
13 | }; | ||
14 | const moveRight = { | ||
15 | handler: handleKey('right'), | ||
``` | ||
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
...ompiler/src/__tests__/fixtures/compiler/error.repro-ref-mutable-range.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,40 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {Stringify, identity, mutate, CONST_TRUE} from 'shared-runtime'; | ||
|
||
function Foo(props, ref) { | ||
const value = {}; | ||
if (CONST_TRUE) { | ||
mutate(value); | ||
return <Stringify ref={ref} />; | ||
} | ||
mutate(value); | ||
if (CONST_TRUE) { | ||
return <Stringify ref={identity(ref)} />; | ||
} | ||
return value; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [{}, {current: 'fake-ref-object'}], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
9 | mutate(value); | ||
10 | if (CONST_TRUE) { | ||
> 11 | return <Stringify ref={identity(ref)} />; | ||
| ^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (11:11) | ||
12 | } | ||
13 | return value; | ||
14 | } | ||
``` | ||
File renamed without changes.
42 changes: 42 additions & 0 deletions
42
...r/error.useCallback-set-ref-nested-property-dont-preserve-memoization.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,42 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enablePreserveExistingMemoizationGuarantees:false | ||
import {useCallback, useRef} from 'react'; | ||
|
||
function Component(props) { | ||
const ref = useRef({inner: null}); | ||
|
||
const onChange = useCallback(event => { | ||
// The ref should still be mutable here even though function deps are frozen in | ||
// @enablePreserveExistingMemoizationGuarantees mode | ||
ref.current.inner = event.target.value; | ||
}); | ||
|
||
ref.current.inner = null; | ||
|
||
return <input onChange={onChange} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
11 | }); | ||
12 | | ||
> 13 | ref.current.inner = null; | ||
| ^^^^^^^^^^^^^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (13:13) | ||
14 | | ||
15 | return <input onChange={onChange} />; | ||
16 | } | ||
``` | ||
File renamed without changes.
70 changes: 0 additions & 70 deletions
70
...compiler/original-reactive-scopes-fork/capture-ref-for-later-mutation.expect.md
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
...er/original-reactive-scopes-fork/error.capture-ref-for-later-mutation.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,51 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enableReactiveScopesInHIR:false | ||
import {useRef} from 'react'; | ||
import {addOne} from 'shared-runtime'; | ||
|
||
function useKeyCommand() { | ||
const currentPosition = useRef(0); | ||
const handleKey = direction => () => { | ||
const position = currentPosition.current; | ||
const nextPosition = direction === 'left' ? addOne(position) : position; | ||
currentPosition.current = nextPosition; | ||
}; | ||
const moveLeft = { | ||
handler: handleKey('left'), | ||
}; | ||
const moveRight = { | ||
handler: handleKey('right'), | ||
}; | ||
return [moveLeft, moveRight]; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useKeyCommand, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
11 | }; | ||
12 | const moveLeft = { | ||
> 13 | handler: handleKey('left'), | ||
| ^^^^^^^^^ InvalidReact: This function accesses a ref value (the `current` property), which may not be accessed during render. (https://react.dev/reference/react/useRef). Function mutate? $90[9:17]:TObject<BuiltInFunction> accesses a ref (13:13) | ||
InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (13:13) | ||
InvalidReact: This function accesses a ref value (the `current` property), which may not be accessed during render. (https://react.dev/reference/react/useRef). Function mutate? $96[14:17]:TObject<BuiltInFunction> accesses a ref (16:16) | ||
InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (16:16) | ||
14 | }; | ||
15 | const moveRight = { | ||
16 | handler: handleKey('right'), | ||
``` | ||
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
...mpiler/preserve-memo-validation/error.maybe-mutable-ref-not-preserved.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,35 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees:true | ||
|
||
import {useRef, useMemo} from 'react'; | ||
import {makeArray} from 'shared-runtime'; | ||
|
||
function useFoo() { | ||
const r = useRef(); | ||
return useMemo(() => makeArray(r), []); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
6 | function useFoo() { | ||
7 | const r = useRef(); | ||
> 8 | return useMemo(() => makeArray(r), []); | ||
| ^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (8:8) | ||
9 | } | ||
10 | | ||
11 | export const FIXTURE_ENTRYPOINT = { | ||
``` | ||
File renamed without changes.
Oops, something went wrong.