Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 24 additions & 37 deletions packages/react-native-codegen/src/CodegenSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ export interface StringTypeAnnotation {
readonly type: 'StringTypeAnnotation';
}

export interface StringEnumTypeAnnotation {
readonly type: 'StringEnumTypeAnnotation';
readonly options: readonly string[];
}

export interface VoidTypeAnnotation {
readonly type: 'VoidTypeAnnotation';
}
Expand Down Expand Up @@ -127,16 +122,11 @@ export type EventTypeAnnotation =
| FloatTypeAnnotation
| Int32TypeAnnotation
| MixedTypeAnnotation
| StringEnumTypeAnnotation
| StringLiteralUnionTypeAnnotation
| ObjectTypeAnnotation<EventTypeAnnotation>
| {
readonly type: 'ArrayTypeAnnotation';
readonly elementType: EventTypeAnnotation
};
| ArrayTypeAnnotation<EventTypeAnnotation>

export type ArrayTypeAnnotation = {
readonly type: 'ArrayTypeAnnotation';
readonly elementType:
export type ComponentArrayTypeAnnotation = ArrayTypeAnnotation<
| BooleanTypeAnnotation
| StringTypeAnnotation
| DoubleTypeAnnotation
Expand All @@ -149,10 +139,12 @@ export type ArrayTypeAnnotation = {
}
| ObjectTypeAnnotation<PropTypeAnnotation>
| ReservedPropTypeAnnotation
| {
readonly type: 'ArrayTypeAnnotation';
readonly elementType: ObjectTypeAnnotation<PropTypeAnnotation>;
};
| ArrayTypeAnnotation<ObjectTypeAnnotation<PropTypeAnnotation>>
>;

export interface ArrayTypeAnnotation<T> {
readonly type: 'ArrayTypeAnnotation';
readonly elementType: T;
}

export type PropTypeAnnotation =
Expand Down Expand Up @@ -188,7 +180,7 @@ export type PropTypeAnnotation =
}
| ReservedPropTypeAnnotation
| ObjectTypeAnnotation<PropTypeAnnotation>
| ArrayTypeAnnotation
| ComponentArrayTypeAnnotation
| MixedTypeAnnotation;

export interface ReservedPropTypeAnnotation {
Expand All @@ -214,7 +206,7 @@ export type CommandParamTypeAnnotation =
| DoubleTypeAnnotation
| FloatTypeAnnotation
| StringTypeAnnotation
| ArrayTypeAnnotation;
| ComponentArrayTypeAnnotation;

export interface ReservedTypeAnnotation {
readonly type: 'ReservedTypeAnnotation';
Expand Down Expand Up @@ -273,14 +265,12 @@ export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation<
Nullable<NativeModuleBaseTypeAnnotation>
>;

export interface NativeModuleArrayTypeAnnotation<T extends Nullable<NativeModuleBaseTypeAnnotation>> {
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<T> extends ArrayTypeAnnotation<T | UnsafeAnyTypeAnnotation> { }


export interface UnsafeAnyTypeAnnotation {
readonly type: 'AnyTypeAnnotation',
Expand All @@ -300,7 +290,7 @@ export interface NativeModuleStringLiteralTypeAnnotation {
readonly value: string;
}

export interface NativeModuleStringLiteralUnionTypeAnnotation {
export interface StringLiteralUnionTypeAnnotation {
readonly type: 'StringLiteralUnionTypeAnnotation';
readonly types: NativeModuleStringLiteralTypeAnnotation[];
}
Expand Down Expand Up @@ -388,22 +378,19 @@ export type NativeModuleEventEmitterBaseTypeAnnotation =
| NativeModuleNumberLiteralTypeAnnotation
| NativeModuleStringTypeAnnotation
| NativeModuleStringLiteralTypeAnnotation
| NativeModuleStringLiteralUnionTypeAnnotation
| StringLiteralUnionTypeAnnotation
| NativeModuleTypeAliasTypeAnnotation
| NativeModuleGenericObjectTypeAnnotation
| VoidTypeAnnotation;

export type NativeModuleEventEmitterTypeAnnotation =
| NativeModuleEventEmitterBaseTypeAnnotation
| {
readonly type: 'ArrayTypeAnnotation';
readonly elementType: NativeModuleEventEmitterBaseTypeAnnotation;
};
| ArrayTypeAnnotation<NativeModuleEventEmitterBaseTypeAnnotation>;

export type NativeModuleBaseTypeAnnotation =
| NativeModuleStringTypeAnnotation
NativeModuleStringTypeAnnotation
| NativeModuleStringLiteralTypeAnnotation
| NativeModuleStringLiteralUnionTypeAnnotation
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NativeModuleNumberLiteralTypeAnnotation
| NativeModuleInt32TypeAnnotation
Expand All @@ -414,10 +401,10 @@ export type NativeModuleBaseTypeAnnotation =
| NativeModuleGenericObjectTypeAnnotation
| ReservedTypeAnnotation
| NativeModuleTypeAliasTypeAnnotation
| NativeModuleArrayTypeAnnotation<Nullable<NativeModuleBaseTypeAnnotation>>
| NativeModuleObjectTypeAnnotation
| NativeModuleUnionTypeAnnotation
| NativeModuleMixedTypeAnnotation;
| NativeModuleMixedTypeAnnotation
| NativeModuleArrayTypeAnnotation<NativeModuleBaseTypeAnnotation>;

export type NativeModuleParamTypeAnnotation =
| NativeModuleBaseTypeAnnotation
Expand Down
63 changes: 26 additions & 37 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ export type StringLiteralUnionTypeAnnotation = $ReadOnly<{
types: $ReadOnlyArray<StringLiteralTypeAnnotation>,
}>;

export type StringEnumTypeAnnotation = $ReadOnly<{
type: 'StringEnumTypeAnnotation',
options: $ReadOnlyArray<string>,
}>;

export type VoidTypeAnnotation = $ReadOnly<{
type: 'VoidTypeAnnotation',
}>;
Expand Down Expand Up @@ -143,32 +138,29 @@ export type EventTypeAnnotation =
| FloatTypeAnnotation
| Int32TypeAnnotation
| MixedTypeAnnotation
| StringEnumTypeAnnotation
| StringLiteralUnionTypeAnnotation
| ObjectTypeAnnotation<EventTypeAnnotation>
| ArrayTypeAnnotation<EventTypeAnnotation>;

export type ComponentArrayTypeAnnotation = ArrayTypeAnnotation<
| BooleanTypeAnnotation
| StringTypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
| Int32TypeAnnotation
| $ReadOnly<{
type: 'ArrayTypeAnnotation',
elementType: EventTypeAnnotation,
}>;
type: 'StringEnumTypeAnnotation',
default: string,
options: $ReadOnlyArray<string>,
}>
| ObjectTypeAnnotation<PropTypeAnnotation>
| ReservedPropTypeAnnotation
| ArrayTypeAnnotation<ObjectTypeAnnotation<PropTypeAnnotation>>,
>;

export type ArrayTypeAnnotation = $ReadOnly<{
export type ArrayTypeAnnotation<+T> = $ReadOnly<{
type: 'ArrayTypeAnnotation',
elementType:
| BooleanTypeAnnotation
| StringTypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
| Int32TypeAnnotation
| $ReadOnly<{
type: 'StringEnumTypeAnnotation',
default: string,
options: $ReadOnlyArray<string>,
}>
| ObjectTypeAnnotation<PropTypeAnnotation>
| ReservedPropTypeAnnotation
| $ReadOnly<{
type: 'ArrayTypeAnnotation',
elementType: ObjectTypeAnnotation<PropTypeAnnotation>,
}>,
elementType: T,
}>;

export type PropTypeAnnotation =
Expand Down Expand Up @@ -204,7 +196,7 @@ export type PropTypeAnnotation =
}>
| ReservedPropTypeAnnotation
| ObjectTypeAnnotation<PropTypeAnnotation>
| ArrayTypeAnnotation
| ComponentArrayTypeAnnotation
| MixedTypeAnnotation;

export type ReservedPropTypeAnnotation = $ReadOnly<{
Expand All @@ -230,7 +222,7 @@ export type CommandParamTypeAnnotation =
| DoubleTypeAnnotation
| FloatTypeAnnotation
| StringTypeAnnotation
| ArrayTypeAnnotation;
| ComponentArrayTypeAnnotation;

export type ReservedTypeAnnotation = $ReadOnly<{
type: 'ReservedTypeAnnotation',
Expand Down Expand Up @@ -292,14 +284,14 @@ export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation<

export type NativeModuleArrayTypeAnnotation<
+T: Nullable<NativeModuleBaseTypeAnnotation>,
> = $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',
Expand Down Expand Up @@ -379,10 +371,7 @@ type NativeModuleEventEmitterBaseTypeAnnotation =

export type NativeModuleEventEmitterTypeAnnotation =
| NativeModuleEventEmitterBaseTypeAnnotation
| {
type: 'ArrayTypeAnnotation',
elementType: NativeModuleEventEmitterBaseTypeAnnotation,
};
| ArrayTypeAnnotation<NativeModuleEventEmitterBaseTypeAnnotation>;

export type NativeModuleBaseTypeAnnotation =
| StringTypeAnnotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function getCppArrayTypeForAnnotation(
case 'Int32TypeAnnotation':
case 'MixedTypeAnnotation':
return `std::vector<${getCppTypeForAnnotation(typeElement.type)}>`;
case 'StringEnumTypeAnnotation':
case 'StringLiteralUnionTypeAnnotation':
case 'ObjectTypeAnnotation':
if (!structParts) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function handleArrayElementType(
loopLocalVariable,
val => `jsi::valueFromDynamic(runtime, ${val})`,
);
case 'StringEnumTypeAnnotation':
case 'StringLiteralUnionTypeAnnotation':
return setValueAtIndex(
propertyName,
indexVariable,
Expand Down Expand Up @@ -320,7 +320,7 @@ function generateSetters(
usingEvent,
prop => `jsi::valueFromDynamic(runtime, ${prop})`,
);
case 'StringEnumTypeAnnotation':
case 'StringLiteralUnionTypeAnnotation':
return generateSetter(
parentPropertyName,
eventProperty.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function getNativeTypeFromAnnotation(
case 'FloatTypeAnnotation':
case 'MixedTypeAnnotation':
return getCppTypeForAnnotation(type);
case 'StringEnumTypeAnnotation':
case 'StringLiteralUnionTypeAnnotation':
case 'ObjectTypeAnnotation':
return generateEventStructName([...nameParts, eventProperty.name]);
case 'ArrayTypeAnnotation':
Expand Down Expand Up @@ -188,8 +188,12 @@ function handleGenerateStructForArray(
nameParts.concat([name]),
nullthrows(elementType.properties),
);
} else if (elementType.type === 'StringEnumTypeAnnotation') {
generateEnum(structs, elementType.options, nameParts.concat([name]));
} else if (elementType.type === 'StringLiteralUnionTypeAnnotation') {
generateEnum(
structs,
elementType.types.map(literal => literal.value),
nameParts.concat([name]),
);
} else if (elementType.type === 'ArrayTypeAnnotation') {
handleGenerateStructForArray(
structs,
Expand Down Expand Up @@ -247,8 +251,12 @@ function generateStruct(
nullthrows(typeAnnotation.properties),
);
return;
case 'StringEnumTypeAnnotation':
generateEnum(structs, typeAnnotation.options, nameParts.concat([name]));
case 'StringLiteralUnionTypeAnnotation':
generateEnum(
structs,
typeAnnotation.types.map(literal => literal.value),
nameParts.concat([name]),
);
return;
default:
(typeAnnotation.type: empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
'use strict';

import type {
ArrayTypeAnnotation,
BooleanTypeAnnotation,
ComponentArrayTypeAnnotation,
DoubleTypeAnnotation,
FloatTypeAnnotation,
Int32TypeAnnotation,
Expand Down Expand Up @@ -111,8 +111,10 @@ class PojoCollector {
}
case 'ArrayTypeAnnotation': {
const arrayTypeAnnotation = typeAnnotation;
const elementType: $PropertyType<ArrayTypeAnnotation, 'elementType'> =
arrayTypeAnnotation.elementType;
const elementType: $PropertyType<
ComponentArrayTypeAnnotation,
'elementType',
> = arrayTypeAnnotation.elementType;

const pojoElementType = (() => {
switch (elementType.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,8 +1263,17 @@ const EVENT_PROPS: SchemaType = {
typeAnnotation: {
type: 'ArrayTypeAnnotation',
elementType: {
type: 'StringEnumTypeAnnotation',
options: ['YES', 'NO'],
type: 'StringLiteralUnionTypeAnnotation',
types: [
{
type: 'StringLiteralTypeAnnotation',
value: 'YES',
},
{
type: 'StringLiteralTypeAnnotation',
value: 'NO',
},
],
},
},
},
Expand Down Expand Up @@ -1364,8 +1373,17 @@ const EVENT_PROPS: SchemaType = {
name: 'orientation',
optional: false,
typeAnnotation: {
type: 'StringEnumTypeAnnotation',
options: ['landscape', 'portrait'],
type: 'StringLiteralUnionTypeAnnotation',
types: [
{
type: 'StringLiteralTypeAnnotation',
value: 'landscape',
},
{
type: 'StringLiteralTypeAnnotation',
value: 'portrait',
},
],
},
},
],
Expand Down
Loading
Loading