Skip to content

Commit 2d22fed

Browse files
Introduce BooleanLiteralTypeAnnotation (#54590)
Summary: Pull Request resolved: #54590 Introduce `BooleanLiteralTypeAnnotation` in Flow & TypeScript to match the existing `StringLiteralTypeAnnotation` & `NumberLiteralTypeAnnotation` since Unions will be supporting Booleans along with String & Number Changelog: [Internal] Reviewed By: elicwhite Differential Revision: D87384473 fbshipit-source-id: 6b8265914c7884ac99ae69e8992bc9dc6a9e52bc
1 parent b46427a commit 2d22fed

File tree

22 files changed

+169
-0
lines changed

22 files changed

+169
-0
lines changed

packages/react-native-codegen/src/CodegenSchema.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export interface VoidTypeAnnotation {
4646
readonly type: 'VoidTypeAnnotation';
4747
}
4848

49+
export interface BooleanLiteralTypeAnnotation {
50+
readonly type: 'BooleanLiteralTypeAnnotation';
51+
readonly value: boolean;
52+
}
53+
4954
export interface ObjectTypeAnnotation<T> {
5055
readonly type: 'ObjectTypeAnnotation';
5156
readonly properties: readonly NamedShape<T>[];
@@ -399,6 +404,7 @@ export type NativeModuleEventEmitterBaseTypeAnnotation =
399404
| NativeModuleInt32TypeAnnotation
400405
| NativeModuleNumberTypeAnnotation
401406
| NumberLiteralTypeAnnotation
407+
| BooleanLiteralTypeAnnotation
402408
| NativeModuleStringTypeAnnotation
403409
| StringLiteralTypeAnnotation
404410
| StringLiteralUnionTypeAnnotation
@@ -416,6 +422,7 @@ export type NativeModuleBaseTypeAnnotation =
416422
| StringLiteralUnionTypeAnnotation
417423
| NativeModuleNumberTypeAnnotation
418424
| NumberLiteralTypeAnnotation
425+
| BooleanLiteralTypeAnnotation
419426
| NativeModuleInt32TypeAnnotation
420427
| NativeModuleDoubleTypeAnnotation
421428
| NativeModuleFloatTypeAnnotation

packages/react-native-codegen/src/CodegenSchema.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export type StringLiteralTypeAnnotation = $ReadOnly<{
5656
value: string,
5757
}>;
5858

59+
export type BooleanLiteralTypeAnnotation = $ReadOnly<{
60+
type: 'BooleanLiteralTypeAnnotation',
61+
value: boolean,
62+
}>;
63+
5964
export type StringLiteralUnionTypeAnnotation = $ReadOnly<{
6065
type: 'StringLiteralUnionTypeAnnotation',
6166
types: $ReadOnlyArray<StringLiteralTypeAnnotation>,
@@ -388,6 +393,7 @@ type NativeModuleEventEmitterBaseTypeAnnotation =
388393
| Int32TypeAnnotation
389394
| NativeModuleNumberTypeAnnotation
390395
| NumberLiteralTypeAnnotation
396+
| BooleanLiteralTypeAnnotation
391397
| StringTypeAnnotation
392398
| StringLiteralTypeAnnotation
393399
| StringLiteralUnionTypeAnnotation
@@ -405,6 +411,7 @@ export type NativeModuleBaseTypeAnnotation =
405411
| StringLiteralUnionTypeAnnotation
406412
| NativeModuleNumberTypeAnnotation
407413
| NumberLiteralTypeAnnotation
414+
| BooleanLiteralTypeAnnotation
408415
| Int32TypeAnnotation
409416
| DoubleTypeAnnotation
410417
| FloatTypeAnnotation

packages/react-native-codegen/src/generators/modules/GenerateModuleH.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ function serializeArg(
9696
return wrap(val => `${val}.asString(rt)`);
9797
case 'BooleanTypeAnnotation':
9898
return wrap(val => `${val}.asBool()`);
99+
case 'BooleanLiteralTypeAnnotation':
100+
return wrap(val => `${val}.asBool()`);
99101
case 'EnumDeclaration':
100102
switch (realTypeAnnotation.memberType) {
101103
case 'NumberTypeAnnotation':
@@ -265,6 +267,8 @@ function translatePrimitiveJSTypeToCpp(
265267
return wrapOptional('int', isRequired);
266268
case 'BooleanTypeAnnotation':
267269
return wrapOptional('bool', isRequired);
270+
case 'BooleanLiteralTypeAnnotation':
271+
return wrapOptional('bool', isRequired);
268272
case 'EnumDeclaration':
269273
switch (realTypeAnnotation.memberType) {
270274
case 'NumberTypeAnnotation':

packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ function translateEventEmitterTypeToJavaType(
142142
case 'Int32TypeAnnotation':
143143
return 'double';
144144
case 'BooleanTypeAnnotation':
145+
case 'BooleanLiteralTypeAnnotation':
145146
return 'boolean';
146147
case 'GenericObjectTypeAnnotation':
147148
case 'ObjectTypeAnnotation':
@@ -214,6 +215,8 @@ function translateFunctionParamToJavaType(
214215
return wrapOptional('double', isRequired);
215216
case 'BooleanTypeAnnotation':
216217
return wrapOptional('boolean', isRequired);
218+
case 'BooleanLiteralTypeAnnotation':
219+
return wrapOptional('boolean', isRequired);
217220
case 'EnumDeclaration':
218221
switch (realTypeAnnotation.memberType) {
219222
case 'NumberTypeAnnotation':
@@ -310,6 +313,8 @@ function translateFunctionReturnTypeToJavaType(
310313
return wrapOptional('double', isRequired);
311314
case 'BooleanTypeAnnotation':
312315
return wrapOptional('boolean', isRequired);
316+
case 'BooleanLiteralTypeAnnotation':
317+
return wrapOptional('boolean', isRequired);
313318
case 'EnumDeclaration':
314319
switch (realTypeAnnotation.memberType) {
315320
case 'NumberTypeAnnotation':
@@ -388,6 +393,8 @@ function getFalsyReturnStatementFromReturnType(
388393
return nullable ? 'return null;' : 'return 0;';
389394
case 'BooleanTypeAnnotation':
390395
return nullable ? 'return null;' : 'return false;';
396+
case 'BooleanLiteralTypeAnnotation':
397+
return nullable ? 'return null;' : 'return false;';
391398
case 'EnumDeclaration':
392399
switch (realTypeAnnotation.memberType) {
393400
case 'NumberTypeAnnotation':

packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ function translateReturnTypeToKind(
171171
return 'StringKind';
172172
case 'BooleanTypeAnnotation':
173173
return 'BooleanKind';
174+
case 'BooleanLiteralTypeAnnotation':
175+
return 'BooleanKind';
174176
case 'EnumDeclaration':
175177
switch (typeAnnotation.memberType) {
176178
case 'NumberTypeAnnotation':
@@ -256,6 +258,8 @@ function translateParamTypeToJniType(
256258
return 'Ljava/lang/String;';
257259
case 'BooleanTypeAnnotation':
258260
return !isRequired ? 'Ljava/lang/Boolean;' : 'Z';
261+
case 'BooleanLiteralTypeAnnotation':
262+
return !isRequired ? 'Ljava/lang/Boolean;' : 'Z';
259263
case 'EnumDeclaration':
260264
switch (typeAnnotation.memberType) {
261265
case 'NumberTypeAnnotation':
@@ -338,6 +342,8 @@ function translateReturnTypeToJniType(
338342
return 'Ljava/lang/String;';
339343
case 'BooleanTypeAnnotation':
340344
return nullable ? 'Ljava/lang/Boolean;' : 'Z';
345+
case 'BooleanLiteralTypeAnnotation':
346+
return nullable ? 'Ljava/lang/Boolean;' : 'Z';
341347
case 'EnumDeclaration':
342348
switch (typeAnnotation.memberType) {
343349
case 'NumberTypeAnnotation':

packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/StructCollector.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
import type {
14+
BooleanLiteralTypeAnnotation,
1415
BooleanTypeAnnotation,
1516
DoubleTypeAnnotation,
1617
FloatTypeAnnotation,
@@ -65,6 +66,7 @@ export type StructTypeAnnotation =
6566
| StringLiteralUnionTypeAnnotation
6667
| NativeModuleNumberTypeAnnotation
6768
| NumberLiteralTypeAnnotation
69+
| BooleanLiteralTypeAnnotation
6870
| Int32TypeAnnotation
6971
| DoubleTypeAnnotation
7072
| FloatTypeAnnotation

packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ function toObjCType(
110110
return wrapCxxOptional('double', isRequired);
111111
case 'BooleanTypeAnnotation':
112112
return wrapCxxOptional('bool', isRequired);
113+
case 'BooleanLiteralTypeAnnotation':
114+
return wrapCxxOptional('bool', isRequired);
113115
case 'EnumDeclaration':
114116
switch (typeAnnotation.memberType) {
115117
case 'NumberTypeAnnotation':
@@ -195,6 +197,8 @@ function toObjCValue(
195197
return wrapPrimitive('double');
196198
case 'BooleanTypeAnnotation':
197199
return wrapPrimitive('BOOL');
200+
case 'BooleanLiteralTypeAnnotation':
201+
return wrapPrimitive('BOOL');
198202
case 'EnumDeclaration':
199203
switch (typeAnnotation.memberType) {
200204
case 'NumberTypeAnnotation':

packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ function toObjCType(
101101
return wrapCxxOptional('double', isRequired);
102102
case 'BooleanTypeAnnotation':
103103
return wrapCxxOptional('bool', isRequired);
104+
case 'BooleanLiteralTypeAnnotation':
105+
return wrapCxxOptional('bool', isRequired);
104106
case 'EnumDeclaration':
105107
switch (typeAnnotation.memberType) {
106108
case 'NumberTypeAnnotation':
@@ -185,6 +187,8 @@ function toObjCValue(
185187
return RCTBridgingTo('Double');
186188
case 'BooleanTypeAnnotation':
187189
return RCTBridgingTo('Bool');
190+
case 'BooleanLiteralTypeAnnotation':
191+
return RCTBridgingTo('Bool');
188192
case 'EnumDeclaration':
189193
switch (typeAnnotation.memberType) {
190194
case 'NumberTypeAnnotation':

packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeEventEmitter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function getEventEmitterTypeObjCType(
2828
case 'NumberLiteralTypeAnnotation':
2929
return 'NSNumber *_Nonnull';
3030
case 'BooleanTypeAnnotation':
31+
case 'BooleanLiteralTypeAnnotation':
3132
return 'BOOL';
3233
case 'GenericObjectTypeAnnotation':
3334
case 'ObjectTypeAnnotation':

packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ function getParamObjCType(
273273
return notStruct(isRequired ? 'NSInteger' : 'NSNumber *');
274274
case 'BooleanTypeAnnotation':
275275
return notStruct(isRequired ? 'BOOL' : 'NSNumber *');
276+
case 'BooleanLiteralTypeAnnotation':
277+
return notStruct(isRequired ? 'BOOL' : 'NSNumber *');
276278
case 'EnumDeclaration':
277279
switch (typeAnnotation.memberType) {
278280
case 'NumberTypeAnnotation':
@@ -356,6 +358,8 @@ function getReturnObjCType(
356358
return wrapOptional('NSNumber *', isRequired);
357359
case 'BooleanTypeAnnotation':
358360
return wrapOptional('NSNumber *', isRequired);
361+
case 'BooleanLiteralTypeAnnotation':
362+
return wrapOptional('NSNumber *', isRequired);
359363
case 'EnumDeclaration':
360364
switch (typeAnnotation.memberType) {
361365
case 'NumberTypeAnnotation':
@@ -428,6 +432,8 @@ function getReturnJSType(
428432
return 'NumberKind';
429433
case 'BooleanTypeAnnotation':
430434
return 'BooleanKind';
435+
case 'BooleanLiteralTypeAnnotation':
436+
return 'BooleanKind';
431437
case 'GenericObjectTypeAnnotation':
432438
return 'ObjectKind';
433439
case 'EnumDeclaration':

0 commit comments

Comments
 (0)