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
16 changes: 16 additions & 0 deletions packages/react-native-codegen/src/CodegenSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export interface VoidTypeAnnotation {
readonly type: 'VoidTypeAnnotation';
}

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

export interface ObjectTypeAnnotation<T> {
readonly type: 'ObjectTypeAnnotation';
readonly properties: readonly NamedShape<T>[];
Expand Down Expand Up @@ -383,6 +388,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;
Expand All @@ -399,6 +413,7 @@ export type NativeModuleEventEmitterBaseTypeAnnotation =
| NativeModuleInt32TypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| NativeModuleStringTypeAnnotation
| StringLiteralTypeAnnotation
| StringLiteralUnionTypeAnnotation
Expand All @@ -416,6 +431,7 @@ export type NativeModuleBaseTypeAnnotation =
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| NativeModuleInt32TypeAnnotation
| NativeModuleDoubleTypeAnnotation
| NativeModuleFloatTypeAnnotation
Expand Down
16 changes: 16 additions & 0 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringLiteralTypeAnnotation>,
Expand Down Expand Up @@ -372,6 +377,15 @@ export type UnionTypeAnnotationMemberType =
| 'ObjectTypeAnnotation'
| 'StringTypeAnnotation';

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

export type NativeModuleUnionTypeAnnotation = $ReadOnly<{
type: 'UnionTypeAnnotation',
memberType: UnionTypeAnnotationMemberType,
Expand All @@ -388,6 +402,7 @@ type NativeModuleEventEmitterBaseTypeAnnotation =
| Int32TypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| StringTypeAnnotation
| StringLiteralTypeAnnotation
| StringLiteralUnionTypeAnnotation
Expand All @@ -405,6 +420,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 @@ -96,6 +96,8 @@ function serializeArg(
return wrap(val => `${val}.asString(rt)`);
case 'BooleanTypeAnnotation':
return wrap(val => `${val}.asBool()`);
case 'BooleanLiteralTypeAnnotation':
return wrap(val => `${val}.asBool()`);
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -265,6 +267,8 @@ function translatePrimitiveJSTypeToCpp(
return wrapOptional('int', isRequired);
case 'BooleanTypeAnnotation':
return wrapOptional('bool', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapOptional('bool', isRequired);
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ function translateEventEmitterTypeToJavaType(
case 'Int32TypeAnnotation':
return 'double';
case 'BooleanTypeAnnotation':
case 'BooleanLiteralTypeAnnotation':
return 'boolean';
case 'GenericObjectTypeAnnotation':
case 'ObjectTypeAnnotation':
Expand Down Expand Up @@ -214,6 +215,8 @@ function translateFunctionParamToJavaType(
return wrapOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapOptional('boolean', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapOptional('boolean', isRequired);
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -310,6 +313,8 @@ function translateFunctionReturnTypeToJavaType(
return wrapOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapOptional('boolean', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapOptional('boolean', isRequired);
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -388,6 +393,8 @@ function getFalsyReturnStatementFromReturnType(
return nullable ? 'return null;' : 'return 0;';
case 'BooleanTypeAnnotation':
return nullable ? 'return null;' : 'return false;';
case 'BooleanLiteralTypeAnnotation':
return nullable ? 'return null;' : 'return false;';
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ function translateReturnTypeToKind(
return 'StringKind';
case 'BooleanTypeAnnotation':
return 'BooleanKind';
case 'BooleanLiteralTypeAnnotation':
return 'BooleanKind';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -256,6 +258,8 @@ function translateParamTypeToJniType(
return 'Ljava/lang/String;';
case 'BooleanTypeAnnotation':
return !isRequired ? 'Ljava/lang/Boolean;' : 'Z';
case 'BooleanLiteralTypeAnnotation':
return !isRequired ? 'Ljava/lang/Boolean;' : 'Z';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -338,6 +342,8 @@ function translateReturnTypeToJniType(
return 'Ljava/lang/String;';
case 'BooleanTypeAnnotation':
return nullable ? 'Ljava/lang/Boolean;' : 'Z';
case 'BooleanLiteralTypeAnnotation':
return nullable ? 'Ljava/lang/Boolean;' : 'Z';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

import type {
BooleanLiteralTypeAnnotation,
BooleanTypeAnnotation,
DoubleTypeAnnotation,
FloatTypeAnnotation,
Expand Down Expand Up @@ -65,6 +66,7 @@ export type StructTypeAnnotation =
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| Int32TypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ function toObjCType(
return wrapCxxOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -195,6 +197,8 @@ function toObjCValue(
return wrapPrimitive('double');
case 'BooleanTypeAnnotation':
return wrapPrimitive('BOOL');
case 'BooleanLiteralTypeAnnotation':
return wrapPrimitive('BOOL');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ function toObjCType(
return wrapCxxOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -185,6 +187,8 @@ function toObjCValue(
return RCTBridgingTo('Double');
case 'BooleanTypeAnnotation':
return RCTBridgingTo('Bool');
case 'BooleanLiteralTypeAnnotation':
return RCTBridgingTo('Bool');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function getEventEmitterTypeObjCType(
case 'NumberLiteralTypeAnnotation':
return 'NSNumber *_Nonnull';
case 'BooleanTypeAnnotation':
case 'BooleanLiteralTypeAnnotation':
return 'BOOL';
case 'GenericObjectTypeAnnotation':
case 'ObjectTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ function getParamObjCType(
return notStruct(isRequired ? 'NSInteger' : 'NSNumber *');
case 'BooleanTypeAnnotation':
return notStruct(isRequired ? 'BOOL' : 'NSNumber *');
case 'BooleanLiteralTypeAnnotation':
return notStruct(isRequired ? 'BOOL' : 'NSNumber *');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -356,6 +358,8 @@ function getReturnObjCType(
return wrapOptional('NSNumber *', isRequired);
case 'BooleanTypeAnnotation':
return wrapOptional('NSNumber *', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapOptional('NSNumber *', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down Expand Up @@ -428,6 +432,8 @@ function getReturnJSType(
return 'NumberKind';
case 'BooleanTypeAnnotation':
return 'BooleanKind';
case 'BooleanLiteralTypeAnnotation':
return 'BooleanKind';
case 'GenericObjectTypeAnnotation':
return 'ObjectKind';
case 'EnumDeclaration':
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;
+passBooleanLiteral: (arg: true) => void;
+passNumberLiteral: (arg: 4) => void;
+passString: (arg: string) => void;
+passStringish: (arg: Stringish) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,26 @@ exports[`RN Codegen Flow Parser can generate fixture NATIVE_MODULE_WITH_BASIC_PA
]
}
},
{
'name': 'passBooleanLiteral',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'VoidTypeAnnotation'
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'BooleanLiteralTypeAnnotation',
'value': true
}
}
]
}
},
{
'name': 'passNumberLiteral',
'optional': false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
} = require('../../parsers-commons');
const {
emitArrayType,
emitBooleanLiteral,
emitCommonTypes,
emitDictionary,
emitFunction,
Expand Down Expand Up @@ -249,6 +250,9 @@ function translateTypeAnnotation(
case 'NumberLiteralTypeAnnotation': {
return emitNumberLiteral(nullable, typeAnnotation.value);
}
case 'BooleanLiteralTypeAnnotation': {
return emitBooleanLiteral(nullable, typeAnnotation.value);
}
case 'StringLiteralTypeAnnotation': {
return wrapNullable(nullable, {
type: 'StringLiteralTypeAnnotation',
Expand Down
12 changes: 12 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

import type {
BooleanLiteralTypeAnnotation,
BooleanTypeAnnotation,
DoubleTypeAnnotation,
EventTypeAnnotation,
Expand Down Expand Up @@ -181,6 +182,16 @@ function emitNumberLiteral(
});
}

function emitBooleanLiteral(
nullable: boolean,
value: boolean,
): Nullable<BooleanLiteralTypeAnnotation> {
return wrapNullable(nullable, {
type: 'BooleanLiteralTypeAnnotation',
value,
});
}

function emitString(nullable: boolean): Nullable<StringTypeAnnotation> {
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
Expand Down Expand Up @@ -764,6 +775,7 @@ function emitUnionProp(
module.exports = {
emitArrayType,
emitBoolean,
emitBooleanLiteral,
emitBoolProp,
emitDouble,
emitDoubleProp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import * as TurboModuleRegistry from '../TurboModuleRegistry';
export interface Spec extends TurboModule {
readonly passBool?: (arg: boolean) => void;
readonly passNumber: (arg: number) => void;
readonly passBooleanLiteral: (arg: true) => void;
readonly passNumberLiteral: (arg: 4) => void;
readonly passString: (arg: string) => void;
readonly passStringish: (arg: Stringish) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,26 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BA
]
}
},
{
'name': 'passBooleanLiteral',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'VoidTypeAnnotation'
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'BooleanLiteralTypeAnnotation',
'value': true
}
}
]
}
},
{
'name': 'passNumberLiteral',
'optional': false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
const {parseObjectProperty} = require('../../parsers-commons');
const {
emitArrayType,
emitBooleanLiteral,
emitCommonTypes,
emitDictionary,
emitFunction,
Expand Down Expand Up @@ -409,6 +410,9 @@ function translateTypeAnnotation(
case 'NumericLiteral': {
return emitNumberLiteral(nullable, literal.value);
}
case 'BooleanLiteral': {
return emitBooleanLiteral(nullable, literal.value);
}
default: {
throw new UnsupportedTypeAnnotationParserError(
hasteModuleName,
Expand Down
Loading
Loading