From 3c9c0e38e451956e840db041cadca90956b81041 Mon Sep 17 00:00:00 2001 From: Eli White Date: Thu, 2 Jan 2025 11:00:54 -0800 Subject: [PATCH] Share ArrayTypeAnnotation between components and modules (#48318) Summary: These structures were the same, but the component side didn't use generics and just had duplicates. Making a base one to be shared. I need to follow up to this and constrain the types that components allow. Changelog: [Internal] Reviewed By: javache Differential Revision: D67371894 --- .../src/CodegenSchema.d.ts | 48 +++++++--------- .../react-native-codegen/src/CodegenSchema.js | 56 +++++++++---------- .../GeneratePropsJavaPojo/PojoCollector.js | 8 ++- 3 files changed, 50 insertions(+), 62 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index 112d04f6a075a5..31ed3d93e93b88 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -129,14 +129,9 @@ export type EventTypeAnnotation = | MixedTypeAnnotation | StringEnumTypeAnnotation | ObjectTypeAnnotation - | { - readonly type: 'ArrayTypeAnnotation'; - readonly elementType: EventTypeAnnotation - }; + | ArrayTypeAnnotation -export type ArrayTypeAnnotation = { - readonly type: 'ArrayTypeAnnotation'; - readonly elementType: +export type ComponentArrayTypeAnnotation = ArrayTypeAnnotation< | BooleanTypeAnnotation | StringTypeAnnotation | DoubleTypeAnnotation @@ -149,10 +144,12 @@ export type ArrayTypeAnnotation = { } | ObjectTypeAnnotation | ReservedPropTypeAnnotation - | { - readonly type: 'ArrayTypeAnnotation'; - readonly elementType: ObjectTypeAnnotation; - }; + | ArrayTypeAnnotation> +>; + +export interface ArrayTypeAnnotation { + readonly type: 'ArrayTypeAnnotation'; + readonly elementType: T; } export type PropTypeAnnotation = @@ -188,7 +185,7 @@ export type PropTypeAnnotation = } | ReservedPropTypeAnnotation | ObjectTypeAnnotation - | ArrayTypeAnnotation + | ComponentArrayTypeAnnotation | MixedTypeAnnotation; export interface ReservedPropTypeAnnotation { @@ -214,7 +211,7 @@ export type CommandParamTypeAnnotation = | DoubleTypeAnnotation | FloatTypeAnnotation | StringTypeAnnotation - | ArrayTypeAnnotation; + | ComponentArrayTypeAnnotation; export interface ReservedTypeAnnotation { readonly type: 'ReservedTypeAnnotation'; @@ -273,14 +270,12 @@ export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation< Nullable >; -export interface NativeModuleArrayTypeAnnotation> { - readonly type: 'ArrayTypeAnnotation'; - /** - * TODO(T72031674): Migrate all our NativeModule specs to not use - * invalid Array ElementTypes. Then, make the elementType required. - */ - readonly elementType: T | UnsafeAnyTypeAnnotation; -} +/** + * TODO(T72031674): Migrate all our NativeModule specs to not use + * invalid Array ElementTypes. Then, make the elementType required. + */ +interface NativeModuleArrayTypeAnnotation extends ArrayTypeAnnotation { } + export interface UnsafeAnyTypeAnnotation { readonly type: 'AnyTypeAnnotation', @@ -395,13 +390,10 @@ export type NativeModuleEventEmitterBaseTypeAnnotation = export type NativeModuleEventEmitterTypeAnnotation = | NativeModuleEventEmitterBaseTypeAnnotation - | { - readonly type: 'ArrayTypeAnnotation'; - readonly elementType: NativeModuleEventEmitterBaseTypeAnnotation; - }; + | ArrayTypeAnnotation; export type NativeModuleBaseTypeAnnotation = - | NativeModuleStringTypeAnnotation + NativeModuleStringTypeAnnotation | NativeModuleStringLiteralTypeAnnotation | NativeModuleStringLiteralUnionTypeAnnotation | NativeModuleNumberTypeAnnotation @@ -414,10 +406,10 @@ export type NativeModuleBaseTypeAnnotation = | NativeModuleGenericObjectTypeAnnotation | ReservedTypeAnnotation | NativeModuleTypeAliasTypeAnnotation - | NativeModuleArrayTypeAnnotation> | NativeModuleObjectTypeAnnotation | NativeModuleUnionTypeAnnotation - | NativeModuleMixedTypeAnnotation; + | NativeModuleMixedTypeAnnotation + | NativeModuleArrayTypeAnnotation; export type NativeModuleParamTypeAnnotation = | NativeModuleBaseTypeAnnotation diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 8bd4fc0a214d30..f3378136025f0b 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -145,30 +145,27 @@ export type EventTypeAnnotation = | MixedTypeAnnotation | StringEnumTypeAnnotation | ObjectTypeAnnotation + | ArrayTypeAnnotation; + +export type ComponentArrayTypeAnnotation = ArrayTypeAnnotation< + | BooleanTypeAnnotation + | StringTypeAnnotation + | DoubleTypeAnnotation + | FloatTypeAnnotation + | Int32TypeAnnotation | $ReadOnly<{ - type: 'ArrayTypeAnnotation', - elementType: EventTypeAnnotation, - }>; + type: 'StringEnumTypeAnnotation', + default: string, + options: $ReadOnlyArray, + }> + | ObjectTypeAnnotation + | ReservedPropTypeAnnotation + | ArrayTypeAnnotation>, +>; -export type ArrayTypeAnnotation = $ReadOnly<{ +export type ArrayTypeAnnotation<+T> = $ReadOnly<{ type: 'ArrayTypeAnnotation', - elementType: - | BooleanTypeAnnotation - | StringTypeAnnotation - | DoubleTypeAnnotation - | FloatTypeAnnotation - | Int32TypeAnnotation - | $ReadOnly<{ - type: 'StringEnumTypeAnnotation', - default: string, - options: $ReadOnlyArray, - }> - | ObjectTypeAnnotation - | ReservedPropTypeAnnotation - | $ReadOnly<{ - type: 'ArrayTypeAnnotation', - elementType: ObjectTypeAnnotation, - }>, + elementType: T, }>; export type PropTypeAnnotation = @@ -204,7 +201,7 @@ export type PropTypeAnnotation = }> | ReservedPropTypeAnnotation | ObjectTypeAnnotation - | ArrayTypeAnnotation + | ComponentArrayTypeAnnotation | MixedTypeAnnotation; export type ReservedPropTypeAnnotation = $ReadOnly<{ @@ -230,7 +227,7 @@ export type CommandParamTypeAnnotation = | DoubleTypeAnnotation | FloatTypeAnnotation | StringTypeAnnotation - | ArrayTypeAnnotation; + | ComponentArrayTypeAnnotation; export type ReservedTypeAnnotation = $ReadOnly<{ type: 'ReservedTypeAnnotation', @@ -292,14 +289,14 @@ export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation< export type NativeModuleArrayTypeAnnotation< +T: Nullable, -> = $ReadOnly<{ - type: 'ArrayTypeAnnotation', +> = ArrayTypeAnnotation< + | T /** * TODO(T72031674): Migrate all our NativeModule specs to not use * invalid Array ElementTypes. Then, make the elementType required. */ - elementType: T | UnsafeAnyTypeAnnotation, -}>; + | UnsafeAnyTypeAnnotation, +>; export type UnsafeAnyTypeAnnotation = { type: 'AnyTypeAnnotation', @@ -379,10 +376,7 @@ type NativeModuleEventEmitterBaseTypeAnnotation = export type NativeModuleEventEmitterTypeAnnotation = | NativeModuleEventEmitterBaseTypeAnnotation - | { - type: 'ArrayTypeAnnotation', - elementType: NativeModuleEventEmitterBaseTypeAnnotation, - }; + | ArrayTypeAnnotation; export type NativeModuleBaseTypeAnnotation = | StringTypeAnnotation diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/PojoCollector.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/PojoCollector.js index 6f5e19e88fd243..94ca45d8da9ba9 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/PojoCollector.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaPojo/PojoCollector.js @@ -11,8 +11,8 @@ 'use strict'; import type { - ArrayTypeAnnotation, BooleanTypeAnnotation, + ComponentArrayTypeAnnotation, DoubleTypeAnnotation, FloatTypeAnnotation, Int32TypeAnnotation, @@ -111,8 +111,10 @@ class PojoCollector { } case 'ArrayTypeAnnotation': { const arrayTypeAnnotation = typeAnnotation; - const elementType: $PropertyType = - arrayTypeAnnotation.elementType; + const elementType: $PropertyType< + ComponentArrayTypeAnnotation, + 'elementType', + > = arrayTypeAnnotation.elementType; const pojoElementType = (() => { switch (elementType.type) {