@react-native/codegen 0.79.0-rc.0 → 0.79.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,8 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- const j = require('jscodeshift');
13
+ const core = require('@babel/core');
14
+ const t = core.types;
14
15
 
15
16
  // File path -> contents
16
17
 
@@ -36,6 +37,9 @@ ${componentConfig}
36
37
  // We use this to add to a set. Need to make sure we aren't importing
37
38
  // this multiple times.
38
39
  const UIMANAGER_IMPORT = 'const {UIManager} = require("react-native")';
40
+ function expression(input) {
41
+ return core.template.expression(input)();
42
+ }
39
43
  function getReactDiffProcessValue(typeAnnotation) {
40
44
  switch (typeAnnotation.type) {
41
45
  case 'BooleanTypeAnnotation':
@@ -47,25 +51,29 @@ function getReactDiffProcessValue(typeAnnotation) {
47
51
  case 'StringEnumTypeAnnotation':
48
52
  case 'Int32EnumTypeAnnotation':
49
53
  case 'MixedTypeAnnotation':
50
- return j.literal(true);
54
+ return t.booleanLiteral(true);
51
55
  case 'ReservedPropTypeAnnotation':
52
56
  switch (typeAnnotation.name) {
53
57
  case 'ColorPrimitive':
54
- return j.template
55
- .expression`{ process: require('react-native/Libraries/StyleSheet/processColor').default }`;
58
+ return expression(
59
+ `{ process: require('react-native/Libraries/StyleSheet/processColor').default }`,
60
+ );
56
61
  case 'ImageSourcePrimitive':
57
- return j.template
58
- .expression`{ process: require('react-native/Libraries/Image/resolveAssetSource') }`;
62
+ return expression(
63
+ `{ process: require('react-native/Libraries/Image/resolveAssetSource') }`,
64
+ );
59
65
  case 'ImageRequestPrimitive':
60
66
  throw new Error('ImageRequest should not be used in props');
61
67
  case 'PointPrimitive':
62
- return j.template
63
- .expression`{ diff: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/pointsDiffer')) }`;
68
+ return expression(
69
+ `{ diff: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/pointsDiffer')) }`,
70
+ );
64
71
  case 'EdgeInsetsPrimitive':
65
- return j.template
66
- .expression`{ diff: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/insetsDiffer')) }`;
72
+ return expression(
73
+ `{ diff: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/insetsDiffer')) }`,
74
+ );
67
75
  case 'DimensionPrimitive':
68
- return j.literal(true);
76
+ return t.booleanLiteral(true);
69
77
  default:
70
78
  typeAnnotation.name;
71
79
  throw new Error(
@@ -76,20 +84,21 @@ function getReactDiffProcessValue(typeAnnotation) {
76
84
  if (typeAnnotation.elementType.type === 'ReservedPropTypeAnnotation') {
77
85
  switch (typeAnnotation.elementType.name) {
78
86
  case 'ColorPrimitive':
79
- return j.template
80
- .expression`{ process: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/StyleSheet/processColorArray')) }`;
87
+ return expression(
88
+ `{ process: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/StyleSheet/processColorArray')) }`,
89
+ );
81
90
  case 'ImageSourcePrimitive':
82
91
  case 'PointPrimitive':
83
92
  case 'EdgeInsetsPrimitive':
84
93
  case 'DimensionPrimitive':
85
- return j.literal(true);
94
+ return t.booleanLiteral(true);
86
95
  default:
87
96
  throw new Error(
88
97
  `Received unknown array native typeAnnotation: "${typeAnnotation.elementType.name}"`,
89
98
  );
90
99
  }
91
100
  }
92
- return j.literal(true);
101
+ return t.booleanLiteral(true);
93
102
  default:
94
103
  typeAnnotation;
95
104
  throw new Error(
@@ -117,7 +126,7 @@ ${
117
126
  : ''
118
127
  }
119
128
 
120
- export const __INTERNAL_VIEW_CONFIG = VIEW_CONFIG;
129
+ export const __INTERNAL_VIEW_CONFIG = %%VIEW_CONFIG%%;
121
130
 
122
131
  export default NativeComponentRegistry.get(nativeComponentName, () => __INTERNAL_VIEW_CONFIG);
123
132
  `.trim();
@@ -155,44 +164,45 @@ function getValidAttributesForEvents(events, imports) {
155
164
  imports.add(
156
165
  "const {ConditionallyIgnoredEventHandlers} = require('react-native/Libraries/NativeComponent/ViewConfigIgnore');",
157
166
  );
158
- const validAttributes = j.objectExpression(
167
+ const validAttributes = t.objectExpression(
159
168
  events.map(eventType => {
160
- return j.property('init', j.identifier(eventType.name), j.literal(true));
169
+ return t.objectProperty(
170
+ t.identifier(eventType.name),
171
+ t.booleanLiteral(true),
172
+ );
161
173
  }),
162
174
  );
163
- return j.callExpression(j.identifier('ConditionallyIgnoredEventHandlers'), [
175
+ return t.callExpression(t.identifier('ConditionallyIgnoredEventHandlers'), [
164
176
  validAttributes,
165
177
  ]);
166
178
  }
167
179
  function generateBubblingEventInfo(event, nameOveride) {
168
- return j.property(
169
- 'init',
170
- j.identifier(normalizeInputEventName(nameOveride || event.name)),
171
- j.objectExpression([
172
- j.property(
173
- 'init',
174
- j.identifier('phasedRegistrationNames'),
175
- j.objectExpression([
176
- j.property(
177
- 'init',
178
- j.identifier('captured'),
179
- j.literal(`${event.name}Capture`),
180
+ return t.objectProperty(
181
+ t.identifier(normalizeInputEventName(nameOveride || event.name)),
182
+ t.objectExpression([
183
+ t.objectProperty(
184
+ t.identifier('phasedRegistrationNames'),
185
+ t.objectExpression([
186
+ t.objectProperty(
187
+ t.identifier('captured'),
188
+ t.stringLiteral(`${event.name}Capture`),
189
+ ),
190
+ t.objectProperty(
191
+ t.identifier('bubbled'),
192
+ t.stringLiteral(event.name),
180
193
  ),
181
- j.property('init', j.identifier('bubbled'), j.literal(event.name)),
182
194
  ]),
183
195
  ),
184
196
  ]),
185
197
  );
186
198
  }
187
199
  function generateDirectEventInfo(event, nameOveride) {
188
- return j.property(
189
- 'init',
190
- j.identifier(normalizeInputEventName(nameOveride || event.name)),
191
- j.objectExpression([
192
- j.property(
193
- 'init',
194
- j.identifier('registrationName'),
195
- j.literal(event.name),
200
+ return t.objectProperty(
201
+ t.identifier(normalizeInputEventName(nameOveride || event.name)),
202
+ t.objectExpression([
203
+ t.objectProperty(
204
+ t.identifier('registrationName'),
205
+ t.stringLiteral(event.name),
196
206
  ),
197
207
  ]),
198
208
  );
@@ -218,20 +228,15 @@ function buildViewConfig(schema, componentName, component, imports) {
218
228
  throw new Error('Invalid extended type');
219
229
  }
220
230
  });
221
- const validAttributes = j.objectExpression([
231
+ const validAttributes = t.objectExpression([
222
232
  ...componentProps.map(schemaProp => {
223
- return j.property(
224
- 'init',
225
- j.identifier(schemaProp.name),
233
+ return t.objectProperty(
234
+ t.identifier(schemaProp.name),
226
235
  getReactDiffProcessValue(schemaProp.typeAnnotation),
227
236
  );
228
237
  }),
229
238
  ...(componentEvents.length > 0
230
- ? [
231
- j.spreadProperty(
232
- getValidAttributesForEvents(componentEvents, imports),
233
- ),
234
- ]
239
+ ? [t.spreadElement(getValidAttributesForEvents(componentEvents, imports))]
235
240
  : []),
236
241
  ]);
237
242
  const bubblingEventNames = component.events
@@ -249,14 +254,6 @@ function buildViewConfig(schema, componentName, component, imports) {
249
254
  }
250
255
  return bubblingEvents;
251
256
  }, []);
252
- const bubblingEvents =
253
- bubblingEventNames.length > 0
254
- ? j.property(
255
- 'init',
256
- j.identifier('bubblingEventTypes'),
257
- j.objectExpression(bubblingEventNames),
258
- )
259
- : null;
260
257
  const directEventNames = component.events
261
258
  .filter(event => event.bubblingType === 'direct')
262
259
  .reduce((directEvents, event) => {
@@ -272,25 +269,32 @@ function buildViewConfig(schema, componentName, component, imports) {
272
269
  }
273
270
  return directEvents;
274
271
  }, []);
275
- const directEvents =
276
- directEventNames.length > 0
277
- ? j.property(
278
- 'init',
279
- j.identifier('directEventTypes'),
280
- j.objectExpression(directEventNames),
281
- )
282
- : null;
283
272
  const properties = [
284
- j.property(
285
- 'init',
286
- j.identifier('uiViewClassName'),
287
- j.literal(componentName),
273
+ t.objectProperty(
274
+ t.identifier('uiViewClassName'),
275
+ t.stringLiteral(componentName),
288
276
  ),
289
- bubblingEvents,
290
- directEvents,
291
- j.property('init', j.identifier('validAttributes'), validAttributes),
292
- ].filter(Boolean);
293
- return j.objectExpression(properties);
277
+ ];
278
+ if (bubblingEventNames.length > 0) {
279
+ properties.push(
280
+ t.objectProperty(
281
+ t.identifier('bubblingEventTypes'),
282
+ t.objectExpression(bubblingEventNames),
283
+ ),
284
+ );
285
+ }
286
+ if (directEventNames.length > 0) {
287
+ properties.push(
288
+ t.objectProperty(
289
+ t.identifier('directEventTypes'),
290
+ t.objectExpression(directEventNames),
291
+ ),
292
+ );
293
+ }
294
+ properties.push(
295
+ t.objectProperty(t.identifier('validAttributes'), validAttributes),
296
+ );
297
+ return t.objectExpression(properties);
294
298
  }
295
299
  function buildCommands(schema, componentName, component, imports) {
296
300
  const commands = component.commands;
@@ -300,39 +304,29 @@ function buildCommands(schema, componentName, component, imports) {
300
304
  imports.add(
301
305
  'const {dispatchCommand} = require("react-native/Libraries/ReactNative/RendererProxy");',
302
306
  );
303
- const properties = commands.map(command => {
304
- const commandName = command.name;
305
- const params = command.typeAnnotation.params;
306
- const commandNameLiteral = j.literal(commandName);
307
- const commandNameIdentifier = j.identifier(commandName);
308
- const arrayParams = j.arrayExpression(
309
- params.map(param => {
310
- return j.identifier(param.name);
311
- }),
312
- );
313
- const expression = j.template
314
- .expression`dispatchCommand(ref, ${commandNameLiteral}, ${arrayParams})`;
315
- const functionParams = params.map(param => {
316
- return j.identifier(param.name);
317
- });
318
- const property = j.property(
319
- 'init',
320
- commandNameIdentifier,
321
- j.functionExpression(
322
- null,
323
- [j.identifier('ref'), ...functionParams],
324
- j.blockStatement([j.expressionStatement(expression)]),
325
- ),
326
- );
327
- property.method = true;
328
- return property;
329
- });
330
- return j.exportNamedDeclaration(
331
- j.variableDeclaration('const', [
332
- j.variableDeclarator(
333
- j.identifier('Commands'),
334
- j.objectExpression(properties),
335
- ),
307
+ const commandsObject = t.objectExpression(
308
+ commands.map(command => {
309
+ const commandName = command.name;
310
+ const params = command.typeAnnotation.params;
311
+ const dispatchCommandCall = t.callExpression(
312
+ t.identifier('dispatchCommand'),
313
+ [
314
+ t.identifier('ref'),
315
+ t.stringLiteral(commandName),
316
+ t.arrayExpression(params.map(param => t.identifier(param.name))),
317
+ ],
318
+ );
319
+ return t.objectMethod(
320
+ 'method',
321
+ t.identifier(commandName),
322
+ [t.identifier('ref'), ...params.map(param => t.identifier(param.name))],
323
+ t.blockStatement([t.expressionStatement(dispatchCommandCall)]),
324
+ );
325
+ }),
326
+ );
327
+ return t.exportNamedDeclaration(
328
+ t.variableDeclaration('const', [
329
+ t.variableDeclarator(t.identifier('Commands'), commandsObject),
336
330
  ]),
337
331
  );
338
332
  }
@@ -361,40 +355,40 @@ module.exports = {
361
355
  paperComponentNameDeprecated:
362
356
  component.paperComponentNameDeprecated,
363
357
  });
364
- const replacedSourceRoot = j.withParser('flow')(replacedTemplate);
365
358
  const paperComponentName =
366
359
  (_component$paperCompo = component.paperComponentName) !==
367
360
  null && _component$paperCompo !== void 0
368
361
  ? _component$paperCompo
369
362
  : componentName;
370
- replacedSourceRoot
371
- .find(j.Identifier, {
372
- name: 'VIEW_CONFIG',
373
- })
374
- .replaceWith(
375
- buildViewConfig(
376
- schema,
377
- paperComponentName,
378
- component,
379
- imports,
380
- ),
381
- );
382
- const commands = buildCommands(
363
+ const replacedSourceRoot = core.template.program(
364
+ replacedTemplate,
365
+ )({
366
+ VIEW_CONFIG: buildViewConfig(
367
+ schema,
368
+ paperComponentName,
369
+ component,
370
+ imports,
371
+ ),
372
+ });
373
+ const commandsExport = buildCommands(
383
374
  schema,
384
375
  paperComponentName,
385
376
  component,
386
377
  imports,
387
378
  );
388
- if (commands) {
389
- replacedSourceRoot
390
- .find(j.ExportDefaultDeclaration)
391
- .insertAfter(j(commands).toSource());
379
+ if (commandsExport) {
380
+ replacedSourceRoot.body.push(commandsExport);
392
381
  }
393
- const replacedSource = replacedSourceRoot.toSource({
394
- quote: 'single',
395
- trailingComma: true,
396
- });
397
- return replacedSource;
382
+ const replacedSource = core.transformFromAstSync(
383
+ replacedSourceRoot,
384
+ undefined,
385
+ {
386
+ babelrc: false,
387
+ browserslistConfigFile: false,
388
+ configFile: false,
389
+ },
390
+ );
391
+ return replacedSource.code;
398
392
  })
399
393
  .join('\n\n');
400
394
  })
@@ -16,7 +16,8 @@ import type {
16
16
  } from '../../CodegenSchema';
17
17
  import type {SchemaType} from '../../CodegenSchema';
18
18
 
19
- const j = require('jscodeshift');
19
+ const core = require('@babel/core');
20
+ const t = core.types;
20
21
 
21
22
  // File path -> contents
22
23
  type FilesOutput = Map<string, string>;
@@ -50,6 +51,10 @@ ${componentConfig}
50
51
  // this multiple times.
51
52
  const UIMANAGER_IMPORT = 'const {UIManager} = require("react-native")';
52
53
 
54
+ function expression(input: string) {
55
+ return core.template.expression(input)();
56
+ }
57
+
53
58
  function getReactDiffProcessValue(typeAnnotation: PropTypeAnnotation) {
54
59
  switch (typeAnnotation.type) {
55
60
  case 'BooleanTypeAnnotation':
@@ -61,25 +66,29 @@ function getReactDiffProcessValue(typeAnnotation: PropTypeAnnotation) {
61
66
  case 'StringEnumTypeAnnotation':
62
67
  case 'Int32EnumTypeAnnotation':
63
68
  case 'MixedTypeAnnotation':
64
- return j.literal(true);
69
+ return t.booleanLiteral(true);
65
70
  case 'ReservedPropTypeAnnotation':
66
71
  switch (typeAnnotation.name) {
67
72
  case 'ColorPrimitive':
68
- return j.template
69
- .expression`{ process: require('react-native/Libraries/StyleSheet/processColor').default }`;
73
+ return expression(
74
+ `{ process: require('react-native/Libraries/StyleSheet/processColor').default }`,
75
+ );
70
76
  case 'ImageSourcePrimitive':
71
- return j.template
72
- .expression`{ process: require('react-native/Libraries/Image/resolveAssetSource') }`;
77
+ return expression(
78
+ `{ process: require('react-native/Libraries/Image/resolveAssetSource') }`,
79
+ );
73
80
  case 'ImageRequestPrimitive':
74
81
  throw new Error('ImageRequest should not be used in props');
75
82
  case 'PointPrimitive':
76
- return j.template
77
- .expression`{ diff: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/pointsDiffer')) }`;
83
+ return expression(
84
+ `{ diff: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/pointsDiffer')) }`,
85
+ );
78
86
  case 'EdgeInsetsPrimitive':
79
- return j.template
80
- .expression`{ diff: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/insetsDiffer')) }`;
87
+ return expression(
88
+ `{ diff: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/insetsDiffer')) }`,
89
+ );
81
90
  case 'DimensionPrimitive':
82
- return j.literal(true);
91
+ return t.booleanLiteral(true);
83
92
  default:
84
93
  (typeAnnotation.name: empty);
85
94
  throw new Error(
@@ -90,20 +99,21 @@ function getReactDiffProcessValue(typeAnnotation: PropTypeAnnotation) {
90
99
  if (typeAnnotation.elementType.type === 'ReservedPropTypeAnnotation') {
91
100
  switch (typeAnnotation.elementType.name) {
92
101
  case 'ColorPrimitive':
93
- return j.template
94
- .expression`{ process: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/StyleSheet/processColorArray')) }`;
102
+ return expression(
103
+ `{ process: ((req) => 'default' in req ? req.default : req)(require('react-native/Libraries/StyleSheet/processColorArray')) }`,
104
+ );
95
105
  case 'ImageSourcePrimitive':
96
106
  case 'PointPrimitive':
97
107
  case 'EdgeInsetsPrimitive':
98
108
  case 'DimensionPrimitive':
99
- return j.literal(true);
109
+ return t.booleanLiteral(true);
100
110
  default:
101
111
  throw new Error(
102
112
  `Received unknown array native typeAnnotation: "${typeAnnotation.elementType.name}"`,
103
113
  );
104
114
  }
105
115
  }
106
- return j.literal(true);
116
+ return t.booleanLiteral(true);
107
117
  default:
108
118
  (typeAnnotation: empty);
109
119
  throw new Error(
@@ -134,7 +144,7 @@ ${
134
144
  : ''
135
145
  }
136
146
 
137
- export const __INTERNAL_VIEW_CONFIG = VIEW_CONFIG;
147
+ export const __INTERNAL_VIEW_CONFIG = %%VIEW_CONFIG%%;
138
148
 
139
149
  export default NativeComponentRegistry.get(nativeComponentName, () => __INTERNAL_VIEW_CONFIG);
140
150
  `.trim();
@@ -180,13 +190,16 @@ function getValidAttributesForEvents(
180
190
  "const {ConditionallyIgnoredEventHandlers} = require('react-native/Libraries/NativeComponent/ViewConfigIgnore');",
181
191
  );
182
192
 
183
- const validAttributes = j.objectExpression(
193
+ const validAttributes = t.objectExpression(
184
194
  events.map(eventType => {
185
- return j.property('init', j.identifier(eventType.name), j.literal(true));
195
+ return t.objectProperty(
196
+ t.identifier(eventType.name),
197
+ t.booleanLiteral(true),
198
+ );
186
199
  }),
187
200
  );
188
201
 
189
- return j.callExpression(j.identifier('ConditionallyIgnoredEventHandlers'), [
202
+ return t.callExpression(t.identifier('ConditionallyIgnoredEventHandlers'), [
190
203
  validAttributes,
191
204
  ]);
192
205
  }
@@ -195,20 +208,20 @@ function generateBubblingEventInfo(
195
208
  event: EventTypeShape,
196
209
  nameOveride: void | string,
197
210
  ) {
198
- return j.property(
199
- 'init',
200
- j.identifier(normalizeInputEventName(nameOveride || event.name)),
201
- j.objectExpression([
202
- j.property(
203
- 'init',
204
- j.identifier('phasedRegistrationNames'),
205
- j.objectExpression([
206
- j.property(
207
- 'init',
208
- j.identifier('captured'),
209
- j.literal(`${event.name}Capture`),
211
+ return t.objectProperty(
212
+ t.identifier(normalizeInputEventName(nameOveride || event.name)),
213
+ t.objectExpression([
214
+ t.objectProperty(
215
+ t.identifier('phasedRegistrationNames'),
216
+ t.objectExpression([
217
+ t.objectProperty(
218
+ t.identifier('captured'),
219
+ t.stringLiteral(`${event.name}Capture`),
220
+ ),
221
+ t.objectProperty(
222
+ t.identifier('bubbled'),
223
+ t.stringLiteral(event.name),
210
224
  ),
211
- j.property('init', j.identifier('bubbled'), j.literal(event.name)),
212
225
  ]),
213
226
  ),
214
227
  ]),
@@ -219,14 +232,12 @@ function generateDirectEventInfo(
219
232
  event: EventTypeShape,
220
233
  nameOveride: void | string,
221
234
  ) {
222
- return j.property(
223
- 'init',
224
- j.identifier(normalizeInputEventName(nameOveride || event.name)),
225
- j.objectExpression([
226
- j.property(
227
- 'init',
228
- j.identifier('registrationName'),
229
- j.literal(event.name),
235
+ return t.objectProperty(
236
+ t.identifier(normalizeInputEventName(nameOveride || event.name)),
237
+ t.objectExpression([
238
+ t.objectProperty(
239
+ t.identifier('registrationName'),
240
+ t.stringLiteral(event.name),
230
241
  ),
231
242
  ]),
232
243
  );
@@ -261,20 +272,15 @@ function buildViewConfig(
261
272
  }
262
273
  });
263
274
 
264
- const validAttributes = j.objectExpression([
275
+ const validAttributes = t.objectExpression([
265
276
  ...componentProps.map(schemaProp => {
266
- return j.property(
267
- 'init',
268
- j.identifier(schemaProp.name),
277
+ return t.objectProperty(
278
+ t.identifier(schemaProp.name),
269
279
  getReactDiffProcessValue(schemaProp.typeAnnotation),
270
280
  );
271
281
  }),
272
282
  ...(componentEvents.length > 0
273
- ? [
274
- j.spreadProperty(
275
- getValidAttributesForEvents(componentEvents, imports),
276
- ),
277
- ]
283
+ ? [t.spreadElement(getValidAttributesForEvents(componentEvents, imports))]
278
284
  : []),
279
285
  ]);
280
286
 
@@ -294,15 +300,6 @@ function buildViewConfig(
294
300
  return bubblingEvents;
295
301
  }, []);
296
302
 
297
- const bubblingEvents =
298
- bubblingEventNames.length > 0
299
- ? j.property(
300
- 'init',
301
- j.identifier('bubblingEventTypes'),
302
- j.objectExpression(bubblingEventNames),
303
- )
304
- : null;
305
-
306
303
  const directEventNames = component.events
307
304
  .filter(event => event.bubblingType === 'direct')
308
305
  .reduce((directEvents: Array<any>, event) => {
@@ -319,27 +316,34 @@ function buildViewConfig(
319
316
  return directEvents;
320
317
  }, []);
321
318
 
322
- const directEvents =
323
- directEventNames.length > 0
324
- ? j.property(
325
- 'init',
326
- j.identifier('directEventTypes'),
327
- j.objectExpression(directEventNames),
328
- )
329
- : null;
330
-
331
319
  const properties = [
332
- j.property(
333
- 'init',
334
- j.identifier('uiViewClassName'),
335
- j.literal(componentName),
320
+ t.objectProperty(
321
+ t.identifier('uiViewClassName'),
322
+ t.stringLiteral(componentName),
336
323
  ),
337
- bubblingEvents,
338
- directEvents,
339
- j.property('init', j.identifier('validAttributes'), validAttributes),
340
- ].filter(Boolean);
324
+ ];
325
+
326
+ if (bubblingEventNames.length > 0) {
327
+ properties.push(
328
+ t.objectProperty(
329
+ t.identifier('bubblingEventTypes'),
330
+ t.objectExpression(bubblingEventNames),
331
+ ),
332
+ );
333
+ }
334
+ if (directEventNames.length > 0) {
335
+ properties.push(
336
+ t.objectProperty(
337
+ t.identifier('directEventTypes'),
338
+ t.objectExpression(directEventNames),
339
+ ),
340
+ );
341
+ }
341
342
 
342
- return j.objectExpression(properties);
343
+ properties.push(
344
+ t.objectProperty(t.identifier('validAttributes'), validAttributes),
345
+ );
346
+ return t.objectExpression(properties);
343
347
  }
344
348
 
345
349
  function buildCommands(
@@ -358,45 +362,32 @@ function buildCommands(
358
362
  'const {dispatchCommand} = require("react-native/Libraries/ReactNative/RendererProxy");',
359
363
  );
360
364
 
361
- const properties = commands.map(command => {
362
- const commandName = command.name;
363
- const params = command.typeAnnotation.params;
364
-
365
- const commandNameLiteral = j.literal(commandName);
366
- const commandNameIdentifier = j.identifier(commandName);
367
- const arrayParams = j.arrayExpression(
368
- params.map(param => {
369
- return j.identifier(param.name);
370
- }),
371
- );
372
-
373
- const expression = j.template
374
- .expression`dispatchCommand(ref, ${commandNameLiteral}, ${arrayParams})`;
375
-
376
- const functionParams = params.map(param => {
377
- return j.identifier(param.name);
378
- });
379
-
380
- const property = j.property(
381
- 'init',
382
- commandNameIdentifier,
383
- j.functionExpression(
384
- null,
385
- [j.identifier('ref'), ...functionParams],
386
- j.blockStatement([j.expressionStatement(expression)]),
387
- ),
388
- );
389
- property.method = true;
365
+ const commandsObject = t.objectExpression(
366
+ commands.map(command => {
367
+ const commandName = command.name;
368
+ const params = command.typeAnnotation.params;
369
+
370
+ const dispatchCommandCall = t.callExpression(
371
+ t.identifier('dispatchCommand'),
372
+ [
373
+ t.identifier('ref'),
374
+ t.stringLiteral(commandName),
375
+ t.arrayExpression(params.map(param => t.identifier(param.name))),
376
+ ],
377
+ );
390
378
 
391
- return property;
392
- });
379
+ return t.objectMethod(
380
+ 'method',
381
+ t.identifier(commandName),
382
+ [t.identifier('ref'), ...params.map(param => t.identifier(param.name))],
383
+ t.blockStatement([t.expressionStatement(dispatchCommandCall)]),
384
+ );
385
+ }),
386
+ );
393
387
 
394
- return j.exportNamedDeclaration(
395
- j.variableDeclaration('const', [
396
- j.variableDeclarator(
397
- j.identifier('Commands'),
398
- j.objectExpression(properties),
399
- ),
388
+ return t.exportNamedDeclaration(
389
+ t.variableDeclaration('const', [
390
+ t.variableDeclarator(t.identifier('Commands'), commandsObject),
400
391
  ]),
401
392
  );
402
393
  }
@@ -431,42 +422,40 @@ module.exports = {
431
422
  component.paperComponentNameDeprecated,
432
423
  });
433
424
 
434
- const replacedSourceRoot = j.withParser('flow')(replacedTemplate);
435
-
436
425
  const paperComponentName =
437
426
  component.paperComponentName ?? componentName;
438
427
 
439
- replacedSourceRoot
440
- .find(j.Identifier, {
441
- name: 'VIEW_CONFIG',
442
- })
443
- .replaceWith(
444
- buildViewConfig(
445
- schema,
446
- paperComponentName,
447
- component,
448
- imports,
449
- ),
450
- );
451
-
452
- const commands = buildCommands(
428
+ const replacedSourceRoot = core.template.program(
429
+ replacedTemplate,
430
+ )({
431
+ VIEW_CONFIG: buildViewConfig(
432
+ schema,
433
+ paperComponentName,
434
+ component,
435
+ imports,
436
+ ),
437
+ });
438
+
439
+ const commandsExport = buildCommands(
453
440
  schema,
454
441
  paperComponentName,
455
442
  component,
456
443
  imports,
457
444
  );
458
- if (commands) {
459
- replacedSourceRoot
460
- .find(j.ExportDefaultDeclaration)
461
- .insertAfter(j(commands).toSource());
445
+ if (commandsExport) {
446
+ replacedSourceRoot.body.push(commandsExport);
462
447
  }
463
448
 
464
- const replacedSource: string = replacedSourceRoot.toSource({
465
- quote: 'single',
466
- trailingComma: true,
467
- });
468
-
469
- return replacedSource;
449
+ const replacedSource = core.transformFromAstSync(
450
+ replacedSourceRoot,
451
+ undefined,
452
+ {
453
+ babelrc: false,
454
+ browserslistConfigFile: false,
455
+ configFile: false,
456
+ },
457
+ );
458
+ return replacedSource.code;
470
459
  })
471
460
  .join('\n\n');
472
461
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native/codegen",
3
- "version": "0.79.0-rc.0",
3
+ "version": "0.79.0-rc.2",
4
4
  "description": "Code generation tools for React Native",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -29,24 +29,22 @@
29
29
  "lib"
30
30
  ],
31
31
  "dependencies": {
32
- "@babel/parser": "^7.25.3",
33
32
  "glob": "^7.1.1",
34
33
  "hermes-parser": "0.25.1",
35
34
  "invariant": "^2.2.4",
36
- "jscodeshift": "^17.0.0",
37
35
  "nullthrows": "^1.1.1",
38
36
  "yargs": "^17.6.2"
39
37
  },
40
38
  "devDependencies": {
41
39
  "@babel/core": "^7.25.2",
42
- "@babel/plugin-transform-class-properties": "^7.25.4",
43
- "@babel/plugin-transform-nullish-coalescing-operator": "^7.24.7",
44
- "@babel/plugin-transform-object-rest-spread": "^7.24.7",
45
- "@babel/plugin-transform-optional-chaining": "^7.24.8",
46
40
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",
47
41
  "@babel/plugin-transform-async-to-generator": "^7.24.7",
42
+ "@babel/plugin-transform-class-properties": "^7.25.4",
48
43
  "@babel/plugin-transform-destructuring": "^7.24.8",
49
44
  "@babel/plugin-transform-flow-strip-types": "^7.25.2",
45
+ "@babel/plugin-transform-nullish-coalescing-operator": "^7.24.7",
46
+ "@babel/plugin-transform-object-rest-spread": "^7.24.7",
47
+ "@babel/plugin-transform-optional-chaining": "^7.24.8",
50
48
  "@babel/preset-env": "^7.25.3",
51
49
  "chalk": "^4.0.0",
52
50
  "hermes-estree": "0.25.1",
@@ -55,6 +53,6 @@
55
53
  "rimraf": "^3.0.2"
56
54
  },
57
55
  "peerDependencies": {
58
- "@babel/preset-env": "^7.1.6"
56
+ "@babel/core": "*"
59
57
  }
60
58
  }