From 91205d2ba44f286cf2a14fef29a9b76bd8bb5088 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Fri, 31 Jul 2020 15:11:40 -0700 Subject: [PATCH] Re-organize NativeModule types Summary: This diff: - Moves the NativeModule flow types to the bottom of `CodegenSchema.js`. - Re-organizes the NativeModuel flow type declarations based on when they're first used. Essentially, we start off by declaring a giant 'NativeModuleShape' type, which uses smaller undeclared types. Then we declare all the undeclared children of `NativeModuleShape`, and on and on. This way, you know where to start reading the types, and you can easily tell how every type relates to every other type. Changelog: [Internal] Differential Revision: D22828840 fbshipit-source-id: 5b4b9466a41b9bcb92a1de159bcbc12e4dc01df3 --- .../react-native-codegen/src/CodegenSchema.js | 220 +++++++++--------- 1 file changed, 112 insertions(+), 108 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 5ae6d4a2473..ffd3a1a4666 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -55,11 +55,6 @@ export type StringTypeAnnotation = $ReadOnly<{| type: 'StringTypeAnnotation', |}>; -export type TypeAliasTypeAnnotation = $ReadOnly<{| - type: 'TypeAliasTypeAnnotation', - name: string, -|}>; - export type EventObjectPropertyType = | $ReadOnly<{| type: 'BooleanTypeAnnotation', @@ -200,109 +195,6 @@ export type PropTypeShape = $ReadOnly<{| typeAnnotation: PropTypeTypeAnnotation, |}>; -export type PrimitiveTypeAnnotationType = - | 'StringTypeAnnotation' - | 'NumberTypeAnnotation' - | 'Int32TypeAnnotation' - | 'DoubleTypeAnnotation' - | 'FloatTypeAnnotation' - | 'BooleanTypeAnnotation' - | 'GenericObjectTypeAnnotation'; - -export type PrimitiveTypeAnnotation = $ReadOnly<{| - type: PrimitiveTypeAnnotationType, -|}>; - -export type ReservedFunctionValueTypeName = 'RootTag'; // Union with more custom types. - -export type FunctionTypeAnnotationParamTypeAnnotation = - | $ReadOnly<{| - type: - | 'AnyTypeAnnotation' - | 'FunctionTypeAnnotation' - | PrimitiveTypeAnnotationType, - |}> - | $ReadOnly<{| - type: 'ReservedFunctionValueTypeAnnotation', - name: ReservedFunctionValueTypeName, - |}> - | $ReadOnly<{| - type: 'ArrayTypeAnnotation', - elementType: - | ?FunctionTypeAnnotationParamTypeAnnotation - | ?TypeAliasTypeAnnotation, - |}> - | $ReadOnly<{| - type: 'ObjectTypeAnnotation', - properties: ?$ReadOnlyArray, - |}>; - -export type FunctionTypeAnnotationReturnArrayElementType = - | FunctionTypeAnnotationParamTypeAnnotation - | TypeAliasTypeAnnotation; - -export type ObjectParamTypeAnnotation = $ReadOnly<{| - optional: boolean, - name: string, - typeAnnotation?: - | FunctionTypeAnnotationParamTypeAnnotation - | TypeAliasTypeAnnotation, // TODO (T67898313): Workaround for NativeLinking's use of union type, typeAnnotations should not be optional -|}>; - -export type FunctionTypeAnnotationReturn = - | $ReadOnly<{| - nullable: boolean, - type: - | 'GenericPromiseTypeAnnotation' - | 'VoidTypeAnnotation' - | PrimitiveTypeAnnotationType, - |}> - | $ReadOnly<{| - nullable: boolean, - type: 'ReservedFunctionValueTypeAnnotation', - name: ReservedFunctionValueTypeName, - |}> - | $ReadOnly<{| - nullable: boolean, - type: 'ArrayTypeAnnotation', - elementType: ?FunctionTypeAnnotationReturnArrayElementType, - |}> - | $ReadOnly<{| - nullable: boolean, - type: 'ObjectTypeAnnotation', - properties: ?$ReadOnlyArray, - |}>; - -export type FunctionTypeAnnotationParam = $ReadOnly<{| - nullable: boolean, - name: string, - typeAnnotation: - | FunctionTypeAnnotationParamTypeAnnotation - | TypeAliasTypeAnnotation, -|}>; - -export type FunctionTypeAnnotation = $ReadOnly<{| - type: 'FunctionTypeAnnotation', - params: $ReadOnlyArray, - returnTypeAnnotation: FunctionTypeAnnotationReturn, - optional: boolean, -|}>; - -export type NativeModuleMethodTypeShape = $ReadOnly<{| - name: string, - typeAnnotation: FunctionTypeAnnotation, -|}>; - -export type ObjectTypeAliasTypeShape = $ReadOnly<{| - type: 'ObjectTypeAnnotation', - properties: $ReadOnlyArray, -|}>; - -export type NativeModuleShape = $ReadOnly<{| - aliases: $ReadOnly<{[aliasName: string]: ObjectTypeAliasTypeShape, ...}>, - properties: $ReadOnlyArray, -|}>; - export type EventTypeShape = $ReadOnly<{| name: string, bubblingType: 'direct' | 'bubble', @@ -363,3 +255,115 @@ export type SchemaType = $ReadOnly<{| ..., }>, |}>; + +/** + * NativeModule Types + */ +export type NativeModuleShape = $ReadOnly<{| + // We only support aliases to Objects + aliases: $ReadOnly<{[aliasName: string]: ObjectTypeAliasTypeShape, ...}>, + properties: $ReadOnlyArray, +|}>; + +export type ObjectTypeAliasTypeShape = $ReadOnly<{| + type: 'ObjectTypeAnnotation', + properties: $ReadOnlyArray, +|}>; + +export type NativeModuleMethodTypeShape = $ReadOnly<{| + name: string, + typeAnnotation: FunctionTypeAnnotation, +|}>; + +export type FunctionTypeAnnotation = $ReadOnly<{| + type: 'FunctionTypeAnnotation', + params: $ReadOnlyArray, + returnTypeAnnotation: FunctionTypeAnnotationReturn, + optional: boolean, +|}>; + +export type FunctionTypeAnnotationReturn = + | $ReadOnly<{| + nullable: boolean, + type: + | 'GenericPromiseTypeAnnotation' + | 'VoidTypeAnnotation' + | PrimitiveTypeAnnotationType, + |}> + | $ReadOnly<{| + nullable: boolean, + type: 'ReservedFunctionValueTypeAnnotation', + name: ReservedFunctionValueTypeName, + |}> + | $ReadOnly<{| + nullable: boolean, + type: 'ArrayTypeAnnotation', + elementType: ?FunctionTypeAnnotationReturnArrayElementType, + |}> + | $ReadOnly<{| + nullable: boolean, + type: 'ObjectTypeAnnotation', + properties: ?$ReadOnlyArray, + |}>; + +export type FunctionTypeAnnotationReturnArrayElementType = + | FunctionTypeAnnotationParamTypeAnnotation // TODO: What does FunctionTypeAnnotationParamTypeAnnotation have to do with function returns? + | TypeAliasTypeAnnotation; + +export type TypeAliasTypeAnnotation = $ReadOnly<{| + type: 'TypeAliasTypeAnnotation', + name: string, +|}>; + +export type FunctionTypeAnnotationParam = $ReadOnly<{| + nullable: boolean, + name: string, + typeAnnotation: + | FunctionTypeAnnotationParamTypeAnnotation + | TypeAliasTypeAnnotation, +|}>; + +export type FunctionTypeAnnotationParamTypeAnnotation = + | $ReadOnly<{| + type: + | 'AnyTypeAnnotation' + | 'FunctionTypeAnnotation' + | PrimitiveTypeAnnotationType, + |}> + | $ReadOnly<{| + type: 'ReservedFunctionValueTypeAnnotation', + name: ReservedFunctionValueTypeName, + |}> + | $ReadOnly<{| + type: 'ArrayTypeAnnotation', + elementType: + | ?FunctionTypeAnnotationParamTypeAnnotation + | ?TypeAliasTypeAnnotation, + |}> + | $ReadOnly<{| + type: 'ObjectTypeAnnotation', + properties: ?$ReadOnlyArray, + |}>; + +export type PrimitiveTypeAnnotationType = + | 'StringTypeAnnotation' + | 'NumberTypeAnnotation' + | 'Int32TypeAnnotation' + | 'DoubleTypeAnnotation' + | 'FloatTypeAnnotation' + | 'BooleanTypeAnnotation' + | 'GenericObjectTypeAnnotation'; + +export type PrimitiveTypeAnnotation = $ReadOnly<{| + type: PrimitiveTypeAnnotationType, +|}>; + +export type ReservedFunctionValueTypeName = 'RootTag'; // Union with more custom types. + +export type ObjectParamTypeAnnotation = $ReadOnly<{| + optional: boolean, + name: string, + typeAnnotation?: + | FunctionTypeAnnotationParamTypeAnnotation + | TypeAliasTypeAnnotation, // TODO (T67898313): Workaround for NativeLinking's use of union type, typeAnnotations should not be optional +|}>;