mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
e261f022fe
Summary:
The current parser behavior flattens out any object type aliases into ObjectTypeAnnotations. Generators can treat these as regular objects and generate the applicable native code. This, however, can lead to repetition whenever an object type alias is re-used in the same native module.
I propose we treat these as a special case, using a TypeAliasTypeAnnotation to denote them as type aliases. Generators can look up the actual signature for a given object alias by referring to the "aliases" array that is provided in the schema.
**Proposed schema change:**
Adds an "aliases" key to each module in the schema:
```
export type NativeModuleShape = $ReadOnly<{|
properties: $ReadOnlyArray<NativeModuleMethodTypeShape>,
aliases: $ReadOnlyArray<{|
name: string,
typeAnnotation:
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<ObjectParamTypeAnnotation>,
|}>
| $ReadOnly<TypeAliasTypeAnnotation>,
|}>,
|}>;
```
Example:
```
{
modules: {
SampleTurboModule: {
nativeModules: {
SampleTurboModule: {
aliases: [],
properties: [],
},
},
},
},
}
```
Method parameters will now support the new 'TypeAliasTypeAnnotation' wherever a param might have used a 'ObjectTypeAnnotation':
```
export type TypeAliasTypeAnnotation = $ReadOnly<{|
type: 'TypeAliasTypeAnnotation',
name: string,
|}>;
```
Changelog: [Internal]
Reviewed By: RSNara
Differential Revision: D22200700
fbshipit-source-id: 15684620783c752f2fb482ba4b88d1fd1cc07540
366 lines
9.3 KiB
JavaScript
366 lines
9.3 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
export type PlatformType = 'iOS' | 'android';
|
|
|
|
export type CommandsFunctionTypeAnnotation = $ReadOnly<{|
|
|
type: 'FunctionTypeAnnotation',
|
|
params: $ReadOnlyArray<CommandsFunctionTypeParamAnnotation>,
|
|
|}>;
|
|
|
|
export type CommandsFunctionTypeParamAnnotation = $ReadOnly<{|
|
|
name: string,
|
|
typeAnnotation: CommandsTypeAnnotation,
|
|
|}>;
|
|
|
|
export type CommandsTypeAnnotation =
|
|
| ReservedFunctionValueTypeAnnotation
|
|
| BooleanTypeAnnotation
|
|
| Int32TypeAnnotation
|
|
| DoubleTypeAnnotation
|
|
| FloatTypeAnnotation
|
|
| StringTypeAnnotation;
|
|
|
|
type ReservedFunctionValueTypeAnnotation = $ReadOnly<{|
|
|
type: 'ReservedFunctionValueTypeAnnotation',
|
|
name: ReservedFunctionValueTypeName,
|
|
|}>;
|
|
|
|
export type DoubleTypeAnnotation = $ReadOnly<{|
|
|
type: 'DoubleTypeAnnotation',
|
|
|}>;
|
|
|
|
export type FloatTypeAnnotation = $ReadOnly<{|
|
|
type: 'FloatTypeAnnotation',
|
|
|}>;
|
|
|
|
export type BooleanTypeAnnotation = $ReadOnly<{|
|
|
type: 'BooleanTypeAnnotation',
|
|
|}>;
|
|
|
|
export type Int32TypeAnnotation = $ReadOnly<{|
|
|
type: 'Int32TypeAnnotation',
|
|
|}>;
|
|
|
|
export type StringTypeAnnotation = $ReadOnly<{|
|
|
type: 'StringTypeAnnotation',
|
|
|}>;
|
|
|
|
export type TypeAliasTypeAnnotation = $ReadOnly<{|
|
|
type: 'TypeAliasTypeAnnotation',
|
|
name: string,
|
|
|}>;
|
|
|
|
export type EventObjectPropertyType =
|
|
| $ReadOnly<{|
|
|
type: 'BooleanTypeAnnotation',
|
|
name: string,
|
|
optional: boolean,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'StringTypeAnnotation',
|
|
name: string,
|
|
optional: boolean,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'DoubleTypeAnnotation',
|
|
name: string,
|
|
optional: boolean,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'FloatTypeAnnotation',
|
|
name: string,
|
|
optional: boolean,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'Int32TypeAnnotation',
|
|
name: string,
|
|
optional: boolean,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'StringEnumTypeAnnotation',
|
|
name: string,
|
|
optional: boolean,
|
|
options: $ReadOnlyArray<{|
|
|
name: string,
|
|
|}>,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'ObjectTypeAnnotation',
|
|
name: string,
|
|
optional: boolean,
|
|
properties: $ReadOnlyArray<EventObjectPropertyType>,
|
|
|}>;
|
|
|
|
type PropTypeTypeAnnotation =
|
|
| $ReadOnly<{|
|
|
type: 'BooleanTypeAnnotation',
|
|
default: boolean | null,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'StringTypeAnnotation',
|
|
default: string | null,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'DoubleTypeAnnotation',
|
|
default: number,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'FloatTypeAnnotation',
|
|
default: number | null,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'Int32TypeAnnotation',
|
|
default: number,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'StringEnumTypeAnnotation',
|
|
default: string,
|
|
options: $ReadOnlyArray<{|
|
|
name: string,
|
|
|}>,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'Int32EnumTypeAnnotation',
|
|
default: number,
|
|
options: $ReadOnlyArray<{|
|
|
value: number,
|
|
|}>,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'ReservedPropTypeAnnotation',
|
|
name:
|
|
| 'ColorPrimitive'
|
|
| 'ImageSourcePrimitive'
|
|
| 'PointPrimitive'
|
|
| 'EdgeInsetsPrimitive',
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'ObjectTypeAnnotation',
|
|
properties: $ReadOnlyArray<PropTypeShape>,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'ArrayTypeAnnotation',
|
|
elementType:
|
|
| $ReadOnly<{|
|
|
type: 'BooleanTypeAnnotation',
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'StringTypeAnnotation',
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'DoubleTypeAnnotation',
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'FloatTypeAnnotation',
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'Int32TypeAnnotation',
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'StringEnumTypeAnnotation',
|
|
default: string,
|
|
options: $ReadOnlyArray<{|
|
|
name: string,
|
|
|}>,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'ObjectTypeAnnotation',
|
|
properties: $ReadOnlyArray<PropTypeShape>,
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'ReservedPropTypeAnnotation',
|
|
name:
|
|
| 'ColorPrimitive'
|
|
| 'ImageSourcePrimitive'
|
|
| 'PointPrimitive'
|
|
| 'EdgeInsetsPrimitive',
|
|
|}>
|
|
| $ReadOnly<{|
|
|
type: 'ArrayTypeAnnotation',
|
|
elementType: $ReadOnly<{|
|
|
type: 'ObjectTypeAnnotation',
|
|
properties: $ReadOnlyArray<PropTypeShape>,
|
|
|}>,
|
|
|}>,
|
|
|}>;
|
|
|
|
export type PropTypeShape = $ReadOnly<{|
|
|
name: string,
|
|
optional: boolean,
|
|
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<ObjectParamTypeAnnotation>,
|
|
|}>;
|
|
|
|
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<ObjectParamTypeAnnotation>,
|
|
|}>;
|
|
|
|
export type FunctionTypeAnnotationParam = $ReadOnly<{|
|
|
nullable: boolean,
|
|
name: string,
|
|
typeAnnotation:
|
|
| FunctionTypeAnnotationParamTypeAnnotation
|
|
| TypeAliasTypeAnnotation,
|
|
|}>;
|
|
|
|
export type FunctionTypeAnnotation = $ReadOnly<{|
|
|
type: 'FunctionTypeAnnotation',
|
|
params: $ReadOnlyArray<FunctionTypeAnnotationParam>,
|
|
returnTypeAnnotation: FunctionTypeAnnotationReturn,
|
|
optional: boolean,
|
|
|}>;
|
|
|
|
export type NativeModuleMethodTypeShape = $ReadOnly<{|
|
|
name: string,
|
|
typeAnnotation: FunctionTypeAnnotation,
|
|
|}>;
|
|
|
|
export type ObjectTypeAliasTypeShape = $ReadOnly<{|
|
|
type: 'ObjectTypeAnnotation',
|
|
properties: $ReadOnlyArray<ObjectParamTypeAnnotation>,
|
|
|}>;
|
|
|
|
export type NativeModuleShape = $ReadOnly<{|
|
|
aliases: $ReadOnly<{[aliasName: string]: ObjectTypeAliasTypeShape, ...}>,
|
|
properties: $ReadOnlyArray<NativeModuleMethodTypeShape>,
|
|
|}>;
|
|
|
|
export type EventTypeShape = $ReadOnly<{|
|
|
name: string,
|
|
bubblingType: 'direct' | 'bubble',
|
|
optional: boolean,
|
|
paperTopLevelNameDeprecated?: string,
|
|
typeAnnotation: $ReadOnly<{|
|
|
type: 'EventTypeAnnotation',
|
|
argument?: $ReadOnly<{|
|
|
type: 'ObjectTypeAnnotation',
|
|
properties: $ReadOnlyArray<EventObjectPropertyType>,
|
|
|}>,
|
|
|}>,
|
|
|}>;
|
|
|
|
export type CommandTypeShape = $ReadOnly<{|
|
|
name: string,
|
|
optional: boolean,
|
|
typeAnnotation: CommandsFunctionTypeAnnotation,
|
|
|}>;
|
|
|
|
export type OptionsShape = $ReadOnly<{|
|
|
interfaceOnly?: boolean,
|
|
|
|
// Use for components with no current paper rename in progress
|
|
// Does not check for new name
|
|
paperComponentName?: string,
|
|
|
|
// Use for components that are not used on other platforms.
|
|
excludedPlatforms?: $ReadOnlyArray<PlatformType>,
|
|
|
|
// Use for components currently being renamed in paper
|
|
// Will use new name if it is available and fallback to this name
|
|
paperComponentNameDeprecated?: string,
|
|
|}>;
|
|
|
|
export type ExtendsPropsShape = $ReadOnly<{|
|
|
type: 'ReactNativeBuiltInType',
|
|
knownTypeName: 'ReactNativeCoreViewProps',
|
|
|}>;
|
|
|
|
export type ComponentShape = $ReadOnly<{|
|
|
...OptionsShape,
|
|
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
|
|
events: $ReadOnlyArray<EventTypeShape>,
|
|
props: $ReadOnlyArray<PropTypeShape>,
|
|
commands: $ReadOnlyArray<CommandTypeShape>,
|
|
|}>;
|
|
|
|
export type SchemaType = $ReadOnly<{|
|
|
modules: $ReadOnly<{
|
|
[module: string]: $ReadOnly<{|
|
|
components?: $ReadOnly<{[component: string]: ComponentShape, ...}>,
|
|
nativeModules?: $ReadOnly<{
|
|
[nativeModule: string]: NativeModuleShape,
|
|
...,
|
|
}>,
|
|
|}>,
|
|
...,
|
|
}>,
|
|
|}>;
|