Skip to content

Commit

Permalink
Clean up EventObjectPropertyType
Browse files Browse the repository at this point in the history
Summary:
All ObjectTypeAnnotation *properties* in the codegen have the following shape:
```
{
  name: string,
  optional: boolean,
  typeAnnotation: ...
}
```

EventObjectTypeProperty is a property of some ObjectTypeAnnotation, yet it doesn't follow this pattern. This diff cleans up EventObjectPropertyType. This is a part of a larger effort to clean up the Component Schema and unify the notion of a "type annotation" across the Component and Module schemas.

Reviewed By: yungsters

Differential Revision: D24701027

fbshipit-source-id: edc7dc632a217fb5a82ffd8a62aef990baf398c2
  • Loading branch information
RSNara authored and facebook-github-bot committed Nov 6, 2020
1 parent 3e77e15 commit 0e46080
Show file tree
Hide file tree
Showing 6 changed files with 4,990 additions and 3,255 deletions.
80 changes: 26 additions & 54 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,46 +55,28 @@ export type StringTypeAnnotation = $ReadOnly<{|
type: 'StringTypeAnnotation',
|}>;

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,
export type StringEnumTypeAnnotation = $ReadOnly<{|
type: 'StringEnumTypeAnnotation',
options: $ReadOnlyArray<{|
name: string,
|}>,
|}>;

export type EventObjectPropertyType = $ReadOnly<{|
name: string,
optional: boolean,
typeAnnotation:
| BooleanTypeAnnotation
| StringTypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
| Int32TypeAnnotation
| StringEnumTypeAnnotation
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<EventObjectPropertyType>,
|}>,
|}>
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
name: string,
optional: boolean,
properties: $ReadOnlyArray<EventObjectPropertyType>,
|}>;
|}>;

type PropTypeTypeAnnotation =
| $ReadOnly<{|
Expand Down Expand Up @@ -146,21 +128,11 @@ type PropTypeTypeAnnotation =
| $ReadOnly<{|
type: 'ArrayTypeAnnotation',
elementType:
| $ReadOnly<{|
type: 'BooleanTypeAnnotation',
|}>
| $ReadOnly<{|
type: 'StringTypeAnnotation',
|}>
| $ReadOnly<{|
type: 'DoubleTypeAnnotation',
|}>
| $ReadOnly<{|
type: 'FloatTypeAnnotation',
|}>
| $ReadOnly<{|
type: 'Int32TypeAnnotation',
|}>
| BooleanTypeAnnotation
| StringTypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
| Int32TypeAnnotation
| $ReadOnly<{|
type: 'StringEnumTypeAnnotation',
default: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,32 @@ function generateSetters(
): string {
const propSetters = properties
.map(eventProperty => {
switch (eventProperty.type) {
const {typeAnnotation} = eventProperty;
switch (typeAnnotation.type) {
case 'BooleanTypeAnnotation':
return generateSetter(
parentPropertyName,
eventProperty.name,
propertyParts,
);
case 'StringTypeAnnotation':
return generateSetter(
parentPropertyName,
eventProperty.name,
propertyParts,
);
case 'Int32TypeAnnotation':
return generateSetter(
parentPropertyName,
eventProperty.name,
propertyParts,
);
case 'DoubleTypeAnnotation':
return generateSetter(
parentPropertyName,
eventProperty.name,
propertyParts,
);
case 'FloatTypeAnnotation':
return generateSetter(
parentPropertyName,
Expand All @@ -110,15 +131,15 @@ function generateSetters(
auto ${propertyName} = jsi::Object(runtime);
${generateSetters(
propertyName,
eventProperty.properties,
typeAnnotation.properties,
propertyParts.concat([propertyName]),
)}
${parentPropertyName}.setProperty(runtime, "${propertyName}", ${propertyName});
}
`.trim();
default:
(eventProperty: empty);
(typeAnnotation.type: empty);
throw new Error('Received invalid event property type');
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function getNativeTypeFromAnnotation(
eventProperty: EventObjectPropertyType,
nameParts: $ReadOnlyArray<string>,
): string {
const type = eventProperty.type;
const {type} = eventProperty.typeAnnotation;

switch (type) {
case 'BooleanTypeAnnotation':
Expand Down Expand Up @@ -163,30 +163,34 @@ function generateStruct(
})
.join('\n' + ' ');

properties.forEach((property: EventObjectPropertyType) => {
const name = property.name;
switch (property.type) {
properties.forEach(property => {
const {name, typeAnnotation} = property;
switch (typeAnnotation.type) {
case 'BooleanTypeAnnotation':
return;
case 'StringTypeAnnotation':
return;
case 'Int32TypeAnnotation':
return;
case 'DoubleTypeAnnotation':
return;
case 'FloatTypeAnnotation':
return;
case 'ObjectTypeAnnotation':
generateStruct(
structs,
componentName,
nameParts.concat([name]),
nullthrows(property.properties),
nullthrows(typeAnnotation.properties),
);
return;
case 'StringEnumTypeAnnotation':
generateEnum(structs, property.options, nameParts.concat([name]));
generateEnum(structs, typeAnnotation.options, nameParts.concat([name]));
return;
default:
(property: empty);
(typeAnnotation.type: empty);
throw new Error(
`Received invalid event property type ${property.type}`,
`Received invalid event property type ${typeAnnotation.type}`,
);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ const INTERFACE_ONLY: SchemaType = {
type: 'ObjectTypeAnnotation',
properties: [
{
type: 'BooleanTypeAnnotation',
name: 'value',
optional: false,
typeAnnotation: {
type: 'BooleanTypeAnnotation',
},
},
],
},
Expand Down Expand Up @@ -110,9 +112,11 @@ const EVENTS_WITH_PAPER_NAME: SchemaType = {
type: 'ObjectTypeAnnotation',
properties: [
{
type: 'BooleanTypeAnnotation',
name: 'value',
optional: false,
typeAnnotation: {
type: 'BooleanTypeAnnotation',
},
},
],
},
Expand All @@ -129,9 +133,11 @@ const EVENTS_WITH_PAPER_NAME: SchemaType = {
type: 'ObjectTypeAnnotation',
properties: [
{
type: 'BooleanTypeAnnotation',
name: 'value',
optional: false,
typeAnnotation: {
type: 'BooleanTypeAnnotation',
},
},
],
},
Expand Down Expand Up @@ -1134,24 +1140,32 @@ const EVENT_PROPS: SchemaType = {
type: 'ObjectTypeAnnotation',
properties: [
{
type: 'BooleanTypeAnnotation',
name: 'value',
optional: false,
typeAnnotation: {
type: 'BooleanTypeAnnotation',
},
},
{
type: 'StringTypeAnnotation',
name: 'source',
optional: true,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
},
{
type: 'Int32TypeAnnotation',
name: 'progress',
optional: true,
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
},
{
type: 'FloatTypeAnnotation',
name: 'scale',
optional: true,
typeAnnotation: {
type: 'FloatTypeAnnotation',
},
},
],
},
Expand All @@ -1167,9 +1181,11 @@ const EVENT_PROPS: SchemaType = {
type: 'ObjectTypeAnnotation',
properties: [
{
type: 'BooleanTypeAnnotation',
name: 'value',
optional: false,
typeAnnotation: {
type: 'BooleanTypeAnnotation',
},
},
],
},
Expand All @@ -1185,17 +1201,19 @@ const EVENT_PROPS: SchemaType = {
type: 'ObjectTypeAnnotation',
properties: [
{
type: 'StringEnumTypeAnnotation',
name: 'orientation',
optional: false,
options: [
{
name: 'landscape',
},
{
name: 'portrait',
},
],
typeAnnotation: {
type: 'StringEnumTypeAnnotation',
options: [
{
name: 'landscape',
},
{
name: 'portrait',
},
],
},
},
],
},
Expand Down Expand Up @@ -1250,33 +1268,43 @@ const EVENT_NESTED_OBJECT_PROPS: SchemaType = {
type: 'ObjectTypeAnnotation',
properties: [
{
type: 'ObjectTypeAnnotation',
name: 'location',
optional: false,
properties: [
{
type: 'ObjectTypeAnnotation',
name: 'source',
optional: false,
properties: [
{
type: 'StringTypeAnnotation',
name: 'url',
optional: false,
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: [
{
name: 'source',
optional: false,
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: [
{
name: 'url',
optional: false,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
},
],
},
],
},
{
type: 'Int32TypeAnnotation',
name: 'x',
optional: false,
},
{
type: 'Int32TypeAnnotation',
name: 'y',
optional: false,
},
],
},
{
name: 'x',
optional: false,
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
},
{
name: 'y',
optional: false,
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
},
],
},
},
],
},
Expand Down
Loading

0 comments on commit 0e46080

Please sign in to comment.