Skip to content

Commit 1d87279

Browse files
siddarthkayfacebook-github-bot
authored andcommitted
add emitBuildEventSchema to parser commons (#38810)
Summary: [Codegen 119] This PR introduces `emitBuildEventSchema` to parser commons and abstracts the logic out of typescript and parser events as requested on #34872 ## Changelog: [Internal] [Changed] - Add `emitBuildEventSchema` to parser commons and update usages. Pull Request resolved: #38810 Test Plan: Run `yarn jest react-native-codegen` locally and ensure CI is green ## Screenshot of test passing locally: <img width="1047" alt="Screenshot 2023-08-07 at 8 03 49 AM" src="https://github.com/facebook/react-native/assets/64726664/ab87f004-d9f8-4b5f-800b-0e724430301f"> Reviewed By: rshest Differential Revision: D48155051 Pulled By: cipolleschi fbshipit-source-id: a3db3f4783d60681b366a871b1d2ee32eb864bc0
1 parent e563f0f commit 1d87279

File tree

3 files changed

+55
-54
lines changed

3 files changed

+55
-54
lines changed

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

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
getEventArgument,
2828
buildPropertiesForEvent,
2929
handleEventHandler,
30+
emitBuildEventSchema,
3031
} = require('../../parsers-commons');
3132
const {
3233
emitBoolProp,
@@ -245,36 +246,19 @@ function buildEventSchema(
245246
);
246247
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);
247248

248-
if (paperTopLevelNameDeprecated != null) {
249-
return {
250-
name,
251-
optional,
252-
bubblingType: nonNullableBubblingType,
253-
paperTopLevelNameDeprecated,
254-
typeAnnotation: {
255-
type: 'EventTypeAnnotation',
256-
argument: getEventArgument(
257-
nonNullableArgumentProps,
258-
parser,
259-
getPropertyType,
260-
),
261-
},
262-
};
263-
}
249+
const argument = getEventArgument(
250+
nonNullableArgumentProps,
251+
parser,
252+
getPropertyType,
253+
);
264254

265-
return {
255+
return emitBuildEventSchema(
256+
paperTopLevelNameDeprecated,
266257
name,
267258
optional,
268-
bubblingType: nonNullableBubblingType,
269-
typeAnnotation: {
270-
type: 'EventTypeAnnotation',
271-
argument: getEventArgument(
272-
nonNullableArgumentProps,
273-
parser,
274-
getPropertyType,
275-
),
276-
},
277-
};
259+
nonNullableBubblingType,
260+
argument,
261+
);
278262
}
279263

280264
// $FlowFixMe[unclear-type] there's no flowtype for ASTs

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
PropTypeAnnotation,
2727
EventTypeAnnotation,
2828
ObjectTypeAnnotation,
29+
EventTypeShape,
2930
} from '../CodegenSchema.js';
3031

3132
import type {Parser} from './parser';
@@ -1125,6 +1126,37 @@ function handleEventHandler(
11251126
}
11261127
}
11271128

1129+
function emitBuildEventSchema(
1130+
paperTopLevelNameDeprecated: $FlowFixMe,
1131+
name: $FlowFixMe,
1132+
optional: $FlowFixMe,
1133+
nonNullableBubblingType: 'direct' | 'bubble',
1134+
argument: ObjectTypeAnnotation<EventTypeAnnotation>,
1135+
): ?EventTypeShape {
1136+
if (paperTopLevelNameDeprecated != null) {
1137+
return {
1138+
name,
1139+
optional,
1140+
bubblingType: nonNullableBubblingType,
1141+
paperTopLevelNameDeprecated,
1142+
typeAnnotation: {
1143+
type: 'EventTypeAnnotation',
1144+
argument: argument,
1145+
},
1146+
};
1147+
}
1148+
1149+
return {
1150+
name,
1151+
optional,
1152+
bubblingType: nonNullableBubblingType,
1153+
typeAnnotation: {
1154+
type: 'EventTypeAnnotation',
1155+
argument: argument,
1156+
},
1157+
};
1158+
}
1159+
11281160
module.exports = {
11291161
wrapModuleSchema,
11301162
unwrapNullable,
@@ -1154,4 +1186,5 @@ module.exports = {
11541186
buildPropertiesForEvent,
11551187
verifyPropNotAlreadyDefined,
11561188
handleEventHandler,
1189+
emitBuildEventSchema,
11571190
};

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

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
getEventArgument,
2929
buildPropertiesForEvent,
3030
handleEventHandler,
31+
emitBuildEventSchema,
3132
} = require('../../parsers-commons');
3233
const {
3334
emitBoolProp,
@@ -259,36 +260,19 @@ function buildEventSchema(
259260
);
260261
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);
261262

262-
if (paperTopLevelNameDeprecated != null) {
263-
return {
264-
name,
265-
optional,
266-
bubblingType: nonNullableBubblingType,
267-
paperTopLevelNameDeprecated,
268-
typeAnnotation: {
269-
type: 'EventTypeAnnotation',
270-
argument: getEventArgument(
271-
nonNullableArgumentProps,
272-
parser,
273-
getPropertyType,
274-
),
275-
},
276-
};
277-
}
263+
const argument = getEventArgument(
264+
nonNullableArgumentProps,
265+
parser,
266+
getPropertyType,
267+
);
278268

279-
return {
269+
return emitBuildEventSchema(
270+
paperTopLevelNameDeprecated,
280271
name,
281272
optional,
282-
bubblingType: nonNullableBubblingType,
283-
typeAnnotation: {
284-
type: 'EventTypeAnnotation',
285-
argument: getEventArgument(
286-
nonNullableArgumentProps,
287-
parser,
288-
getPropertyType,
289-
),
290-
},
291-
};
273+
nonNullableBubblingType,
274+
argument,
275+
);
292276
}
293277

294278
function getEvents(

0 commit comments

Comments
 (0)