Skip to content

Commit d8924b0

Browse files
committed
Revert "move getEventArgument fn to parsers-commons.js"
This reverts commit ce660bd.
1 parent f516d2e commit d8924b0

File tree

7 files changed

+238
-365
lines changed

7 files changed

+238
-365
lines changed

packages/react-native-codegen/src/parsers/flow/components/events.js

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,104 @@
1010

1111
'use strict';
1212

13-
import type {EventTypeShape} from '../../../CodegenSchema.js';
13+
import type {
14+
EventTypeShape,
15+
NamedShape,
16+
EventTypeAnnotation,
17+
} from '../../../CodegenSchema.js';
1418
import type {Parser} from '../../parser';
1519
const {throwIfEventHasNoName} = require('../../error-utils');
16-
const {getEventArgument} = require('../../parsers-commons');
20+
21+
function getPropertyType(
22+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
23+
* LTI update could not be added via codemod */
24+
name,
25+
optional: boolean,
26+
typeAnnotation: $FlowFixMe,
27+
): NamedShape<EventTypeAnnotation> {
28+
const type =
29+
typeAnnotation.type === 'GenericTypeAnnotation'
30+
? typeAnnotation.id.name
31+
: typeAnnotation.type;
32+
33+
switch (type) {
34+
case 'BooleanTypeAnnotation':
35+
return {
36+
name,
37+
optional,
38+
typeAnnotation: {
39+
type: 'BooleanTypeAnnotation',
40+
},
41+
};
42+
case 'StringTypeAnnotation':
43+
return {
44+
name,
45+
optional,
46+
typeAnnotation: {
47+
type: 'StringTypeAnnotation',
48+
},
49+
};
50+
case 'Int32':
51+
return {
52+
name,
53+
optional,
54+
typeAnnotation: {
55+
type: 'Int32TypeAnnotation',
56+
},
57+
};
58+
case 'Double':
59+
return {
60+
name,
61+
optional,
62+
typeAnnotation: {
63+
type: 'DoubleTypeAnnotation',
64+
},
65+
};
66+
case 'Float':
67+
return {
68+
name,
69+
optional,
70+
typeAnnotation: {
71+
type: 'FloatTypeAnnotation',
72+
},
73+
};
74+
case '$ReadOnly':
75+
return getPropertyType(
76+
name,
77+
optional,
78+
typeAnnotation.typeParameters.params[0],
79+
);
80+
case 'ObjectTypeAnnotation':
81+
return {
82+
name,
83+
optional,
84+
typeAnnotation: {
85+
type: 'ObjectTypeAnnotation',
86+
properties: typeAnnotation.properties.map(buildPropertiesForEvent),
87+
},
88+
};
89+
case 'UnionTypeAnnotation':
90+
return {
91+
name,
92+
optional,
93+
typeAnnotation: {
94+
type: 'StringEnumTypeAnnotation',
95+
options: typeAnnotation.types.map(option => option.value),
96+
},
97+
};
98+
case 'UnsafeMixed':
99+
return {
100+
name,
101+
optional,
102+
typeAnnotation: {
103+
type: 'MixedTypeAnnotation',
104+
},
105+
};
106+
default:
107+
(type: empty);
108+
throw new Error(`Unable to determine event type for "${name}": ${type}`);
109+
}
110+
}
17111

18112
function findEventArgumentsAndType(
19113
parser: Parser,
@@ -74,6 +168,29 @@ function findEventArgumentsAndType(
74168
}
75169
}
76170

171+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
172+
* LTI update could not be added via codemod */
173+
function buildPropertiesForEvent(property): NamedShape<EventTypeAnnotation> {
174+
const name = property.key.name;
175+
const optional =
176+
property.value.type === 'NullableTypeAnnotation' || property.optional;
177+
let typeAnnotation =
178+
property.value.type === 'NullableTypeAnnotation'
179+
? property.value.typeAnnotation
180+
: property.value;
181+
182+
return getPropertyType(name, optional, typeAnnotation);
183+
}
184+
185+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
186+
* LTI update could not be added via codemod */
187+
function getEventArgument(argumentProps, name: $FlowFixMe) {
188+
return {
189+
type: 'ObjectTypeAnnotation',
190+
properties: argumentProps.map(buildPropertiesForEvent),
191+
};
192+
}
193+
77194
function buildEventSchema(
78195
types: TypeMap,
79196
property: EventTypeAST,
@@ -108,7 +225,7 @@ function buildEventSchema(
108225
paperTopLevelNameDeprecated,
109226
typeAnnotation: {
110227
type: 'EventTypeAnnotation',
111-
argument: getEventArgument(argumentProps, name, parser),
228+
argument: getEventArgument(argumentProps, name),
112229
},
113230
};
114231
}
@@ -119,7 +236,7 @@ function buildEventSchema(
119236
bubblingType,
120237
typeAnnotation: {
121238
type: 'EventTypeAnnotation',
122-
argument: getEventArgument(argumentProps, name, parser),
239+
argument: getEventArgument(argumentProps, name),
123240
},
124241
};
125242
}

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import type {
2020
NativeModuleEnumMembers,
2121
NativeModuleAliasMap,
2222
NativeModuleEnumMap,
23-
EventTypeAnnotation,
2423
} from '../../CodegenSchema';
2524
import type {ParserType} from '../errors';
2625
import type {Parser} from '../parser';
@@ -338,113 +337,6 @@ class FlowParser implements Parser {
338337
nameForArgument(prop: PropAST): $FlowFixMe {
339338
return prop.argument.id.name;
340339
}
341-
342-
getPropertyType(
343-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
344-
* LTI update could not be added via codemod */
345-
name: string,
346-
optional: boolean,
347-
typeAnnotation: $FlowFixMe,
348-
): NamedShape<EventTypeAnnotation> {
349-
const type =
350-
typeAnnotation.type === 'GenericTypeAnnotation'
351-
? typeAnnotation.id.name
352-
: typeAnnotation.type;
353-
354-
switch (type) {
355-
case 'BooleanTypeAnnotation':
356-
return {
357-
name,
358-
optional,
359-
typeAnnotation: {
360-
type: 'BooleanTypeAnnotation',
361-
},
362-
};
363-
case 'StringTypeAnnotation':
364-
return {
365-
name,
366-
optional,
367-
typeAnnotation: {
368-
type: 'StringTypeAnnotation',
369-
},
370-
};
371-
case 'Int32':
372-
return {
373-
name,
374-
optional,
375-
typeAnnotation: {
376-
type: 'Int32TypeAnnotation',
377-
},
378-
};
379-
case 'Double':
380-
return {
381-
name,
382-
optional,
383-
typeAnnotation: {
384-
type: 'DoubleTypeAnnotation',
385-
},
386-
};
387-
case 'Float':
388-
return {
389-
name,
390-
optional,
391-
typeAnnotation: {
392-
type: 'FloatTypeAnnotation',
393-
},
394-
};
395-
case '$ReadOnly':
396-
return this.getPropertyType(
397-
name,
398-
optional,
399-
typeAnnotation.typeParameters.params[0],
400-
);
401-
case 'ObjectTypeAnnotation':
402-
return {
403-
name,
404-
optional,
405-
typeAnnotation: {
406-
type: 'ObjectTypeAnnotation',
407-
properties: typeAnnotation.properties.map(property =>
408-
this.buildPropertiesForEvent(property),
409-
),
410-
},
411-
};
412-
case 'UnionTypeAnnotation':
413-
return {
414-
name,
415-
optional,
416-
typeAnnotation: {
417-
type: 'StringEnumTypeAnnotation',
418-
options: typeAnnotation.types.map(option => option.value),
419-
},
420-
};
421-
case 'UnsafeMixed':
422-
return {
423-
name,
424-
optional,
425-
typeAnnotation: {
426-
type: 'MixedTypeAnnotation',
427-
},
428-
};
429-
default:
430-
(type: empty);
431-
throw new Error(
432-
`Unable to determine event type for "${name}": ${type}`,
433-
);
434-
}
435-
}
436-
437-
buildPropertiesForEvent(property: PropAST): NamedShape<EventTypeAnnotation> {
438-
const name = property.key.name;
439-
const optional =
440-
property.value.type === 'NullableTypeAnnotation' || property.optional;
441-
let typeAnnotation =
442-
property.value.type === 'NullableTypeAnnotation'
443-
? property.value.typeAnnotation
444-
: property.value;
445-
446-
return this.getPropertyType(name, optional, typeAnnotation);
447-
}
448340
}
449341

450342
module.exports = {

packages/react-native-codegen/src/parsers/parser.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import type {
2020
NativeModuleEnumMembers,
2121
NativeModuleAliasMap,
2222
NativeModuleEnumMap,
23-
EventTypeAnnotation,
2423
} from '../CodegenSchema';
2524
import type {ParserType} from './errors';
2625
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
@@ -270,16 +269,4 @@ export interface Parser {
270269
* @returns: name property
271270
*/
272271
nameForArgument(prop: PropAST): $FlowFixMe;
273-
274-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
275-
* LTI update could not be added via codemod */
276-
buildPropertiesForEvent(prop: PropAST): NamedShape<EventTypeAnnotation>;
277-
278-
getPropertyType(
279-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
280-
* LTI update could not be added via codemod */
281-
name: string,
282-
optional: boolean,
283-
typeAnnotation: $FlowFixMe,
284-
): NamedShape<EventTypeAnnotation>;
285272
}

0 commit comments

Comments
 (0)