-
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][ez] Fix reanimated custom type defs for imports (#31137)
When we added support for Reanimated, we didn't distinguish between true globals (i.e. identifiers with no static resolutions), module types, and imports #29188. For the past 3-4 months, Reanimated imports were not being matched to the correct hook / function shape we match globals and module imports against two different registries. This PR fixes our support for Reanimated library functions imported under `react-native-reanimated`. See test fixtures for details
- Loading branch information
Showing
9 changed files
with
149 additions
and
16 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
36 changes: 36 additions & 0 deletions
36
...es/compiler/error.invalid-non-imported-reanimated-shared-value-writes.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,36 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enableCustomTypeDefinitionForReanimated | ||
|
||
/** | ||
* Test that a global (i.e. non-imported) useSharedValue is treated as an | ||
* unknown hook. | ||
*/ | ||
function SomeComponent() { | ||
const sharedVal = useSharedValue(0); | ||
return ( | ||
<Button | ||
onPress={() => (sharedVal.value = Math.random())} | ||
title="Randomize" | ||
/> | ||
); | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
9 | return ( | ||
10 | <Button | ||
> 11 | onPress={() => (sharedVal.value = Math.random())} | ||
| ^^^^^^^^^ InvalidReact: Mutating a value returned from a function whose return value should not be mutated. Found mutation of `sharedVal` (11:11) | ||
12 | title="Randomize" | ||
13 | /> | ||
14 | ); | ||
``` | ||
15 changes: 15 additions & 0 deletions
15
...__tests__/fixtures/compiler/error.invalid-non-imported-reanimated-shared-value-writes.jsx
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,15 @@ | ||
// @enableCustomTypeDefinitionForReanimated | ||
|
||
/** | ||
* Test that a global (i.e. non-imported) useSharedValue is treated as an | ||
* unknown hook. | ||
*/ | ||
function SomeComponent() { | ||
const sharedVal = useSharedValue(0); | ||
return ( | ||
<Button | ||
onPress={() => (sharedVal.value = Math.random())} | ||
title="Randomize" | ||
/> | ||
); | ||
} |
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
1 change: 1 addition & 0 deletions
1
...ges/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reanimated-no-memo-arg.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
57 changes: 57 additions & 0 deletions
57
...mpiler/src/__tests__/fixtures/compiler/reanimated-shared-value-writes.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,57 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enableCustomTypeDefinitionForReanimated | ||
import {useSharedValue} from 'react-native-reanimated'; | ||
|
||
/** | ||
* https://docs.swmansion.com/react-native-reanimated/docs/2.x/api/hooks/useSharedValue/ | ||
* | ||
* Test that shared values are treated as ref-like, i.e. allowing writes outside | ||
* of render | ||
*/ | ||
function SomeComponent() { | ||
const sharedVal = useSharedValue(0); | ||
return ( | ||
<Button | ||
onPress={() => (sharedVal.value = Math.random())} | ||
title="Randomize" | ||
/> | ||
); | ||
} | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @enableCustomTypeDefinitionForReanimated | ||
import { useSharedValue } from "react-native-reanimated"; | ||
|
||
/** | ||
* https://docs.swmansion.com/react-native-reanimated/docs/2.x/api/hooks/useSharedValue/ | ||
* | ||
* Test that shared values are treated as ref-like, i.e. allowing writes outside | ||
* of render | ||
*/ | ||
function SomeComponent() { | ||
const $ = _c(3); | ||
const sharedVal = useSharedValue(0); | ||
|
||
const T0 = Button; | ||
const t0 = () => (sharedVal.value = Math.random()); | ||
let t1; | ||
if ($[0] !== T0 || $[1] !== t0) { | ||
t1 = <T0 onPress={t0} title="Randomize" />; | ||
$[0] = T0; | ||
$[1] = t0; | ||
$[2] = t1; | ||
} else { | ||
t1 = $[2]; | ||
} | ||
return t1; | ||
} | ||
|
||
``` | ||
18 changes: 18 additions & 0 deletions
18
...-plugin-react-compiler/src/__tests__/fixtures/compiler/reanimated-shared-value-writes.jsx
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 @@ | ||
// @enableCustomTypeDefinitionForReanimated | ||
import {useSharedValue} from 'react-native-reanimated'; | ||
|
||
/** | ||
* https://docs.swmansion.com/react-native-reanimated/docs/2.x/api/hooks/useSharedValue/ | ||
* | ||
* Test that shared values are treated as ref-like, i.e. allowing writes outside | ||
* of render | ||
*/ | ||
function SomeComponent() { | ||
const sharedVal = useSharedValue(0); | ||
return ( | ||
<Button | ||
onPress={() => (sharedVal.value = Math.random())} | ||
title="Randomize" | ||
/> | ||
); | ||
} |
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