Introduce ObjectTypeAnnotation utility type

Summary:
All throughout the Codegen schema, we re-declare the following shape:
```
{
  type: 'ObjectTypeAnnotation',
  properties: $ReadOnlyArray<{
    name: string,
    optional: boolean,
    typeAnnotation: ...
  }>
}
```

This diff introduces an `ObjectTypeAnnotation<T>` utility type and replaces those re-declarations with instantiations of this type.

**Motivation:** To reduce noise in the CodegenSchema. This should be a pure refactor, and shouldn't actually change any behaviour.

Changelog: [Internal]

Reviewed By: yungsters

Differential Revision: D24707963

fbshipit-source-id: 6b4eb711ddd041f3a041109ade5ad5644fb16924
This commit is contained in:
Ramanpreet Nara
2020-11-05 18:30:08 -08:00
committed by Facebook GitHub Bot
parent b9f6937288
commit 688caa0bdc
12 changed files with 78 additions and 72 deletions
+22 -37
View File
@@ -48,7 +48,12 @@ export type StringEnumTypeAnnotation = $ReadOnly<{|
|}>,
|}>;
type NamedShape<+T> = $ReadOnly<{
type ObjectTypeAnnotation<+T> = $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<NamedShape<T>>,
|}>;
export type NamedShape<+T> = $ReadOnly<{
name: string,
optional: boolean,
typeAnnotation: T,
@@ -65,8 +70,8 @@ export type ComponentShape = $ReadOnly<{|
...OptionsShape,
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
events: $ReadOnlyArray<EventTypeShape>,
props: $ReadOnlyArray<PropTypeShape>,
commands: $ReadOnlyArray<CommandTypeShape>,
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
commands: $ReadOnlyArray<NamedShape<CommandsTypeAnnotation>>,
|}>;
export type OptionsShape = $ReadOnly<{|
@@ -96,29 +101,20 @@ export type EventTypeShape = $ReadOnly<{|
paperTopLevelNameDeprecated?: string,
typeAnnotation: $ReadOnly<{|
type: 'EventTypeAnnotation',
argument?: $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<EventObjectPropertyType>,
|}>,
argument?: ObjectTypeAnnotation<EventTypeAnnotation>,
|}>,
|}>;
export type EventObjectPropertyType = NamedShape<
export type EventTypeAnnotation =
| BooleanTypeAnnotation
| StringTypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
| Int32TypeAnnotation
| StringEnumTypeAnnotation
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<EventObjectPropertyType>,
|}>,
>;
| ObjectTypeAnnotation<EventTypeAnnotation>;
export type PropTypeShape = NamedShape<PropTypeTypeAnnotation>;
type PropTypeTypeAnnotation =
export type PropTypeAnnotation =
| $ReadOnly<{|
type: 'BooleanTypeAnnotation',
default: boolean | null,
@@ -161,10 +157,7 @@ type PropTypeTypeAnnotation =
| 'PointPrimitive'
| 'EdgeInsetsPrimitive',
|}>
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<PropTypeShape>,
|}>
| ObjectTypeAnnotation<PropTypeAnnotation>
| $ReadOnly<{|
type: 'ArrayTypeAnnotation',
elementType:
@@ -180,10 +173,7 @@ type PropTypeTypeAnnotation =
name: string,
|}>,
|}>
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<PropTypeShape>,
|}>
| ObjectTypeAnnotation<PropTypeAnnotation>
| $ReadOnly<{|
type: 'ReservedPropTypeAnnotation',
name:
@@ -194,26 +184,22 @@ type PropTypeTypeAnnotation =
|}>
| $ReadOnly<{|
type: 'ArrayTypeAnnotation',
elementType: $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<PropTypeShape>,
|}>,
elementType: ObjectTypeAnnotation<PropTypeAnnotation>,
|}>,
|}>;
export type CommandTypeShape = NamedShape<CommandsFunctionTypeAnnotation>;
export type CommandsFunctionTypeAnnotation = $ReadOnly<{|
// TODO: Unify this function type annotation with NativeModule schema
export type CommandsTypeAnnotation = $ReadOnly<{|
type: 'FunctionTypeAnnotation',
params: $ReadOnlyArray<CommandsFunctionTypeParamAnnotation>,
|}>;
export type CommandsFunctionTypeParamAnnotation = $ReadOnly<{|
name: string,
typeAnnotation: CommandsTypeAnnotation,
typeAnnotation: CommandsParamTypeAnnotation,
|}>;
export type CommandsTypeAnnotation =
type CommandsParamTypeAnnotation =
| ReservedTypeAnnotation
| BooleanTypeAnnotation
| Int32TypeAnnotation
@@ -273,10 +259,9 @@ export type NativeModuleMethodParamSchema = NamedShape<
Nullable<NativeModuleParamTypeAnnotation>,
>;
export type NativeModuleObjectTypeAnnotation = $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<NativeModuleObjectTypeAnnotationPropertySchema>,
|}>;
export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation<
Nullable<NativeModuleBaseTypeAnnotation>,
>;
export type NativeModuleObjectTypeAnnotationPropertySchema = NamedShape<
Nullable<NativeModuleBaseTypeAnnotation>,
@@ -9,7 +9,7 @@
*/
'use strict';
import type {PropTypeShape} from '../../CodegenSchema';
import type {NamedShape, PropTypeAnnotation} from '../../CodegenSchema';
function upperCaseFirst(inString: string): string {
if (inString.length === 0) {
@@ -55,7 +55,9 @@ function getCppTypeForAnnotation(
}
}
function getImports(properties: $ReadOnlyArray<PropTypeShape>): Set<string> {
function getImports(
properties: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
): Set<string> {
const imports: Set<string> = new Set();
function addImportsForNativeName(name) {
@@ -122,7 +124,7 @@ function getEnumMaskName(enumName: string): string {
function convertDefaultTypeToString(
componentName: string,
prop: PropTypeShape,
prop: NamedShape<PropTypeAnnotation>,
): string {
const typeAnnotation = prop.typeAnnotation;
switch (typeAnnotation.type) {
@@ -11,7 +11,8 @@
'use strict';
import type {
CommandTypeShape,
NamedShape,
CommandsTypeAnnotation,
ComponentShape,
SchemaType,
CommandsFunctionTypeParamAnnotation,
@@ -273,7 +274,7 @@ function generateConvertAndValidateParam(
}
function generateCommandIfCase(
command: CommandTypeShape,
command: NamedShape<CommandsTypeAnnotation>,
componentName: string,
) {
const params = command.typeAnnotation.params;
@@ -14,7 +14,8 @@ const {generateEventStructName} = require('./CppHelpers.js');
import type {
ComponentShape,
EventObjectPropertyType,
NamedShape,
EventTypeAnnotation,
SchemaType,
} from '../../CodegenSchema';
@@ -81,7 +82,7 @@ function generateEnumSetter(variableName, propertyName, propertyParts) {
function generateSetters(
parentPropertyName: string,
properties: $ReadOnlyArray<EventObjectPropertyType>,
properties: $ReadOnlyArray<NamedShape<EventTypeAnnotation>>,
propertyParts: $ReadOnlyArray<string>,
): string {
const propSetters = properties
@@ -21,7 +21,8 @@ const {
import type {
ComponentShape,
EventTypeShape,
EventObjectPropertyType,
NamedShape,
EventTypeAnnotation,
SchemaType,
} from '../../CodegenSchema';
@@ -99,7 +100,7 @@ function indent(nice: string, spaces: number) {
function getNativeTypeFromAnnotation(
componentName: string,
eventProperty: EventObjectPropertyType,
eventProperty: NamedShape<EventTypeAnnotation>,
nameParts: $ReadOnlyArray<string>,
): string {
const {type} = eventProperty.typeAnnotation;
@@ -148,7 +149,7 @@ function generateStruct(
structs: StructsMap,
componentName: string,
nameParts: $ReadOnlyArray<string>,
properties: $ReadOnlyArray<EventObjectPropertyType>,
properties: $ReadOnlyArray<NamedShape<EventTypeAnnotation>>,
): void {
const structNameParts = nameParts;
const structName = generateEventStructName(structNameParts);
@@ -23,7 +23,8 @@ const {
import type {
ExtendsPropsShape,
PropTypeShape,
NamedShape,
PropTypeAnnotation,
SchemaType,
} from '../../CodegenSchema';
@@ -447,7 +448,7 @@ function generateEnumString(componentName: string, component): string {
function generatePropsString(
componentName: string,
props: $ReadOnlyArray<PropTypeShape>,
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
) {
return props
.map(prop => {
@@ -487,7 +488,7 @@ function getExtendsImports(
}
function getLocalImports(
properties: $ReadOnlyArray<PropTypeShape>,
properties: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
): Set<string> {
const imports: Set<string> = new Set();
@@ -677,7 +678,7 @@ function generateStruct(
structs: StructsMap,
componentName: string,
nameParts: $ReadOnlyArray<string>,
properties: $ReadOnlyArray<PropTypeShape>,
properties: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
): void {
const structNameParts = nameParts;
const structName = generateStructName(componentName, structNameParts);
@@ -692,7 +693,7 @@ function generateStruct(
})
.join('\n' + ' ');
properties.forEach((property: PropTypeShape) => {
properties.forEach((property: NamedShape<PropTypeAnnotation>) => {
const name = property.name;
switch (property.typeAnnotation.type) {
case 'BooleanTypeAnnotation':
@@ -11,9 +11,10 @@
'use strict';
import type {
CommandTypeShape,
NamedShape,
CommandsTypeAnnotation,
ComponentShape,
PropTypeShape,
PropTypeAnnotation,
SchemaType,
} from '../../CodegenSchema';
const {
@@ -64,7 +65,7 @@ const commandsTemplate = `
`;
function getJavaValueForProp(
prop: PropTypeShape,
prop: NamedShape<PropTypeAnnotation>,
componentName: string,
): string {
const typeAnnotation = prop.typeAnnotation;
@@ -181,7 +182,9 @@ function getCommandArgJavaType(param, index) {
}
}
function getCommandArguments(command: CommandTypeShape): string {
function getCommandArguments(
command: NamedShape<CommandsTypeAnnotation>,
): string {
return [
'view',
...command.typeAnnotation.params.map(getCommandArgJavaType),
@@ -11,9 +11,10 @@
'use strict';
import type {
CommandTypeShape,
NamedShape,
CommandsTypeAnnotation,
ComponentShape,
PropTypeShape,
PropTypeAnnotation,
SchemaType,
} from '../../CodegenSchema';
const {
@@ -47,7 +48,10 @@ function addNullable(imports) {
imports.add('import androidx.annotation.Nullable;');
}
function getJavaValueForProp(prop: PropTypeShape, imports): string {
function getJavaValueForProp(
prop: NamedShape<PropTypeAnnotation>,
imports,
): string {
const typeAnnotation = prop.typeAnnotation;
switch (typeAnnotation.type) {
@@ -153,7 +157,7 @@ function getCommandArgJavaType(param) {
}
function getCommandArguments(
command: CommandTypeShape,
command: NamedShape<CommandsTypeAnnotation>,
componentName: string,
): string {
return [
@@ -10,7 +10,10 @@
'use strict';
import type {CommandTypeShape} from '../../../CodegenSchema.js';
import type {
NamedShape,
CommandsTypeAnnotation,
} from '../../../CodegenSchema.js';
import type {TypeDeclarationMap} from '../utils.js';
const {getValueFromTypes} = require('../utils.js');
@@ -99,7 +102,7 @@ function buildCommandSchema(property, types: TypeDeclarationMap) {
function getCommands(
commandTypeAST: $ReadOnlyArray<EventTypeAST>,
types: TypeDeclarationMap,
): $ReadOnlyArray<CommandTypeShape> {
): $ReadOnlyArray<NamedShape<CommandsTypeAnnotation>> {
return commandTypeAST
.filter(property => property.type === 'ObjectTypeProperty')
.map(property => buildCommandSchema(property, types))
@@ -12,14 +12,15 @@
import type {
EventTypeShape,
EventObjectPropertyType,
NamedShape,
EventTypeAnnotation,
} from '../../../CodegenSchema.js';
function getPropertyType(
name,
optional,
typeAnnotation,
): EventObjectPropertyType {
): NamedShape<EventTypeAnnotation> {
const type =
typeAnnotation.type === 'GenericTypeAnnotation'
? typeAnnotation.id.name
@@ -150,7 +151,7 @@ function findEventArgumentsAndType(
}
}
function buildPropertiesForEvent(property): EventObjectPropertyType {
function buildPropertiesForEvent(property): NamedShape<EventTypeAnnotation> {
const name = property.key.name;
const optional =
property.value.type === 'NullableTypeAnnotation' || property.optional;
@@ -12,7 +12,7 @@
const {getValueFromTypes} = require('../utils.js');
import type {PropTypeShape} from '../../../CodegenSchema.js';
import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js';
import type {TypeDeclarationMap} from '../utils.js';
function getPropProperties(
@@ -324,7 +324,10 @@ function getTypeAnnotation(
}
}
function buildPropSchema(property, types: TypeDeclarationMap): ?PropTypeShape {
function buildPropSchema(
property,
types: TypeDeclarationMap,
): ?NamedShape<PropTypeAnnotation> {
const name = property.key.name;
const value = getValueFromTypes(property.value, types);
@@ -460,7 +463,7 @@ function flattenProperties(
function getProps(
typeDefinition: $ReadOnlyArray<PropAST>,
types: TypeDeclarationMap,
): $ReadOnlyArray<PropTypeShape> {
): $ReadOnlyArray<NamedShape<PropTypeAnnotation>> {
return flattenProperties(typeDefinition, types)
.map(property => buildPropSchema(property, types))
.filter(Boolean);
@@ -12,8 +12,9 @@
import type {
EventTypeShape,
PropTypeShape,
CommandTypeShape,
NamedShape,
CommandsTypeAnnotation,
PropTypeAnnotation,
ExtendsPropsShape,
SchemaType,
OptionsShape,
@@ -24,8 +25,8 @@ export type ComponentSchemaBuilderConfig = $ReadOnly<{|
componentName: string,
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
events: $ReadOnlyArray<EventTypeShape>,
props: $ReadOnlyArray<PropTypeShape>,
commands: $ReadOnlyArray<CommandTypeShape>,
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
commands: $ReadOnlyArray<NamedShape<CommandsTypeAnnotation>>,
options?: ?OptionsShape,
|}>;