Skip to content
Open
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
56 changes: 47 additions & 9 deletions packages/react-native-codegen/src/CodegenSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,52 @@ export interface Int32TypeAnnotation {
readonly type: 'Int32TypeAnnotation';
}

export interface NumberLiteralTypeAnnotation {
readonly type: 'NumberLiteralTypeAnnotation';
readonly value: number;
}

export interface StringTypeAnnotation {
readonly type: 'StringTypeAnnotation';
}

export interface NumberTypeAnnotation {
readonly type: 'NumberTypeAnnotation';
}

export interface StringLiteralTypeAnnotation {
readonly type: 'StringLiteralTypeAnnotation';
readonly value: string;
}

export interface BooleanLiteralTypeAnnotation {
readonly type: 'BooleanLiteralTypeAnnotation';
readonly value: boolean;
}

export interface UnionTypeAnnotation<T> {
readonly type: 'UnionTypeAnnotation';
readonly types: readonly T[];
}

export type StringLiteralUnionTypeAnnotation =
UnionTypeAnnotation<StringLiteralTypeAnnotation>;

export type NumberLiteralUnionTypeAnnotation =
UnionTypeAnnotation<NumberLiteralTypeAnnotation>;

export type BooleanLiteralUnionTypeAnnotation =
UnionTypeAnnotation<BooleanLiteralTypeAnnotation>;

export interface VoidTypeAnnotation {
readonly type: 'VoidTypeAnnotation';
}

export interface TupleTypeAnnotation {
readonly type: 'TupleTypeAnnotation';
readonly types: StringLiteralTypeAnnotation | NumberLiteralTypeAnnotation;
}

export interface ObjectTypeAnnotation<T> {
readonly type: 'ObjectTypeAnnotation';
readonly properties: readonly NamedShape<T>[];
Expand Down Expand Up @@ -304,10 +342,6 @@ export interface NativeModuleStringLiteralTypeAnnotation {
readonly value: string;
}

export interface StringLiteralUnionTypeAnnotation {
readonly type: 'StringLiteralUnionTypeAnnotation';
readonly types: NativeModuleStringLiteralTypeAnnotation[];
}

export interface NativeModuleNumberTypeAnnotation {
readonly type: 'NumberTypeAnnotation';
Expand Down Expand Up @@ -369,14 +403,18 @@ export interface NativeModulePromiseTypeAnnotation {
readonly elementType: Nullable<NativeModuleBaseTypeAnnotation> | VoidTypeAnnotation;
}

export type UnionTypeAnnotationMemberType =
| 'NumberTypeAnnotation'
| 'ObjectTypeAnnotation'
| 'StringTypeAnnotation';
export type NativeModuleUnionTypeAnnotationMemberType =
| NativeModuleObjectTypeAnnotation
| StringLiteralTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| BooleanTypeAnnotation
| StringTypeAnnotation
| NumberTypeAnnotation;

export interface NativeModuleUnionTypeAnnotation {
readonly type: 'UnionTypeAnnotation';
readonly memberType: UnionTypeAnnotationMemberType;
readonly memberType: NativeModuleUnionTypeAnnotationMemberType;
}

export interface NativeModuleMixedTypeAnnotation {
Expand Down
48 changes: 37 additions & 11 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,29 @@ export type StringTypeAnnotation = $ReadOnly<{
type: 'StringTypeAnnotation',
}>;

export type NumberTypeAnnotation = $ReadOnly<{
type: 'NumberTypeAnnotation',
}>;

export type StringLiteralTypeAnnotation = $ReadOnly<{
type: 'StringLiteralTypeAnnotation',
value: string,
}>;

export type StringLiteralUnionTypeAnnotation = $ReadOnly<{
type: 'StringLiteralUnionTypeAnnotation',
types: $ReadOnlyArray<StringLiteralTypeAnnotation>,
export type BooleanLiteralTypeAnnotation = $ReadOnly<{
type: 'BooleanLiteralTypeAnnotation',
value: boolean,
}>;

export type StringLiteralUnionTypeAnnotation =
UnionTypeAnnotation<StringLiteralTypeAnnotation>;

export type NumberLiteralUnionTypeAnnotation =
UnionTypeAnnotation<NumberLiteralTypeAnnotation>;

export type BooleanLiteralUnionTypeAnnotation =
UnionTypeAnnotation<BooleanLiteralTypeAnnotation>;

export type VoidTypeAnnotation = $ReadOnly<{
type: 'VoidTypeAnnotation',
}>;
Expand All @@ -68,6 +81,16 @@ export type ObjectTypeAnnotation<+T> = $ReadOnly<{
baseTypes?: $ReadOnlyArray<string>,
}>;

export type UnionTypeAnnotation<+T> = $ReadOnly<{
type: 'UnionTypeAnnotation',
types: $ReadOnlyArray<T>,
}>;

export type TupleTypeAnnotation = $ReadOnly<{
type: 'TupleTypeAnnotation',
types: StringLiteralTypeAnnotation | NumberLiteralTypeAnnotation,
}>;

export type MixedTypeAnnotation = $ReadOnly<{
type: 'MixedTypeAnnotation',
}>;
Expand Down Expand Up @@ -358,15 +381,17 @@ export type NativeModulePromiseTypeAnnotation = $ReadOnly<{
elementType: VoidTypeAnnotation | Nullable<NativeModuleBaseTypeAnnotation>,
}>;

export type UnionTypeAnnotationMemberType =
| 'NumberTypeAnnotation'
| 'ObjectTypeAnnotation'
| 'StringTypeAnnotation';
export type NativeModuleUnionTypeAnnotationMemberType =
| NativeModuleObjectTypeAnnotation
| StringLiteralTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| BooleanTypeAnnotation
| StringTypeAnnotation
| NumberTypeAnnotation;

export type NativeModuleUnionTypeAnnotation = $ReadOnly<{
type: 'UnionTypeAnnotation',
memberType: UnionTypeAnnotationMemberType,
}>;
export type NativeModuleUnionTypeAnnotation =
UnionTypeAnnotation<NativeModuleUnionTypeAnnotationMemberType>;

export type NativeModuleMixedTypeAnnotation = $ReadOnly<{
type: 'MixedTypeAnnotation',
Expand Down Expand Up @@ -396,6 +421,7 @@ export type NativeModuleBaseTypeAnnotation =
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| Int32TypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
import type {
EventTypeAnnotation,
NamedShape,
NativeModuleUnionTypeAnnotation,
PropTypeAnnotation,
} from '../../CodegenSchema';

const {getEnumName, toSafeCppString} = require('../Utils');

const NumberTypes = ['NumberTypeAnnotation', 'NumberLiteralTypeAnnotation'];
const StringTypes = ['StringTypeAnnotation', 'StringLiteralTypeAnnotation'];
const ObjectTypes = ['ObjectTypeAnnotation'];
const BooleanTypes = ['BooleanTypeAnnotation', 'BooleanLiteralTypeAnnotation'];
const ValidTypes = [
...NumberTypes,
...ObjectTypes,
...StringTypes,
...BooleanTypes,
];

function toIntEnumValueName(propName: string, value: number): string {
return `${toSafeCppString(propName)}${value}`;
}
Expand Down Expand Up @@ -61,7 +73,47 @@ function getCppArrayTypeForAnnotation(
case 'Int32TypeAnnotation':
case 'MixedTypeAnnotation':
return `std::vector<${getCppTypeForAnnotation(typeElement.type)}>`;
case 'StringLiteralUnionTypeAnnotation':
case 'UnionTypeAnnotation':
const union: NativeModuleUnionTypeAnnotation = typeElement;
const isUnionOfType = (types: $ReadOnlyArray<string>): boolean => {
return union.types.every(memberTypeAnnotation =>
types.includes(memberTypeAnnotation.type),
);
};

if (isUnionOfType(NumberTypes)) {
return `std::vector<${getCppTypeForAnnotation('DoubleTypeAnnotation')}>`;
}

if (isUnionOfType(ObjectTypes)) {
if (!structParts) {
throw new Error(
`Trying to generate the event emitter for an Array of ${typeElement.type} without informations to generate the generic type`,
);
}
return `std::vector<${generateEventStructName(structParts)}>`;
}

if (isUnionOfType(StringTypes)) {
if (!structParts) {
throw new Error(
`Trying to generate the event emitter for an Array of ${typeElement.type} without informations to generate the generic type`,
);
}
return `std::vector<${generateEventStructName(structParts)}>`;
}

if (isUnionOfType(BooleanTypes)) {
return `std::vector<${getCppTypeForAnnotation('BooleanTypeAnnotation')}>`;
}

const invalidTypes = union.types.filter(member => {
return !ValidTypes.includes(member.type);
});

throw new Error(
`Unsupported union member types: ${invalidTypes.join(', ')}"`,
);
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 'StringLiteralUnionTypeAnnotation':
case 'UnionTypeAnnotation':
return setValueAtIndex(
propertyName,
indexVariable,
Expand Down Expand Up @@ -320,7 +320,7 @@ function generateSetters(
usingEvent,
prop => `jsi::valueFromDynamic(runtime, ${prop})`,
);
case 'StringLiteralUnionTypeAnnotation':
case 'UnionTypeAnnotation':
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 'StringLiteralUnionTypeAnnotation':
case 'UnionTypeAnnotation':
case 'ObjectTypeAnnotation':
return generateEventStructName([...nameParts, eventProperty.name]);
case 'ArrayTypeAnnotation':
Expand Down Expand Up @@ -188,7 +188,7 @@ function handleGenerateStructForArray(
nameParts.concat([name]),
nullthrows(elementType.properties),
);
} else if (elementType.type === 'StringLiteralUnionTypeAnnotation') {
} else if (elementType.type === 'UnionTypeAnnotation') {
generateEnum(
structs,
elementType.types.map(literal => literal.value),
Expand Down Expand Up @@ -251,7 +251,7 @@ function generateStruct(
nullthrows(typeAnnotation.properties),
);
return;
case 'StringLiteralUnionTypeAnnotation':
case 'UnionTypeAnnotation':
generateEnum(
structs,
typeAnnotation.types.map(literal => literal.value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ const EVENT_PROPS: SchemaType = {
typeAnnotation: {
type: 'ArrayTypeAnnotation',
elementType: {
type: 'StringLiteralUnionTypeAnnotation',
type: 'UnionTypeAnnotation',
types: [
{
type: 'StringLiteralTypeAnnotation',
Expand Down Expand Up @@ -1383,7 +1383,7 @@ const EVENT_PROPS: SchemaType = {
name: 'orientation',
optional: false,
typeAnnotation: {
type: 'StringLiteralUnionTypeAnnotation',
type: 'UnionTypeAnnotation',
types: [
{
type: 'StringLiteralTypeAnnotation',
Expand Down
Loading
Loading