From 856bc2978cabba20374b5a57683d1e9e7bd52ce2 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 5 Nov 2020 17:43:23 -0800 Subject: [PATCH] Introduce NamedShape utility type Summary: Our CodegenSchema is littered with types that have the following shape ``` { name: string, optional: boolean, typeAnnotation: ... } ``` In all these types, the only difference is the typeAnnotation. This diff introduces a new utility type called `NamedShape`, that just creates this shape, given a type annotation. This should help reduce the amount of noise in the CodegenSchema, and make it a bit easier to read. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D24701331 fbshipit-source-id: a30d3e22933116e3dabf7929615905febacecba3 --- .../react-native-codegen/src/CodegenSchema.js | 65 ++++++++----------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 3fe73a0ca60..b141a28b12f 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -62,21 +62,24 @@ export type StringEnumTypeAnnotation = $ReadOnly<{| |}>, |}>; -export type EventObjectPropertyType = $ReadOnly<{| +type NamedShape<+T> = $ReadOnly<{ name: string, optional: boolean, - typeAnnotation: - | BooleanTypeAnnotation - | StringTypeAnnotation - | DoubleTypeAnnotation - | FloatTypeAnnotation - | Int32TypeAnnotation - | StringEnumTypeAnnotation - | $ReadOnly<{| - type: 'ObjectTypeAnnotation', - properties: $ReadOnlyArray, - |}>, -|}>; + typeAnnotation: T, +}>; + +export type EventObjectPropertyType = NamedShape< + | BooleanTypeAnnotation + | StringTypeAnnotation + | DoubleTypeAnnotation + | FloatTypeAnnotation + | Int32TypeAnnotation + | StringEnumTypeAnnotation + | $ReadOnly<{| + type: 'ObjectTypeAnnotation', + properties: $ReadOnlyArray, + |}>, +>; type PropTypeTypeAnnotation = | $ReadOnly<{| @@ -161,11 +164,7 @@ type PropTypeTypeAnnotation = |}>, |}>; -export type PropTypeShape = $ReadOnly<{| - name: string, - optional: boolean, - typeAnnotation: PropTypeTypeAnnotation, -|}>; +export type PropTypeShape = NamedShape; export type EventTypeShape = $ReadOnly<{| name: string, @@ -181,11 +180,7 @@ export type EventTypeShape = $ReadOnly<{| |}>, |}>; -export type CommandTypeShape = $ReadOnly<{| - name: string, - optional: boolean, - typeAnnotation: CommandsFunctionTypeAnnotation, -|}>; +export type CommandTypeShape = NamedShape; export type OptionsShape = $ReadOnly<{| interfaceOnly?: boolean, @@ -257,11 +252,9 @@ type NativeModuleSpec = $ReadOnly<{| properties: $ReadOnlyArray, |}>; -export type NativeModulePropertySchema = $ReadOnly<{| - name: string, - optional: boolean, - typeAnnotation: Nullable, -|}>; +export type NativeModulePropertySchema = NamedShape< + Nullable, +>; export type NativeModuleAliasMap = $ReadOnly<{| [aliasName: string]: NativeModuleObjectTypeAnnotation, @@ -273,22 +266,18 @@ export type NativeModuleFunctionTypeAnnotation = $ReadOnly<{| returnTypeAnnotation: Nullable, |}>; -export type NativeModuleMethodParamSchema = $ReadOnly<{| - name: string, - optional: boolean, - typeAnnotation: Nullable, -|}>; +export type NativeModuleMethodParamSchema = NamedShape< + Nullable, +>; export type NativeModuleObjectTypeAnnotation = $ReadOnly<{| type: 'ObjectTypeAnnotation', properties: $ReadOnlyArray, |}>; -export type NativeModuleObjectTypeAnnotationPropertySchema = $ReadOnly<{| - name: string, - optional: boolean, - typeAnnotation: Nullable, -|}>; +export type NativeModuleObjectTypeAnnotationPropertySchema = NamedShape< + Nullable, +>; export type NativeModuleArrayTypeAnnotation< +T: Nullable,