From 0d8cfc6e73941a126a1839a6e0c42a4fd64db133 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 01/13] Adding NumberTypeAnnotation to Codegen Schema (#54586) Summary: Adding NumberTypeAnnotation to the Codegen Schema in order to obtain parity with String & Boolean which will be the Union types Changelog: [Internal] Differential Revision: D87374063 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 4 ++++ packages/react-native-codegen/src/CodegenSchema.js | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index 2d28bbfe7e27..4f1ad89b87e1 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -26,6 +26,10 @@ export interface FloatTypeAnnotation { readonly type: 'FloatTypeAnnotation'; } +export interface NumberTypeAnnotation { + readonly type: 'NumberTypeAnnotation'; +} + export interface BooleanTypeAnnotation { readonly type: 'BooleanTypeAnnotation'; } diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 99299dafc3c0..c5e154c5b7d8 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -30,6 +30,10 @@ export type FloatTypeAnnotation = $ReadOnly<{ type: 'FloatTypeAnnotation', }>; +export type NumberTypeAnnotation = $ReadOnly<{ + type: 'NumberTypeAnnotation', +}>; + export type BooleanTypeAnnotation = $ReadOnly<{ type: 'BooleanTypeAnnotation', }>; From 95662d9edf350f5e0b75b25c14c67273199a7c6a Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 02/13] Introduce generic UnionTypeAnnotation (#54587) Summary: Adding the generic type T UnionTypeAnnotation. This will be used later to create Unions of types String, Number & Boolean. Changelog: [Internal] Differential Revision: D87374445 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 5 +++++ packages/react-native-codegen/src/CodegenSchema.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index 4f1ad89b87e1..fc68a1722448 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -53,6 +53,11 @@ export interface ObjectTypeAnnotation { readonly baseTypes?: readonly string[] | undefined; } +export interface UnionTypeAnnotation { + readonly type: 'UnionTypeAnnotation'; + readonly types: readonly T[]; +} + export interface MixedTypeAnnotation { readonly type: 'MixedTypeAnnotation'; } diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index c5e154c5b7d8..98cf7af93960 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -72,6 +72,11 @@ export type ObjectTypeAnnotation<+T> = $ReadOnly<{ baseTypes?: $ReadOnlyArray, }>; +export type UnionTypeAnnotation<+T> = $ReadOnly<{ + type: 'UnionTypeAnnotation', + types: $ReadOnlyArray, +}>; + export type MixedTypeAnnotation = $ReadOnly<{ type: 'MixedTypeAnnotation', }>; From 7b0d0a60a0ba5d3142ed787efa7ddb5a86b26b94 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 03/13] Introduce NumberLiteralType & StringLiteralType in TS (#54588) Summary: Introduce NumberLiteralType & StringLiteralType in TypeScript just as they already exist in Flow here: https://www.internalfb.com/code/fbsource/[9b248afa0cd5548b81dd44f1042b230e6069432b]/xplat/js/react-native-github/packages/react-native-codegen/src/CodegenSchema.js?lines=41-53 Changelog: [Internal] Differential Revision: D87375511 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index fc68a1722448..24f82ad80b0c 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -46,6 +46,16 @@ export interface VoidTypeAnnotation { readonly type: 'VoidTypeAnnotation'; } +export interface NumberLiteralTypeAnnotation { + readonly type: 'NumberLiteralTypeAnnotation'; + readonly value: number; +} + +export interface StringLiteralTypeAnnotation { + readonly type: 'StringLiteralTypeAnnotation'; + readonly value: string; +} + export interface ObjectTypeAnnotation { readonly type: 'ObjectTypeAnnotation'; readonly properties: readonly NamedShape[]; From 23e2cbae34fa392fd5bad0c43fd59d1ff45f5465 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 04/13] Introduce TupleTypeAnnotation Summary: TupleTypeAnnotation is added for parity with UnionTypeAnnotation to support future implementation. Currently limited to String and Number literals as per: https://docs.google.com/document/d/1pTBMOEIov5n5-0L9z925XPvGX1YxlmI6n6FJvd0oXtE/edit?tab=t.0#heading=h.fhe5py9plytd Differential Revision: D87383455 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 7 +++++++ packages/react-native-codegen/src/CodegenSchema.js | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index 24f82ad80b0c..a220ca10fa3e 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -68,6 +68,13 @@ export interface UnionTypeAnnotation { readonly types: readonly T[]; } +// TODO(T72031674): TupleTypeAnnotation is added for parity with UnionTypeAnnotation +// to support future implementation. Currently limited to String and Number literals. +export interface TupleTypeAnnotation { + readonly type: 'TupleTypeAnnotation'; + readonly types: StringLiteralTypeAnnotation | NumberLiteralTypeAnnotation; +} + export interface MixedTypeAnnotation { readonly type: 'MixedTypeAnnotation'; } diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 98cf7af93960..44286533e2a5 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -77,6 +77,13 @@ export type UnionTypeAnnotation<+T> = $ReadOnly<{ types: $ReadOnlyArray, }>; +// TODO(T72031674): TupleTypeAnnotation is added for parity with UnionTypeAnnotation +// to support future implementation. Currently limited to String and Number literals. +export type TupleTypeAnnotation = $ReadOnly<{ + type: 'TupleTypeAnnotation', + types: StringLiteralTypeAnnotation | NumberLiteralTypeAnnotation, +}>; + export type MixedTypeAnnotation = $ReadOnly<{ type: 'MixedTypeAnnotation', }>; From e98089497906ddfd6372ed51c7e3b5196e75fdef Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 05/13] Introduce BooleanLiteralTypeAnnotation (#54590) Summary: Introduce `BooleanLiteralTypeAnnotation` in Flow & TypeScript to match the existing `StringLiteralTypeAnnotation` & `NumberLiteralTypeAnnotation` since Unions will be supporting Booleans along with String & Number Changelog: [Internal] Differential Revision: D87384473 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 5 +++++ packages/react-native-codegen/src/CodegenSchema.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index a220ca10fa3e..88061b89dc6f 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -56,6 +56,11 @@ export interface StringLiteralTypeAnnotation { readonly value: string; } +export interface BooleanLiteralTypeAnnotation { + readonly type: 'BooleanLiteralTypeAnnotation'; + readonly value: boolean; +} + export interface ObjectTypeAnnotation { readonly type: 'ObjectTypeAnnotation'; readonly properties: readonly NamedShape[]; diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 44286533e2a5..2684864829d6 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -56,6 +56,11 @@ export type StringLiteralTypeAnnotation = $ReadOnly<{ value: string, }>; +export type BooleanLiteralTypeAnnotation = $ReadOnly<{ + type: 'BooleanLiteralTypeAnnotation', + value: boolean, +}>; + export type StringLiteralUnionTypeAnnotation = $ReadOnly<{ type: 'StringLiteralUnionTypeAnnotation', types: $ReadOnlyArray, From 0b8f7d0d3b1ff908144b938b67acb4ee82a4065d Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 06/13] Introduce the supported member types of Union (#54591) Summary: Following types will be supported in Union currently: 1. Number : NumberType + NumberLiteralType 2. Boolean : BooleanType + BooleanLiteralType 3. String : StringType + StringLiteralType 4. Object: NativeModuleObjectType These are the only ones that exist today as per : https://docs.google.com/document/d/1pTBMOEIov5n5-0L9z925XPvGX1YxlmI6n6FJvd0oXtE/edit?tab=t.0#heading=h.fhe5py9plytd Changelog: [Internal] Differential Revision: D87384995 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 9 +++++++++ packages/react-native-codegen/src/CodegenSchema.js | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index 88061b89dc6f..4f08147cffa2 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -405,6 +405,15 @@ export type UnionTypeAnnotationMemberType = | 'ObjectTypeAnnotation' | 'StringTypeAnnotation'; +export type NativeModuleUnionTypeAnnotationMemberType = + | NativeModuleObjectTypeAnnotation + | StringLiteralTypeAnnotation + | NumberLiteralTypeAnnotation + | BooleanLiteralTypeAnnotation + | BooleanTypeAnnotation + | StringTypeAnnotation + | NumberTypeAnnotation; + export interface NativeModuleUnionTypeAnnotation { readonly type: 'UnionTypeAnnotation'; readonly memberType: UnionTypeAnnotationMemberType; diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 2684864829d6..a40c23939085 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -384,6 +384,15 @@ export type UnionTypeAnnotationMemberType = | 'ObjectTypeAnnotation' | 'StringTypeAnnotation'; +export type NativeModuleUnionTypeAnnotationMemberType = + | NativeModuleObjectTypeAnnotation + | StringLiteralTypeAnnotation + | NumberLiteralTypeAnnotation + | BooleanLiteralTypeAnnotation + | BooleanTypeAnnotation + | StringTypeAnnotation + | NumberTypeAnnotation; + export type NativeModuleUnionTypeAnnotation = $ReadOnly<{ type: 'UnionTypeAnnotation', memberType: UnionTypeAnnotationMemberType, From 13a1fc79e44dfd152ee4c6de00b38f6860aa98b3 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 07/13] Introduce UnionTypeAnnotation for Number & Boolean (#54592) Summary: Adding `NumberLiteralUnionTypeAnnotation` & `BooleanLiteralUnionTypeAnnotation` to Flow & TypeScript so that they are in parity with other Union type : `StringLiteralUnionTypeAnnotation`. Number & Boolean aren't used anywhere yet. Changelog: [Internal] Differential Revision: D87386144 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 6 ++++++ packages/react-native-codegen/src/CodegenSchema.js | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index 4f08147cffa2..d5ab6ef86690 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -340,6 +340,12 @@ export interface StringLiteralUnionTypeAnnotation { readonly types: NativeModuleStringLiteralTypeAnnotation[]; } +export type NumberLiteralUnionTypeAnnotation = + UnionTypeAnnotation; + +export type BooleanLiteralUnionTypeAnnotation = + UnionTypeAnnotation; + export interface NativeModuleNumberTypeAnnotation { readonly type: 'NumberTypeAnnotation'; } diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index a40c23939085..bc8f9cdcb780 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -66,6 +66,12 @@ export type StringLiteralUnionTypeAnnotation = $ReadOnly<{ types: $ReadOnlyArray, }>; +export type NumberLiteralUnionTypeAnnotation = + UnionTypeAnnotation; + +export type BooleanLiteralUnionTypeAnnotation = + UnionTypeAnnotation; + export type VoidTypeAnnotation = $ReadOnly<{ type: 'VoidTypeAnnotation', }>; From c93e4258d91ac99d918e901f3a4c23472017b426 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 08/13] Add BooleanLiteralTypeAnnotation to the NativeModuleBaseTypeAnnotation in Flow (#54593) Summary: Just as `NumberLiteralTypeAnnotation` was part of the `NativeModuleBaseTypeAnnotation` in Flow, adding the `BooleanLiteralTypeAnnotation`. Similarly adding it to the StructCollector since this is needed for Flow exhaustiveness check in generators/modules in D87410022 NOTE: Didn't add this change for TS as both `NumberLiteralTypeAnnotation` was not included as part of `NativeModuleBaseTypeAnnotation` in TS, also the generators were not failing in TS for this. Changelog: [Internal] Differential Revision: D87392274 --- packages/react-native-codegen/src/CodegenSchema.js | 1 + .../generators/modules/GenerateModuleObjCpp/StructCollector.js | 2 ++ 2 files changed, 3 insertions(+) diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index bc8f9cdcb780..6a760cdd70d2 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -432,6 +432,7 @@ export type NativeModuleBaseTypeAnnotation = | StringLiteralUnionTypeAnnotation | NativeModuleNumberTypeAnnotation | NumberLiteralTypeAnnotation + | BooleanLiteralTypeAnnotation | Int32TypeAnnotation | DoubleTypeAnnotation | FloatTypeAnnotation diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js index db243d9f3024..88ef558f40e0 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js @@ -11,6 +11,7 @@ 'use strict'; import type { + BooleanLiteralTypeAnnotation, BooleanTypeAnnotation, DoubleTypeAnnotation, FloatTypeAnnotation, @@ -65,6 +66,7 @@ export type StructTypeAnnotation = | StringLiteralUnionTypeAnnotation | NativeModuleNumberTypeAnnotation | NumberLiteralTypeAnnotation + | BooleanLiteralTypeAnnotation | Int32TypeAnnotation | DoubleTypeAnnotation | FloatTypeAnnotation From b120a5cda1a837b248b7492ee5565e206c331f3e Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 09/13] Refactor NativeModuleUnionTypeAnnotation (#54594) Summary: Refactoring `NativeModuleUnionTypeAnnotation` to use the newly introduced `NativeModuleUnionTypeAnnotationMemberType` Changelog: [Internal] Differential Revision: D87386775 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 11 ++--------- packages/react-native-codegen/src/CodegenSchema.js | 11 ++--------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index d5ab6ef86690..a1532bb7564a 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -406,11 +406,6 @@ export interface NativeModulePromiseTypeAnnotation { readonly elementType: Nullable | VoidTypeAnnotation; } -export type UnionTypeAnnotationMemberType = - | 'NumberTypeAnnotation' - | 'ObjectTypeAnnotation' - | 'StringTypeAnnotation'; - export type NativeModuleUnionTypeAnnotationMemberType = | NativeModuleObjectTypeAnnotation | StringLiteralTypeAnnotation @@ -420,10 +415,8 @@ export type NativeModuleUnionTypeAnnotationMemberType = | StringTypeAnnotation | NumberTypeAnnotation; -export interface NativeModuleUnionTypeAnnotation { - readonly type: 'UnionTypeAnnotation'; - readonly memberType: UnionTypeAnnotationMemberType; -} +export type NativeModuleUnionTypeAnnotation = + UnionTypeAnnotation; export interface NativeModuleMixedTypeAnnotation { readonly type: 'MixedTypeAnnotation'; diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 6a760cdd70d2..2d63fff6cc80 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -385,11 +385,6 @@ export type NativeModulePromiseTypeAnnotation = $ReadOnly<{ elementType: VoidTypeAnnotation | Nullable, }>; -export type UnionTypeAnnotationMemberType = - | 'NumberTypeAnnotation' - | 'ObjectTypeAnnotation' - | 'StringTypeAnnotation'; - export type NativeModuleUnionTypeAnnotationMemberType = | NativeModuleObjectTypeAnnotation | StringLiteralTypeAnnotation @@ -399,10 +394,8 @@ export type NativeModuleUnionTypeAnnotationMemberType = | StringTypeAnnotation | NumberTypeAnnotation; -export type NativeModuleUnionTypeAnnotation = $ReadOnly<{ - type: 'UnionTypeAnnotation', - memberType: UnionTypeAnnotationMemberType, -}>; +export type NativeModuleUnionTypeAnnotation = + UnionTypeAnnotation; export type NativeModuleMixedTypeAnnotation = $ReadOnly<{ type: 'MixedTypeAnnotation', From b06ae5551ac1bb4014a12e7f342d753f0a607de8 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 10/13] Un-special Case StringLiteralUnionTypeAnnotation (#54595) Summary: Now that UnionTypeAnnotation itself supports both the member types and value, we can fold the StringLiteralUnionTypeAnnotation into the UnionTypeAnnotation and un-special case the StringLiteralUnionTypeAnnotation Changelog: [Internal] Differential Revision: D87388948 --- packages/react-native-codegen/src/CodegenSchema.d.ts | 6 ++---- packages/react-native-codegen/src/CodegenSchema.js | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts index a1532bb7564a..3c1ba4b9f4bb 100644 --- a/packages/react-native-codegen/src/CodegenSchema.d.ts +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -335,10 +335,8 @@ export interface NativeModuleStringLiteralTypeAnnotation { readonly value: string; } -export interface StringLiteralUnionTypeAnnotation { - readonly type: 'StringLiteralUnionTypeAnnotation'; - readonly types: NativeModuleStringLiteralTypeAnnotation[]; -} +export type StringLiteralUnionTypeAnnotation = + UnionTypeAnnotation; export type NumberLiteralUnionTypeAnnotation = UnionTypeAnnotation; diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 2d63fff6cc80..d94e410ee5f6 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -61,10 +61,8 @@ export type BooleanLiteralTypeAnnotation = $ReadOnly<{ value: boolean, }>; -export type StringLiteralUnionTypeAnnotation = $ReadOnly<{ - type: 'StringLiteralUnionTypeAnnotation', - types: $ReadOnlyArray, -}>; +export type StringLiteralUnionTypeAnnotation = + UnionTypeAnnotation; export type NumberLiteralUnionTypeAnnotation = UnionTypeAnnotation; From 014656881814fef7d6b0a198177ad27d08b10a7b Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 11/13] Modifying the Parser logic to handle Unions of different types and remove the special handling of StringLiteralUnionType (#54596) Summary: - Getting rid of emitStringLiteralUnion logic in parser - Modifying the emitUnion logic in parser Changelog: [Internal] Differential Revision: D87412493 --- .../src/parsers/parser.js | 12 +-- .../src/parsers/parserMock.js | 10 +- .../src/parsers/parsers-primitives.js | 99 ++++++++----------- 3 files changed, 46 insertions(+), 75 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/parser.js b/packages/react-native-codegen/src/parsers/parser.js index f151fd1e2f72..4e25ed6495c5 100644 --- a/packages/react-native-codegen/src/parsers/parser.js +++ b/packages/react-native-codegen/src/parsers/parser.js @@ -18,10 +18,10 @@ import type { NativeModuleEnumMember, NativeModuleEnumMemberType, NativeModuleParamTypeAnnotation, + NativeModuleUnionTypeAnnotationMemberType, Nullable, PropTypeAnnotation, SchemaType, - UnionTypeAnnotationMemberType, } from '../CodegenSchema'; import type {ParserType} from './errors'; import type { @@ -146,15 +146,7 @@ export interface Parser { */ remapUnionTypeAnnotationMemberNames( types: $FlowFixMe, - ): UnionTypeAnnotationMemberType[]; - /** - * Given a union annotation members types, it returns an array of string literals. - * @parameter membersTypes: union annotation members types - * @returns: an array of string literals. - */ - getStringLiteralUnionTypeAnnotationStringLiterals( - types: $FlowFixMe, - ): string[]; + ): NativeModuleUnionTypeAnnotationMemberType[]; /** * Given the name of a file, it returns a Schema. * @parameter filename: the name of the file. diff --git a/packages/react-native-codegen/src/parsers/parserMock.js b/packages/react-native-codegen/src/parsers/parserMock.js index 7b22d8a0a5e9..5ce04ca11396 100644 --- a/packages/react-native-codegen/src/parsers/parserMock.js +++ b/packages/react-native-codegen/src/parsers/parserMock.js @@ -18,10 +18,10 @@ import type { NativeModuleEnumMember, NativeModuleEnumMemberType, NativeModuleParamTypeAnnotation, + NativeModuleUnionTypeAnnotationMemberType, Nullable, PropTypeAnnotation, SchemaType, - UnionTypeAnnotationMemberType, } from '../CodegenSchema'; import type {ParserType} from './errors'; import type { @@ -105,13 +105,7 @@ export class MockedParser implements Parser { remapUnionTypeAnnotationMemberNames( membersTypes: $FlowFixMe[], - ): UnionTypeAnnotationMemberType[] { - return []; - } - - getStringLiteralUnionTypeAnnotationStringLiterals( - membersTypes: $FlowFixMe[], - ): string[] { + ): NativeModuleUnionTypeAnnotationMemberType[] { return []; } diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 27097a0b0b6f..3dd4b5bf5e47 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -30,12 +30,12 @@ import type { NativeModuleTypeAliasTypeAnnotation, NativeModuleTypeAnnotation, NativeModuleUnionTypeAnnotation, + NativeModuleUnionTypeAnnotationMemberType, Nullable, NumberLiteralTypeAnnotation, ObjectTypeAnnotation, ReservedTypeAnnotation, StringLiteralTypeAnnotation, - StringLiteralUnionTypeAnnotation, StringTypeAnnotation, VoidTypeAnnotation, } from '../CodegenSchema'; @@ -51,11 +51,7 @@ const { throwIfPartialNotAnnotatingTypeParameter, throwIfPartialWithMoreParameter, } = require('./error-utils'); -const { - ParserError, - UnsupportedTypeAnnotationParserError, - UnsupportedUnionTypeAnnotationParserError, -} = require('./errors'); +const {ParserError, UnsupportedTypeAnnotationParserError} = require('./errors'); const { assertGenericTypeAnnotationHasExactlyOneTypeParameter, translateFunctionTypeAnnotation, @@ -420,61 +416,50 @@ function emitUnion( nullable: boolean, hasteModuleName: string, typeAnnotation: $FlowFixMe, + types: TypeDeclarationMap, + aliasMap: {...NativeModuleAliasMap}, + enumMap: {...NativeModuleEnumMap}, + tryParse: ParserErrorCapturer, + cxxOnly: boolean, + translateTypeAnnotation: $FlowFixMe, parser: Parser, -): Nullable< - NativeModuleUnionTypeAnnotation | StringLiteralUnionTypeAnnotation, -> { - // Get all the literals by type - // Verify they are all the same - // If string, persist as StringLiteralUnionType - // If number, persist as NumberTypeAnnotation (TODO: Number literal) - - const unionTypes = parser.remapUnionTypeAnnotationMemberNames( - typeAnnotation.types, +): Nullable { + const unparsedMemberTypes: $ReadOnlyArray<$FlowFixMe> = + (typeAnnotation.types: $ReadOnlyArray<$FlowFixMe>); + + const memberTypes = unparsedMemberTypes.map( + (memberType: $FlowFixMe): NativeModuleUnionTypeAnnotationMemberType => { + switch (memberType.type) { + case 'NumberLiteralTypeAnnotation': + case 'StringLiteralTypeAnnotation': + case 'BooleanLiteralTypeAnnotation': + case 'TSLiteralType': + case 'GenericTypeAnnotation': + case 'TSTypeLiteral': + case 'ObjectTypeAnnotation': + return translateTypeAnnotation( + hasteModuleName, + memberType, + types, + aliasMap, + enumMap, + tryParse, + cxxOnly, + parser, + ); + default: + throw new UnsupportedTypeAnnotationParserError( + hasteModuleName, + memberType, + parser.language(), + ); + } + }, ); - // Only support unionTypes of the same kind - if (unionTypes.length > 1) { - throw new UnsupportedUnionTypeAnnotationParserError( - hasteModuleName, - typeAnnotation, - unionTypes, - ); - } - - if (unionTypes[0] === 'StringTypeAnnotation') { - // Reprocess as a string literal union - return emitStringLiteralUnion( - nullable, - hasteModuleName, - typeAnnotation, - parser, - ); - } - return wrapNullable(nullable, { type: 'UnionTypeAnnotation', - memberType: unionTypes[0], - }); -} - -function emitStringLiteralUnion( - nullable: boolean, - hasteModuleName: string, - typeAnnotation: $FlowFixMe, - parser: Parser, -): Nullable { - const stringLiterals = - parser.getStringLiteralUnionTypeAnnotationStringLiterals( - typeAnnotation.types, - ); - - return wrapNullable(nullable, { - type: 'StringLiteralUnionTypeAnnotation', - types: stringLiterals.map(stringLiteral => ({ - type: 'StringLiteralTypeAnnotation', - value: stringLiteral, - })), + types: memberTypes, }); } @@ -752,7 +737,7 @@ function emitUnionProp( name, optional, typeAnnotation: { - type: 'StringLiteralUnionTypeAnnotation', + type: 'UnionTypeAnnotation', types: typeAnnotation.types.map(option => ({ type: 'StringLiteralTypeAnnotation', value: parser.getLiteralValue(option), From 318929600bcb207007d63f6dd55e15c20ccd9ac0 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 12/13] Flow Parser Changes (#54597) Summary: Flow Parser changes to accomodate the new emitUnion Changelog: [Internal] Differential Revision: D87412532 --- .../src/parsers/flow/components/events.js | 2 +- .../src/parsers/flow/modules/index.js | 13 ++++++++++++- .../react-native-codegen/src/parsers/flow/parser.js | 10 ++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/components/events.js b/packages/react-native-codegen/src/parsers/flow/components/events.js index ba900707aaae..c8f41e125c1b 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -115,7 +115,7 @@ function extractArrayElementType( }; case 'UnionTypeAnnotation': return { - type: 'StringLiteralUnionTypeAnnotation', + type: 'UnionTypeAnnotation', types: typeAnnotation.types.map(option => ({ type: 'StringLiteralTypeAnnotation', value: parser.getLiteralValue(option), diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 51972f39ae03..33d858d8ba8a 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -244,7 +244,18 @@ function translateTypeAnnotation( ); } case 'UnionTypeAnnotation': { - return emitUnion(nullable, hasteModuleName, typeAnnotation, parser); + return emitUnion( + nullable, + hasteModuleName, + typeAnnotation, + types, + aliasMap, + enumMap, + tryParse, + cxxOnly, + translateTypeAnnotation, + parser, + ); } case 'NumberLiteralTypeAnnotation': { return emitNumberLiteral(nullable, typeAnnotation.value); diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index c8224d4e44e7..d251d7976dbc 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -18,10 +18,10 @@ import type { NativeModuleEnumMember, NativeModuleEnumMemberType, NativeModuleParamTypeAnnotation, + NativeModuleUnionTypeAnnotationMemberType, Nullable, PropTypeAnnotation, SchemaType, - UnionTypeAnnotationMemberType, } from '../../CodegenSchema'; import type {ParserType} from '../errors'; import type { @@ -111,7 +111,7 @@ class FlowParser implements Parser { remapUnionTypeAnnotationMemberNames( membersTypes: $FlowFixMe[], - ): UnionTypeAnnotationMemberType[] { + ): NativeModuleUnionTypeAnnotationMemberType[] { const remapLiteral = (item: $FlowFixMe) => { return item.type .replace('NumberLiteralTypeAnnotation', 'NumberTypeAnnotation') @@ -121,12 +121,6 @@ class FlowParser implements Parser { return [...new Set(membersTypes.map(remapLiteral))]; } - getStringLiteralUnionTypeAnnotationStringLiterals( - membersTypes: $FlowFixMe[], - ): string[] { - return membersTypes.map((item: $FlowFixMe) => item.value); - } - parseFile(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); From 63b09e0b11084cf48c4d85c3f430b858181c136f Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:17:44 -0800 Subject: [PATCH 13/13] TypeScript Parser Changes Summary: TypeScript Parser changes to accomodate new emitUnion Changelog: [Internal] Differential Revision: D87412635 --- .../src/parsers/typescript/components/events.js | 2 +- .../src/parsers/typescript/modules/index.js | 13 ++++++++++++- .../src/parsers/typescript/parser.js | 10 ++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/typescript/components/events.js b/packages/react-native-codegen/src/parsers/typescript/components/events.js index 1a2e038756bd..9a97288a98f5 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -127,7 +127,7 @@ function extractArrayElementType( }; case 'TSUnionType': return { - type: 'StringLiteralUnionTypeAnnotation', + type: 'UnionTypeAnnotation', types: typeAnnotation.types.map(option => ({ type: 'StringLiteralTypeAnnotation', value: parser.getLiteralValue(option), diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 15f96966660d..56573e49645c 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -398,7 +398,18 @@ function translateTypeAnnotation( ); } case 'TSUnionType': { - return emitUnion(nullable, hasteModuleName, typeAnnotation, parser); + return emitUnion( + nullable, + hasteModuleName, + typeAnnotation, + types, + aliasMap, + enumMap, + tryParse, + cxxOnly, + translateTypeAnnotation, + parser, + ); } case 'TSLiteralType': { const literal = typeAnnotation.literal; diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index edbaf368b526..60416eb6a1b3 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -18,10 +18,10 @@ import type { NativeModuleEnumMember, NativeModuleEnumMemberType, NativeModuleParamTypeAnnotation, + NativeModuleUnionTypeAnnotationMemberType, Nullable, PropTypeAnnotation, SchemaType, - UnionTypeAnnotationMemberType, } from '../../CodegenSchema'; import type {ParserType} from '../errors'; import type { @@ -109,7 +109,7 @@ class TypeScriptParser implements Parser { remapUnionTypeAnnotationMemberNames( membersTypes: Array<$FlowFixMe>, - ): Array { + ): Array { const remapLiteral = (item: $FlowFixMe) => { return item.literal ? item.literal.type @@ -123,12 +123,6 @@ class TypeScriptParser implements Parser { return [...new Set(membersTypes.map(remapLiteral))]; } - getStringLiteralUnionTypeAnnotationStringLiterals( - membersTypes: Array<$FlowFixMe>, - ): Array { - return membersTypes.map((item: $FlowFixMe) => item.literal.value); - } - parseFile(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8');