From ed26e625dd459efa82b78f6ca5d616737b733ed6 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 01/11] 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 2d28bbfe7e271c..4f1ad89b87e107 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 99299dafc3c043..c5e154c5b7d84e 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 a70ecd8fae57fdb29a40b8367d70d39ac0fe77ee Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 02/11] 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 4f1ad89b87e107..fc68a17224480b 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 c5e154c5b7d84e..98cf7af93960ab 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 f74afa645e2eb91577d403be7804e858968c551e Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 03/11] 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 fc68a17224480b..24f82ad80b0c40 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 a8f887875587f2f916e452fdc3a9a87b026f7dec Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 04/11] 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 24f82ad80b0c40..a220ca10fa3e43 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 98cf7af93960ab..44286533e2a59a 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 8f4eceb0bf96a519f47fca014a9ad337b75b2398 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 05/11] 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 a220ca10fa3e43..88061b89dc6f4e 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 44286533e2a59a..2684864829d6a5 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 7126905ff3108edf50b93c19bec42ea8527af832 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 06/11] 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 88061b89dc6f4e..4f08147cffa278 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 2684864829d6a5..a40c239390851d 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 fa0951c8adab5cfc6d9df348c886a19ed1bca43b Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 07/11] 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 4f08147cffa278..d5ab6ef8669078 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 a40c239390851d..bc8f9cdcb78047 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 19cb720e2e782534c2651f20c786271a854876b9 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 08/11] 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 bc8f9cdcb78047..6a760cdd70d208 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 db243d9f3024a2..88ef558f40e0bc 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 a6b08b0d45484cf6188a8a7733897c6a5aa1b804 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 09/11] 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 d5ab6ef8669078..a1532bb7564a2b 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 6a760cdd70d208..2d63fff6cc8013 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 50205972dca1ce945909c571dfccb910744d4b7e Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 10/11] 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 a1532bb7564a2b..3c1ba4b9f4bb1d 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 2d63fff6cc8013..d94e410ee5f681 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 528474314a63da882f27fe3cc2e674e16296d385 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Wed, 19 Nov 2025 00:15:23 -0800 Subject: [PATCH 11/11] Modifying the Parser logic to handle Unions of different types and remove the special handling of StringLiteralUnionType 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 f151fd1e2f7248..4e25ed6495c5ef 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 7b22d8a0a5e97c..5ce04ca1139697 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 27097a0b0b6f09..3dd4b5bf5e4743 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),