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
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
} )
0 commit comments