mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Support ReadOnly/Readonly wrapped custom structs in C++ TurboModule codegen (#56051)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56051 When parsing custom struct types in RN TurboModule flow specs for C++ (cxxOnly) codegen, the `getObjectTypeAnnotations` function in `parsers-commons.js` silently dropped types wrapped in `$ReadOnly<{...}>` or `Readonly<{...}>`. The root cause: `parser.nextNodeForTypeAlias(value)` returns the raw right-hand side of a type alias. For `export type Foo = Readonly<{...}>`, this returns a `GenericTypeAnnotation` node (the `Readonly<...>` wrapper), not an `ObjectTypeAnnotation`. The existing guard check (`parent.type !== 'ObjectTypeAnnotation'`) then rejects the type, causing it to be silently skipped from the alias map. The fix unwraps `$ReadOnly`/`Readonly` `GenericTypeAnnotation` wrappers (Flow) and `Readonly` `TSTypeReference` wrappers (TypeScript) before performing the type check, then obtains properties directly from the unwrapped node. Also updates `NativeCxxModuleExample.js` to use `Readonly<{...}>` on `ConstantsStruct` as a demonstration and build-time validation of the fix. Changelog: [Internal] Reviewed By: vzaidman Differential Revision: D96044884 fbshipit-source-id: 811ecb878ce573a2250b9e8ac54345bac42c7e07
This commit is contained in:
1 parent
ebb2feed67
commit
321afe690a
2 files changed
+32
-20
No files matched your search
+30
-18
@@ -189,30 +189,42 @@ function getObjectTypeAnnotations(
|
||||
if (!isTypeAlias) {
|
||||
return;
|
||||
}
|
||||
const parent = parser.nextNodeForTypeAlias(value);
|
||||
let parent = parser.nextNodeForTypeAlias(value);
|
||||
// Unwrap $ReadOnly/Readonly wrappers to get the inner object type
|
||||
if (
|
||||
parent.type === 'GenericTypeAnnotation' &&
|
||||
(parent.id?.name === '$ReadOnly' || parent.id?.name === 'Readonly') &&
|
||||
parent.typeParameters?.params?.length === 1
|
||||
) {
|
||||
parent = parent.typeParameters.params[0];
|
||||
} else if (
|
||||
parent.type === 'TSTypeReference' &&
|
||||
parent.typeName?.name === 'Readonly' &&
|
||||
parent.typeParameters?.params?.length === 1
|
||||
) {
|
||||
parent = parent.typeParameters.params[0];
|
||||
}
|
||||
if (
|
||||
parent.type !== 'ObjectTypeAnnotation' &&
|
||||
parent.type !== 'TSTypeLiteral'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const typeProperties = parser
|
||||
.getAnnotatedElementProperties(value)
|
||||
.map(prop =>
|
||||
parseObjectProperty(
|
||||
parent,
|
||||
prop,
|
||||
hasteModuleName,
|
||||
types,
|
||||
aliasMap,
|
||||
{}, // enumMap
|
||||
tryParse,
|
||||
true, // cxxOnly
|
||||
prop?.optional || false,
|
||||
translateTypeAnnotation,
|
||||
parser,
|
||||
),
|
||||
);
|
||||
const typeProperties = (parent.properties ?? parent.members).map(prop =>
|
||||
parseObjectProperty(
|
||||
parent,
|
||||
prop,
|
||||
hasteModuleName,
|
||||
types,
|
||||
aliasMap,
|
||||
{}, // enumMap
|
||||
tryParse,
|
||||
true, // cxxOnly
|
||||
prop?.optional || false,
|
||||
translateTypeAnnotation,
|
||||
parser,
|
||||
),
|
||||
);
|
||||
aliasMap[key] = {
|
||||
type: 'ObjectTypeAnnotation',
|
||||
properties: typeProperties,
|
||||
|
||||
@@ -31,11 +31,11 @@ export type UnionFloat = 1.44 | 2.88 | 5.76;
|
||||
export type UnionString = 'One' | 'Two' | 'Three';
|
||||
export type UnionObject = {value: number} | {low: string};
|
||||
|
||||
export type ConstantsStruct = {
|
||||
export type ConstantsStruct = Readonly<{
|
||||
const1: boolean,
|
||||
const2: number,
|
||||
const3: string,
|
||||
};
|
||||
}>;
|
||||
|
||||
export type ObjectStruct = {
|
||||
a: number,
|
||||
|
||||
Reference in new issue
Block a user