Skip to content

Commit 0e46080

Browse files
RSNarafacebook-github-bot
authored andcommitted
Clean up EventObjectPropertyType
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
1 parent 3e77e15 commit 0e46080

File tree

6 files changed

+4990
-3255
lines changed

6 files changed

+4990
-3255
lines changed

packages/react-native-codegen/src/CodegenSchema.js

+26-54
Original file line numberDiff line numberDiff line change
@@ -55,46 +55,28 @@ export type StringTypeAnnotation = $ReadOnly<{|
5555
type: 'StringTypeAnnotation',
5656
|}>;
5757

58-
export type EventObjectPropertyType =
59-
| $ReadOnly<{|
60-
type: 'BooleanTypeAnnotation',
61-
name: string,
62-
optional: boolean,
63-
|}>
64-
| $ReadOnly<{|
65-
type: 'StringTypeAnnotation',
66-
name: string,
67-
optional: boolean,
68-
|}>
69-
| $ReadOnly<{|
70-
type: 'DoubleTypeAnnotation',
71-
name: string,
72-
optional: boolean,
73-
|}>
74-
| $ReadOnly<{|
75-
type: 'FloatTypeAnnotation',
76-
name: string,
77-
optional: boolean,
78-
|}>
79-
| $ReadOnly<{|
80-
type: 'Int32TypeAnnotation',
81-
name: string,
82-
optional: boolean,
83-
|}>
84-
| $ReadOnly<{|
85-
type: 'StringEnumTypeAnnotation',
86-
name: string,
87-
optional: boolean,
88-
options: $ReadOnlyArray<{|
89-
name: string,
58+
export type StringEnumTypeAnnotation = $ReadOnly<{|
59+
type: 'StringEnumTypeAnnotation',
60+
options: $ReadOnlyArray<{|
61+
name: string,
62+
|}>,
63+
|}>;
64+
65+
export type EventObjectPropertyType = $ReadOnly<{|
66+
name: string,
67+
optional: boolean,
68+
typeAnnotation:
69+
| BooleanTypeAnnotation
70+
| StringTypeAnnotation
71+
| DoubleTypeAnnotation
72+
| FloatTypeAnnotation
73+
| Int32TypeAnnotation
74+
| StringEnumTypeAnnotation
75+
| $ReadOnly<{|
76+
type: 'ObjectTypeAnnotation',
77+
properties: $ReadOnlyArray<EventObjectPropertyType>,
9078
|}>,
91-
|}>
92-
| $ReadOnly<{|
93-
type: 'ObjectTypeAnnotation',
94-
name: string,
95-
optional: boolean,
96-
properties: $ReadOnlyArray<EventObjectPropertyType>,
97-
|}>;
79+
|}>;
9880

9981
type PropTypeTypeAnnotation =
10082
| $ReadOnly<{|
@@ -146,21 +128,11 @@ type PropTypeTypeAnnotation =
146128
| $ReadOnly<{|
147129
type: 'ArrayTypeAnnotation',
148130
elementType:
149-
| $ReadOnly<{|
150-
type: 'BooleanTypeAnnotation',
151-
|}>
152-
| $ReadOnly<{|
153-
type: 'StringTypeAnnotation',
154-
|}>
155-
| $ReadOnly<{|
156-
type: 'DoubleTypeAnnotation',
157-
|}>
158-
| $ReadOnly<{|
159-
type: 'FloatTypeAnnotation',
160-
|}>
161-
| $ReadOnly<{|
162-
type: 'Int32TypeAnnotation',
163-
|}>
131+
| BooleanTypeAnnotation
132+
| StringTypeAnnotation
133+
| DoubleTypeAnnotation
134+
| FloatTypeAnnotation
135+
| Int32TypeAnnotation
164136
| $ReadOnly<{|
165137
type: 'StringEnumTypeAnnotation',
166138
default: string,

packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,32 @@ function generateSetters(
8686
): string {
8787
const propSetters = properties
8888
.map(eventProperty => {
89-
switch (eventProperty.type) {
89+
const {typeAnnotation} = eventProperty;
90+
switch (typeAnnotation.type) {
9091
case 'BooleanTypeAnnotation':
92+
return generateSetter(
93+
parentPropertyName,
94+
eventProperty.name,
95+
propertyParts,
96+
);
9197
case 'StringTypeAnnotation':
98+
return generateSetter(
99+
parentPropertyName,
100+
eventProperty.name,
101+
propertyParts,
102+
);
92103
case 'Int32TypeAnnotation':
104+
return generateSetter(
105+
parentPropertyName,
106+
eventProperty.name,
107+
propertyParts,
108+
);
93109
case 'DoubleTypeAnnotation':
110+
return generateSetter(
111+
parentPropertyName,
112+
eventProperty.name,
113+
propertyParts,
114+
);
94115
case 'FloatTypeAnnotation':
95116
return generateSetter(
96117
parentPropertyName,
@@ -110,15 +131,15 @@ function generateSetters(
110131
auto ${propertyName} = jsi::Object(runtime);
111132
${generateSetters(
112133
propertyName,
113-
eventProperty.properties,
134+
typeAnnotation.properties,
114135
propertyParts.concat([propertyName]),
115136
)}
116137
117138
${parentPropertyName}.setProperty(runtime, "${propertyName}", ${propertyName});
118139
}
119140
`.trim();
120141
default:
121-
(eventProperty: empty);
142+
(typeAnnotation.type: empty);
122143
throw new Error('Received invalid event property type');
123144
}
124145
})

packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function getNativeTypeFromAnnotation(
102102
eventProperty: EventObjectPropertyType,
103103
nameParts: $ReadOnlyArray<string>,
104104
): string {
105-
const type = eventProperty.type;
105+
const {type} = eventProperty.typeAnnotation;
106106

107107
switch (type) {
108108
case 'BooleanTypeAnnotation':
@@ -163,30 +163,34 @@ function generateStruct(
163163
})
164164
.join('\n' + ' ');
165165

166-
properties.forEach((property: EventObjectPropertyType) => {
167-
const name = property.name;
168-
switch (property.type) {
166+
properties.forEach(property => {
167+
const {name, typeAnnotation} = property;
168+
switch (typeAnnotation.type) {
169169
case 'BooleanTypeAnnotation':
170+
return;
170171
case 'StringTypeAnnotation':
172+
return;
171173
case 'Int32TypeAnnotation':
174+
return;
172175
case 'DoubleTypeAnnotation':
176+
return;
173177
case 'FloatTypeAnnotation':
174178
return;
175179
case 'ObjectTypeAnnotation':
176180
generateStruct(
177181
structs,
178182
componentName,
179183
nameParts.concat([name]),
180-
nullthrows(property.properties),
184+
nullthrows(typeAnnotation.properties),
181185
);
182186
return;
183187
case 'StringEnumTypeAnnotation':
184-
generateEnum(structs, property.options, nameParts.concat([name]));
188+
generateEnum(structs, typeAnnotation.options, nameParts.concat([name]));
185189
return;
186190
default:
187-
(property: empty);
191+
(typeAnnotation.type: empty);
188192
throw new Error(
189-
`Received invalid event property type ${property.type}`,
193+
`Received invalid event property type ${typeAnnotation.type}`,
190194
);
191195
}
192196
});

packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js

+69-41
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ const INTERFACE_ONLY: SchemaType = {
5858
type: 'ObjectTypeAnnotation',
5959
properties: [
6060
{
61-
type: 'BooleanTypeAnnotation',
6261
name: 'value',
6362
optional: false,
63+
typeAnnotation: {
64+
type: 'BooleanTypeAnnotation',
65+
},
6466
},
6567
],
6668
},
@@ -110,9 +112,11 @@ const EVENTS_WITH_PAPER_NAME: SchemaType = {
110112
type: 'ObjectTypeAnnotation',
111113
properties: [
112114
{
113-
type: 'BooleanTypeAnnotation',
114115
name: 'value',
115116
optional: false,
117+
typeAnnotation: {
118+
type: 'BooleanTypeAnnotation',
119+
},
116120
},
117121
],
118122
},
@@ -129,9 +133,11 @@ const EVENTS_WITH_PAPER_NAME: SchemaType = {
129133
type: 'ObjectTypeAnnotation',
130134
properties: [
131135
{
132-
type: 'BooleanTypeAnnotation',
133136
name: 'value',
134137
optional: false,
138+
typeAnnotation: {
139+
type: 'BooleanTypeAnnotation',
140+
},
135141
},
136142
],
137143
},
@@ -1134,24 +1140,32 @@ const EVENT_PROPS: SchemaType = {
11341140
type: 'ObjectTypeAnnotation',
11351141
properties: [
11361142
{
1137-
type: 'BooleanTypeAnnotation',
11381143
name: 'value',
11391144
optional: false,
1145+
typeAnnotation: {
1146+
type: 'BooleanTypeAnnotation',
1147+
},
11401148
},
11411149
{
1142-
type: 'StringTypeAnnotation',
11431150
name: 'source',
11441151
optional: true,
1152+
typeAnnotation: {
1153+
type: 'StringTypeAnnotation',
1154+
},
11451155
},
11461156
{
1147-
type: 'Int32TypeAnnotation',
11481157
name: 'progress',
11491158
optional: true,
1159+
typeAnnotation: {
1160+
type: 'Int32TypeAnnotation',
1161+
},
11501162
},
11511163
{
1152-
type: 'FloatTypeAnnotation',
11531164
name: 'scale',
11541165
optional: true,
1166+
typeAnnotation: {
1167+
type: 'FloatTypeAnnotation',
1168+
},
11551169
},
11561170
],
11571171
},
@@ -1167,9 +1181,11 @@ const EVENT_PROPS: SchemaType = {
11671181
type: 'ObjectTypeAnnotation',
11681182
properties: [
11691183
{
1170-
type: 'BooleanTypeAnnotation',
11711184
name: 'value',
11721185
optional: false,
1186+
typeAnnotation: {
1187+
type: 'BooleanTypeAnnotation',
1188+
},
11731189
},
11741190
],
11751191
},
@@ -1185,17 +1201,19 @@ const EVENT_PROPS: SchemaType = {
11851201
type: 'ObjectTypeAnnotation',
11861202
properties: [
11871203
{
1188-
type: 'StringEnumTypeAnnotation',
11891204
name: 'orientation',
11901205
optional: false,
1191-
options: [
1192-
{
1193-
name: 'landscape',
1194-
},
1195-
{
1196-
name: 'portrait',
1197-
},
1198-
],
1206+
typeAnnotation: {
1207+
type: 'StringEnumTypeAnnotation',
1208+
options: [
1209+
{
1210+
name: 'landscape',
1211+
},
1212+
{
1213+
name: 'portrait',
1214+
},
1215+
],
1216+
},
11991217
},
12001218
],
12011219
},
@@ -1250,33 +1268,43 @@ const EVENT_NESTED_OBJECT_PROPS: SchemaType = {
12501268
type: 'ObjectTypeAnnotation',
12511269
properties: [
12521270
{
1253-
type: 'ObjectTypeAnnotation',
12541271
name: 'location',
12551272
optional: false,
1256-
properties: [
1257-
{
1258-
type: 'ObjectTypeAnnotation',
1259-
name: 'source',
1260-
optional: false,
1261-
properties: [
1262-
{
1263-
type: 'StringTypeAnnotation',
1264-
name: 'url',
1265-
optional: false,
1273+
typeAnnotation: {
1274+
type: 'ObjectTypeAnnotation',
1275+
properties: [
1276+
{
1277+
name: 'source',
1278+
optional: false,
1279+
typeAnnotation: {
1280+
type: 'ObjectTypeAnnotation',
1281+
properties: [
1282+
{
1283+
name: 'url',
1284+
optional: false,
1285+
typeAnnotation: {
1286+
type: 'StringTypeAnnotation',
1287+
},
1288+
},
1289+
],
12661290
},
1267-
],
1268-
},
1269-
{
1270-
type: 'Int32TypeAnnotation',
1271-
name: 'x',
1272-
optional: false,
1273-
},
1274-
{
1275-
type: 'Int32TypeAnnotation',
1276-
name: 'y',
1277-
optional: false,
1278-
},
1279-
],
1291+
},
1292+
{
1293+
name: 'x',
1294+
optional: false,
1295+
typeAnnotation: {
1296+
type: 'Int32TypeAnnotation',
1297+
},
1298+
},
1299+
{
1300+
name: 'y',
1301+
optional: false,
1302+
typeAnnotation: {
1303+
type: 'Int32TypeAnnotation',
1304+
},
1305+
},
1306+
],
1307+
},
12801308
},
12811309
],
12821310
},

0 commit comments

Comments
 (0)