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
13 changes: 10 additions & 3 deletions packages/react-native-codegen/src/CodegenSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ export interface UnsafeAnyTypeAnnotation {
readonly type: 'AnyTypeAnnotation',
}

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

export interface NativeModuleStringTypeAnnotation {
readonly type: 'StringTypeAnnotation';
}
Expand Down Expand Up @@ -320,10 +325,10 @@ export interface NativeModuleBooleanTypeAnnotation {
readonly type: 'BooleanTypeAnnotation';
}

export type NativeModuleEnumMembers = readonly {
export type NativeModuleEnumMember = {
readonly name: string;
readonly value: string | number;
}[];
};

export type NativeModuleEnumMemberType =
| 'NumberTypeAnnotation'
Expand All @@ -339,7 +344,7 @@ export interface NativeModuleEnumDeclarationWithMembers {
name: string;
type: 'EnumDeclarationWithMembers';
memberType: NativeModuleEnumMemberType;
members: NativeModuleEnumMembers;
members: readonly NativeModuleEnumMember[];
}

export interface NativeModuleGenericObjectTypeAnnotation {
Expand Down Expand Up @@ -380,6 +385,7 @@ export type NativeModuleEventEmitterBaseTypeAnnotation =
| NativeModuleFloatTypeAnnotation
| NativeModuleInt32TypeAnnotation
| NativeModuleNumberTypeAnnotation
| NativeModuleNumberLiteralTypeAnnotation
| NativeModuleStringTypeAnnotation
| NativeModuleStringLiteralTypeAnnotation
| NativeModuleStringLiteralUnionTypeAnnotation
Expand All @@ -399,6 +405,7 @@ export type NativeModuleBaseTypeAnnotation =
| NativeModuleStringLiteralTypeAnnotation
| NativeModuleStringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NativeModuleNumberLiteralTypeAnnotation
| NativeModuleInt32TypeAnnotation
| NativeModuleDoubleTypeAnnotation
| NativeModuleFloatTypeAnnotation
Expand Down
19 changes: 12 additions & 7 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export type Int32TypeAnnotation = $ReadOnly<{
type: 'Int32TypeAnnotation',
}>;

export type NumberLiteralTypeAnnotation = $ReadOnly<{
type: 'NumberLiteralTypeAnnotation',
value: number,
}>;

export type StringTypeAnnotation = $ReadOnly<{
type: 'StringTypeAnnotation',
}>;
Expand Down Expand Up @@ -304,12 +309,10 @@ export type NativeModuleNumberTypeAnnotation = $ReadOnly<{
type: 'NumberTypeAnnotation',
}>;

export type NativeModuleEnumMembers = $ReadOnlyArray<
$ReadOnly<{
name: string,
value: string | number,
}>,
>;
export type NativeModuleEnumMember = {
name: string,
value: string | number,
};

export type NativeModuleEnumMemberType =
| 'NumberTypeAnnotation'
Expand All @@ -325,7 +328,7 @@ export type NativeModuleEnumDeclarationWithMembers = {
name: string,
type: 'EnumDeclarationWithMembers',
memberType: NativeModuleEnumMemberType,
members: NativeModuleEnumMembers,
members: $ReadOnlyArray<NativeModuleEnumMember>,
};

export type NativeModuleGenericObjectTypeAnnotation = $ReadOnly<{
Expand Down Expand Up @@ -366,6 +369,7 @@ type NativeModuleEventEmitterBaseTypeAnnotation =
| FloatTypeAnnotation
| Int32TypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| StringTypeAnnotation
| StringLiteralTypeAnnotation
| StringLiteralUnionTypeAnnotation
Expand All @@ -385,6 +389,7 @@ export type NativeModuleBaseTypeAnnotation =
| StringLiteralTypeAnnotation
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| Int32TypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ function serializeArg(
return wrap(val => `${val}.asNumber()`);
case 'Int32TypeAnnotation':
return wrap(val => `${val}.asNumber()`);
case 'NumberLiteralTypeAnnotation':
return wrap(val => `${val}.asNumber()`);
case 'ArrayTypeAnnotation':
return wrap(val => `${val}.asObject(rt).asArray(rt)`);
case 'FunctionTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
import type {
NativeModuleAliasMap,
NativeModuleEnumMap,
NativeModuleEnumMembers,
NativeModuleEnumMember,
NativeModuleEnumMemberType,
NativeModuleEventEmitterShape,
NativeModuleFunctionTypeAnnotation,
Expand Down Expand Up @@ -179,6 +179,8 @@ function translatePrimitiveJSTypeToCpp(
return wrapOptional('jsi::String', isRequired);
case 'NumberTypeAnnotation':
return wrapOptional('double', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapOptional('double', isRequired);
case 'DoubleTypeAnnotation':
return wrapOptional('double', isRequired);
case 'FloatTypeAnnotation':
Expand Down Expand Up @@ -407,7 +409,7 @@ struct Bridging<${enumName}> {
function generateEnum(
hasteModuleName: string,
origEnumName: string,
members: NativeModuleEnumMembers,
members: $ReadOnlyArray<NativeModuleEnumMember>,
memberType: NativeModuleEnumMemberType,
): string {
const enumName = getEnumName(hasteModuleName, origEnumName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function translateEventEmitterTypeToJavaType(
case 'StringLiteralUnionTypeAnnotation':
return 'String';
case 'NumberTypeAnnotation':
case 'NumberLiteralTypeAnnotation':
case 'FloatTypeAnnotation':
case 'DoubleTypeAnnotation':
case 'Int32TypeAnnotation':
Expand Down Expand Up @@ -203,6 +204,8 @@ function translateFunctionParamToJavaType(
return wrapOptional('String', isRequired);
case 'NumberTypeAnnotation':
return wrapOptional('double', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapOptional('double', isRequired);
case 'FloatTypeAnnotation':
return wrapOptional('double', isRequired);
case 'DoubleTypeAnnotation':
Expand Down Expand Up @@ -297,6 +300,8 @@ function translateFunctionReturnTypeToJavaType(
return wrapOptional('String', isRequired);
case 'NumberTypeAnnotation':
return wrapOptional('double', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapOptional('double', isRequired);
case 'FloatTypeAnnotation':
return wrapOptional('double', isRequired);
case 'DoubleTypeAnnotation':
Expand Down Expand Up @@ -373,6 +378,8 @@ function getFalsyReturnStatementFromReturnType(
return '';
case 'NumberTypeAnnotation':
return nullable ? 'return null;' : 'return 0;';
case 'NumberLiteralTypeAnnotation':
return nullable ? 'return null;' : 'return 0;';
case 'FloatTypeAnnotation':
return nullable ? 'return null;' : 'return 0.0;';
case 'DoubleTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ function translateReturnTypeToKind(
}
case 'NumberTypeAnnotation':
return 'NumberKind';
case 'NumberLiteralTypeAnnotation':
return 'NumberKind';
case 'DoubleTypeAnnotation':
return 'NumberKind';
case 'FloatTypeAnnotation':
Expand Down Expand Up @@ -280,6 +282,8 @@ function translateParamTypeToJniType(
}
case 'NumberTypeAnnotation':
return !isRequired ? 'Ljava/lang/Double;' : 'D';
case 'NumberLiteralTypeAnnotation':
return !isRequired ? 'Ljava/lang/Double;' : 'D';
case 'DoubleTypeAnnotation':
return !isRequired ? 'Ljava/lang/Double;' : 'D';
case 'FloatTypeAnnotation':
Expand Down Expand Up @@ -360,6 +364,8 @@ function translateReturnTypeToJniType(
}
case 'NumberTypeAnnotation':
return nullable ? 'Ljava/lang/Double;' : 'D';
case 'NumberLiteralTypeAnnotation':
return nullable ? 'Ljava/lang/Double;' : 'D';
case 'DoubleTypeAnnotation':
return nullable ? 'Ljava/lang/Double;' : 'D';
case 'FloatTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
NativeModuleObjectTypeAnnotation,
NativeModuleTypeAliasTypeAnnotation,
Nullable,
NumberLiteralTypeAnnotation,
ReservedTypeAnnotation,
StringLiteralTypeAnnotation,
StringLiteralUnionTypeAnnotation,
Expand Down Expand Up @@ -63,6 +64,7 @@ export type StructTypeAnnotation =
| StringLiteralTypeAnnotation
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| Int32TypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ function toObjCType(
return 'NSString *';
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'FloatTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'Int32TypeAnnotation':
Expand Down Expand Up @@ -183,6 +185,8 @@ function toObjCValue(
return value;
case 'NumberTypeAnnotation':
return wrapPrimitive('double');
case 'NumberLiteralTypeAnnotation':
return wrapPrimitive('double');
case 'FloatTypeAnnotation':
return wrapPrimitive('double');
case 'Int32TypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ function toObjCType(
return 'NSString *';
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'FloatTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'Int32TypeAnnotation':
Expand Down Expand Up @@ -173,6 +175,8 @@ function toObjCValue(
return RCTBridgingTo('String');
case 'NumberTypeAnnotation':
return RCTBridgingTo('Double');
case 'NumberLiteralTypeAnnotation':
return RCTBridgingTo('Double');
case 'FloatTypeAnnotation':
return RCTBridgingTo('Double');
case 'Int32TypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function getEventEmitterTypeObjCType(
case 'StringLiteralUnionTypeAnnotation':
return 'NSString *_Nonnull';
case 'NumberTypeAnnotation':
case 'NumberLiteralTypeAnnotation':
return 'NSNumber *_Nonnull';
case 'BooleanTypeAnnotation':
return 'BOOL';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ function getParamObjCType(
return notStruct(wrapOptional('NSString *', !nullable));
case 'NumberTypeAnnotation':
return notStruct(isRequired ? 'double' : 'NSNumber *');
case 'NumberLiteralTypeAnnotation':
return notStruct(isRequired ? 'double' : 'NSNumber *');
case 'FloatTypeAnnotation':
return notStruct(isRequired ? 'float' : 'NSNumber *');
case 'DoubleTypeAnnotation':
Expand Down Expand Up @@ -344,6 +346,8 @@ function getReturnObjCType(
return wrapOptional('NSString *', isRequired);
case 'NumberTypeAnnotation':
return wrapOptional('NSNumber *', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapOptional('NSNumber *', isRequired);
case 'FloatTypeAnnotation':
return wrapOptional('NSNumber *', isRequired);
case 'DoubleTypeAnnotation':
Expand Down Expand Up @@ -414,6 +418,8 @@ function getReturnJSType(
return 'StringKind';
case 'NumberTypeAnnotation':
return 'NumberKind';
case 'NumberLiteralTypeAnnotation':
return 'NumberKind';
case 'FloatTypeAnnotation':
return 'NumberKind';
case 'DoubleTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import * as TurboModuleRegistry from '../TurboModuleRegistry';
export interface Spec extends TurboModule {
+passBool?: (arg: boolean) => void;
+passNumber: (arg: number) => void;
+passNumberLiteral: (arg: 4) => void;
+passString: (arg: string) => void;
+passStringish: (arg: Stringish) => void;
+passStringLiteral: (arg: 'A String Literal') => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,26 @@ exports[`RN Codegen Flow Parser can generate fixture NATIVE_MODULE_WITH_BASIC_PA
]
}
},
{
'name': 'passNumberLiteral',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'VoidTypeAnnotation'
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'NumberLiteralTypeAnnotation',
'value': 4
}
}
]
}
},
{
'name': 'passString',
'optional': false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
emitCommonTypes,
emitDictionary,
emitFunction,
emitNumberLiteral,
emitPromise,
emitRootTag,
emitUnion,
Expand Down Expand Up @@ -243,6 +244,9 @@ function translateTypeAnnotation(
case 'UnionTypeAnnotation': {
return emitUnion(nullable, hasteModuleName, typeAnnotation, parser);
}
case 'NumberLiteralTypeAnnotation': {
return emitNumberLiteral(nullable, typeAnnotation.value);
}
case 'StringLiteralTypeAnnotation': {
return wrapNullable(nullable, {
type: 'StringLiteralTypeAnnotation',
Expand Down
6 changes: 4 additions & 2 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
NamedShape,
NativeModuleAliasMap,
NativeModuleEnumMap,
NativeModuleEnumMembers,
NativeModuleEnumMember,
NativeModuleEnumMemberType,
NativeModuleParamTypeAnnotation,
Nullable,
Expand Down Expand Up @@ -227,7 +227,9 @@ class FlowParser implements Parser {
});
}

parseEnumMembers(typeAnnotation: $FlowFixMe): NativeModuleEnumMembers {
parseEnumMembers(
typeAnnotation: $FlowFixMe,
): $ReadOnlyArray<NativeModuleEnumMember> {
return typeAnnotation.members.map(member => ({
name: member.id.name,
value: member.init?.value ?? member.id.name,
Expand Down
6 changes: 4 additions & 2 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
NamedShape,
NativeModuleAliasMap,
NativeModuleEnumMap,
NativeModuleEnumMembers,
NativeModuleEnumMember,
NativeModuleEnumMemberType,
NativeModuleParamTypeAnnotation,
Nullable,
Expand Down Expand Up @@ -240,7 +240,9 @@ export interface Parser {
/**
* Calculates enum's members
*/
parseEnumMembers(typeAnnotation: $FlowFixMe): NativeModuleEnumMembers;
parseEnumMembers(
typeAnnotation: $FlowFixMe,
): $ReadOnlyArray<NativeModuleEnumMember>;

/**
* Given a node, it returns true if it is a module interface
Expand Down
Loading