Skip to content

Commit 0ea52bc

Browse files
Introduce BooleanLiteralTypeAnnotation (facebook#54590)
Summary: Introduce `BooleanLiteralTypeAnnotation` in Flow & TypeScript to match the existing `StringLiteralTypeAnnotation` & `NumberLiteralTypeAnnotation` since Unions will be supporting Booleans along with String & Number Changelog: [Internal] Differential Revision: D87384473
1 parent 1d9d3ae commit 0ea52bc

File tree

7 files changed

+71
-0
lines changed

7 files changed

+71
-0
lines changed

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

Lines changed: 5 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>[];

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>,
@@ -457,6 +462,8 @@ export type CompleteTypeAnnotation =
457462
| UnsafeAnyTypeAnnotation
458463
| ArrayTypeAnnotation<CompleteTypeAnnotation>
459464
| ObjectTypeAnnotation<CompleteTypeAnnotation>
465+
// TODO: D87392274 Remove BooleanLiteralTypeAnnotation from here and add it to existing places
466+
| BooleanLiteralTypeAnnotation
460467
// Components
461468
| CommandTypeAnnotation
462469
| CompleteReservedTypeAnnotation;

packages/react-native-compatibility-check/src/ErrorFormatting.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ function formatTypeAnnotation(annotation: CompleteTypeAnnotation): string {
173173
return 'int';
174174
case 'NumberLiteralTypeAnnotation':
175175
return annotation.value.toString();
176+
case 'BooleanLiteralTypeAnnotation':
177+
return annotation.value.toString();
176178
case 'ObjectTypeAnnotation':
177179
return (
178180
'{' +

packages/react-native-compatibility-check/src/SortTypeAnnotations.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ export function compareTypeAnnotationForSorting(
116116
case 'NumberLiteralTypeAnnotation':
117117
invariant(typeB.type === 'NumberLiteralTypeAnnotation', EQUALITY_MSG);
118118
return typeA.value - typeB.value;
119+
case 'BooleanLiteralTypeAnnotation':
120+
invariant(typeB.type === 'BooleanLiteralTypeAnnotation', EQUALITY_MSG);
121+
return originalPositionA - originalPositionB;
119122
case 'ObjectTypeAnnotation':
120123
invariant(typeB.type === 'ObjectTypeAnnotation', EQUALITY_MSG);
121124
return compareNameAnnotationArraysForSorting(
@@ -261,6 +264,8 @@ function typeAnnotationArbitraryOrder(annotation: CompleteTypeAnnotation) {
261264
return 14;
262265
case 'ObjectTypeAnnotation':
263266
return 15;
267+
case 'BooleanLiteralTypeAnnotation':
268+
return 16;
264269
case 'StringLiteralUnionTypeAnnotation':
265270
return 17;
266271
case 'StringTypeAnnotation':

packages/react-native-compatibility-check/src/TypeDiffing.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
TypeComparisonError,
1818
} from './ComparisonResult';
1919
import type {
20+
BooleanLiteralTypeAnnotation,
2021
CompleteReservedTypeAnnotation,
2122
CompleteTypeAnnotation,
2223
EventEmitterTypeAnnotation,
@@ -237,6 +238,12 @@ export function compareTypeAnnotation(
237238
EQUALITY_MSG,
238239
);
239240
return compareNumberLiteralTypes(newerAnnotation, olderAnnotation);
241+
case 'BooleanLiteralTypeAnnotation':
242+
invariant(
243+
olderAnnotation.type === 'BooleanLiteralTypeAnnotation',
244+
EQUALITY_MSG,
245+
);
246+
return compareBooleanLiteralTypes(newerAnnotation, olderAnnotation);
240247
case 'StringLiteralUnionTypeAnnotation':
241248
invariant(
242249
olderAnnotation.type === 'StringLiteralUnionTypeAnnotation',
@@ -907,6 +914,21 @@ export function compareStringLiteralTypes(
907914
);
908915
}
909916

917+
export function compareBooleanLiteralTypes(
918+
newerType: BooleanLiteralTypeAnnotation,
919+
olderType: BooleanLiteralTypeAnnotation,
920+
): ComparisonResult {
921+
return newerType.value === olderType.value
922+
? {status: 'matching'}
923+
: makeError(
924+
typeAnnotationComparisonError(
925+
'Boolean literals are not equal',
926+
newerType,
927+
olderType,
928+
),
929+
);
930+
}
931+
910932
export function compareStringLiteralUnionTypes(
911933
newerType: StringLiteralUnionTypeAnnotation,
912934
olderType: StringLiteralUnionTypeAnnotation,

packages/react-native-compatibility-check/src/__tests__/TypeDiffing-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,30 @@ describe('compareTypes on string literals', () => {
905905
});
906906
});
907907

908+
describe('compareTypes on boolean literals', () => {
909+
it('matches literals that are the same', () => {
910+
expect(
911+
compareTypes(
912+
nativeTypeDiffingTypesMethodParamLookup('booleanLiteral0'),
913+
nativeTypeDiffingTypesMethodParamLookup('booleanLiteral0'),
914+
nativeTypeDiffingTypesAliases,
915+
nativeTypeDiffingTypesAliases,
916+
).status,
917+
).toBe('matching');
918+
});
919+
920+
it('fails on literals that are not the same', () => {
921+
expect(
922+
compareTypes(
923+
nativeTypeDiffingTypesMethodParamLookup('booleanLiteral0'),
924+
nativeTypeDiffingTypesMethodParamLookup('booleanLiteral1'),
925+
nativeTypeDiffingTypesAliases,
926+
nativeTypeDiffingTypesAliases,
927+
),
928+
).toHaveErrorWithMessage('Boolean literals are not equal');
929+
});
930+
});
931+
908932
describe('compareTypes on numeric literals', () => {
909933
it('matches literals that are the same', () => {
910934
expect(

packages/react-native-compatibility-check/src/__tests__/__fixtures__/native-module-type-diffing-types/NativeTypeDiffingTypes.js.flow

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboMo
1717
type StringLiteral0 = 'foo';
1818
type StringLiteral1 = 'bar';
1919

20+
type BooleanLiteral0 = true;
21+
type BooleanLiteral1 = false;
22+
2023
type NumericLiteral0 = 1;
2124
type NumericLiteral1 = 2;
2225

@@ -170,6 +173,9 @@ export interface Spec extends TurboModule {
170173
+stringLiteral0: (a: StringLiteral0) => void;
171174
+stringLiteral1: (a: StringLiteral1) => void;
172175

176+
+booleanLiteral0: (a: BooleanLiteral0) => void;
177+
+booleanLiteral1: (a: BooleanLiteral1) => void;
178+
173179
+numericLiteral0: (a: NumericLiteral0) => void;
174180
+numericLiteral1: (a: NumericLiteral1) => void;
175181

0 commit comments

Comments
 (0)