@@ -182,10 +182,16 @@ function _generate(
182
182
const codeMaps = new Map < string , RawSourceMap > ( )
183
183
const { type, sourceMap, isGlobal, locale, jit } = options
184
184
185
- const codegenFn : CodeGenFunction = jit
185
+ const _codegenFn : CodeGenFunction = jit
186
186
? generateResourceAst
187
187
: generateMessageFunction
188
188
189
+ function codegenFn ( value : string ) {
190
+ const { code, map } = _codegenFn ( value , options , pathStack )
191
+ sourceMap && map != null && codeMaps . set ( value , map )
192
+ return code
193
+ }
194
+
189
195
const componentNamespace = '_Component'
190
196
const variableDeclarations : string [ ] = [ ]
191
197
@@ -250,15 +256,20 @@ function _generate(
250
256
}
251
257
}
252
258
259
+ if ( parent ?. type === 'ArrayExpression' ) {
260
+ const lastIndex = itemsCountStack . length - 1
261
+ const currentCount = parent . elements . length - itemsCountStack [ lastIndex ]
262
+ pathStack . push ( currentCount . toString ( ) )
263
+ itemsCountStack [ lastIndex ] = -- itemsCountStack [ lastIndex ]
264
+ }
265
+
253
266
switch ( node . type ) {
254
267
case 'Program' :
255
268
if ( type === 'plain' ) {
256
269
generator . push ( `const resource = ` )
257
270
} else if ( type === 'sfc' ) {
258
- // for 'sfc'
259
- const variableName =
260
- type === 'sfc' ? ( ! isGlobal ? '__i18n' : '__i18nGlobal' ) : ''
261
- const localeName = type === 'sfc' ? locale ?? `""` : ''
271
+ const localeName = JSON . stringify ( locale ?? '' )
272
+ const variableName = ! isGlobal ? '__i18n' : '__i18nGlobal'
262
273
generator . push ( `export default function (Component) {` )
263
274
generator . indent ( )
264
275
generator . pushline ( `const ${ componentNamespace } = Component` )
@@ -267,163 +278,108 @@ function _generate(
267
278
)
268
279
generator . push ( `${ componentNamespace } .${ variableName } .push({` )
269
280
generator . indent ( )
270
- generator . pushline ( `"locale": ${ JSON . stringify ( localeName ) } ,` )
281
+ generator . pushline ( `"locale": ${ localeName } ,` )
271
282
generator . push ( `"resource": ` )
272
283
}
273
284
break
274
285
case 'ObjectExpression' :
275
- generator . push ( `{` )
286
+ generator . push ( '{' )
276
287
generator . indent ( )
277
288
propsCountStack . push ( node . properties . length )
278
- if ( parent ?. type === 'ArrayExpression' ) {
279
- const lastIndex = itemsCountStack . length - 1
280
- const currentCount =
281
- parent . elements . length - itemsCountStack [ lastIndex ]
282
- pathStack . push ( currentCount . toString ( ) )
283
- itemsCountStack [ lastIndex ] = -- itemsCountStack [ lastIndex ]
284
- }
289
+ break
290
+ case 'ArrayExpression' :
291
+ generator . push ( '[' )
292
+ generator . indent ( )
293
+ itemsCountStack . push ( node . elements . length )
285
294
break
286
295
case 'Property' :
287
- if ( parent ?. type === 'ObjectExpression' ) {
296
+ if ( parent ?. type !== 'ObjectExpression' ) break
297
+ if ( node . key . type !== 'Literal' && node . key . type !== 'Identifier' )
298
+ break
299
+
300
+ // prettier-ignore
301
+ const name = node . key . type === 'Literal'
302
+ ? String ( node . key . value )
303
+ : node . key . name
304
+ const strName = JSON . stringify ( name )
305
+ if ( isJSONablePrimitiveLiteral ( node . value ) ) {
306
+ generator . push ( `${ strName } : ` )
307
+ pathStack . push ( name )
308
+ const value = getValue ( node . value ) as string
309
+ const strValue = JSON . stringify ( value )
288
310
if (
289
- isJSONablePrimitiveLiteral ( node . value ) &&
290
- ( node . key . type === 'Literal' || node . key . type === 'Identifier' )
291
- ) {
292
- // prettier-ignore
293
- const name = node . key . type === 'Literal'
294
- ? String ( node . key . value )
295
- : node . key . name
296
- if (
297
- ( node . value . type === 'Literal' && isString ( node . value . value ) ) ||
298
- node . value . type === 'TemplateLiteral'
299
- ) {
300
- const value = getValue ( node . value ) as string
301
- generator . push ( `${ JSON . stringify ( name ) } : ` )
302
- pathStack . push ( name )
303
- const { code, map } = codegenFn ( value , options , pathStack )
304
- sourceMap && map != null && codeMaps . set ( value , map )
305
- generator . push ( `${ code } ` , node . value , value )
306
- skipStack . push ( false )
307
- } else {
308
- const value = getValue ( node . value )
309
- if ( forceStringify ) {
310
- const strValue = JSON . stringify ( value )
311
- generator . push ( `${ JSON . stringify ( name ) } : ` )
312
- pathStack . push ( name )
313
- const { code, map } = codegenFn ( strValue , options , pathStack )
314
- sourceMap && map != null && codeMaps . set ( strValue , map )
315
- generator . push ( `${ code } ` , node . value , strValue )
316
- } else {
317
- generator . push (
318
- `${ JSON . stringify ( name ) } : ${ JSON . stringify ( value ) } `
319
- )
320
- pathStack . push ( name )
321
- }
322
- skipStack . push ( false )
323
- }
324
- } else if (
325
- ( node . value . type === 'FunctionExpression' ||
326
- node . value . type === 'ArrowFunctionExpression' ) &&
327
- ( node . key . type === 'Literal' || node . key . type === 'Identifier' )
311
+ ( node . value . type === 'Literal' && isString ( node . value . value ) ) ||
312
+ node . value . type === 'TemplateLiteral'
328
313
) {
329
- // prettier-ignore
330
- const name = node . key . type === 'Literal'
331
- ? String ( node . key . value )
332
- : node . key . name
333
- generator . push ( `${ JSON . stringify ( name ) } : ` )
334
- pathStack . push ( name )
335
- const code = generateJavaScript ( node . value , {
336
- format : { compact : true }
337
- } )
338
- generator . push ( `${ code } ` , node . value , code )
314
+ generator . push ( codegenFn ( value ) , node . value , value )
315
+ } else if ( forceStringify ) {
316
+ generator . push ( codegenFn ( strValue ) , node . value , strValue )
317
+ } else {
318
+ generator . push ( strValue )
319
+ }
320
+ skipStack . push ( false )
321
+ } else if (
322
+ node . value . type === 'ArrayExpression' ||
323
+ node . value . type === 'ObjectExpression'
324
+ ) {
325
+ generator . push ( `${ strName } : ` )
326
+ pathStack . push ( name )
327
+ skipStack . push ( false )
328
+ } else if (
329
+ node . value . type === 'FunctionExpression' ||
330
+ node . value . type === 'ArrowFunctionExpression'
331
+ ) {
332
+ generator . push ( `${ strName } : ` )
333
+ pathStack . push ( name )
334
+ const code = generateJavaScript ( node . value , {
335
+ format : { compact : true }
336
+ } )
337
+ generator . push ( code , node . value , code )
338
+ skipStack . push ( false )
339
+ } else {
340
+ const skipProperty = 'regex' in node . value
341
+ if ( ! skipProperty && node . type === 'Property' ) {
342
+ const identifierName =
343
+ ( node . value . type === 'Identifier' && String ( node . value . name ) ) ||
344
+ ( node . value . type === 'Literal' && String ( node . value . value ) )
345
+
346
+ generator . push ( `${ strName } : ${ identifierName || name } ` )
339
347
skipStack . push ( false )
340
- } else if (
341
- ( node . value . type === 'ObjectExpression' ||
342
- node . value . type === 'ArrayExpression' ) &&
343
- ( node . key . type === 'Literal' || node . key . type === 'Identifier' )
344
- ) {
345
- // prettier-ignore
346
- const name = node . key . type === 'Literal'
347
- ? String ( node . key . value )
348
- : node . key . name
349
- generator . push ( `${ JSON . stringify ( name ) } : ` )
350
- pathStack . push ( name )
351
348
} else {
352
- const skipProperty = 'regex' in node . value
353
- if ( ! skipProperty && node . type === 'Property' ) {
354
- const name =
355
- ( node . key . type === 'Identifier' && String ( node . key . name ) ) ||
356
- ( node . key . type === 'Literal' && String ( node . key . value ) )
357
- const name2 =
358
- ( node . value . type === 'Identifier' &&
359
- String ( node . value . name ) ) ||
360
- ( node . value . type === 'Literal' && String ( node . value . value ) )
361
-
362
- generator . push ( `${ JSON . stringify ( name ) } : ${ name2 || name } ` )
363
- skipStack . push ( false )
364
- } else {
365
- // for Regex, function, etc.
366
- skipStack . push ( true )
367
- }
349
+ // for Regex, function, etc.
350
+ skipStack . push ( true )
368
351
}
369
- const lastIndex = propsCountStack . length - 1
370
- propsCountStack [ lastIndex ] = -- propsCountStack [ lastIndex ]
371
352
}
372
- break
373
- case 'ArrayExpression' :
374
- generator . push ( `[` )
375
- generator . indent ( )
376
- if ( parent ?. type === 'ArrayExpression' ) {
377
- const lastIndex = itemsCountStack . length - 1
378
- const currentCount =
379
- parent . elements . length - itemsCountStack [ lastIndex ]
380
- pathStack . push ( currentCount . toString ( ) )
381
- itemsCountStack [ lastIndex ] = -- itemsCountStack [ lastIndex ]
382
- }
383
- itemsCountStack . push ( node . elements . length )
353
+ const lastIndex = propsCountStack . length - 1
354
+ propsCountStack [ lastIndex ] = -- propsCountStack [ lastIndex ]
384
355
break
385
356
case 'SpreadElement' :
386
- const name =
357
+ const spreadIdentifier =
387
358
( node . argument . type === 'Identifier' &&
388
359
String ( node . argument . name ) ) ||
389
360
( node . argument . type === 'Literal' && String ( node . argument . value ) )
390
- generator . push ( `...${ name } ` )
361
+ generator . push ( `...${ spreadIdentifier } ` )
391
362
break
392
363
default :
393
364
if ( parent ?. type === 'ArrayExpression' ) {
394
- const lastIndex = itemsCountStack . length - 1
395
- const currentCount =
396
- parent . elements . length - itemsCountStack [ lastIndex ]
397
-
398
- pathStack . push ( currentCount . toString ( ) )
399
-
400
365
if ( isJSONablePrimitiveLiteral ( node ) ) {
366
+ const value = getValue ( node ) as string
367
+ const strValue = JSON . stringify ( value )
401
368
if (
402
369
( node . type === 'Literal' && isString ( node . value ) ) ||
403
370
node . type === 'TemplateLiteral'
404
371
) {
405
- const value = getValue ( node ) as string
406
- const { code, map } = codegenFn ( value , options , pathStack )
407
- sourceMap && map != null && codeMaps . set ( value , map )
408
- generator . push ( `${ code } ` , node , value )
372
+ generator . push ( codegenFn ( value ) , node , value )
373
+ } else if ( forceStringify ) {
374
+ generator . push ( codegenFn ( strValue ) , node , strValue )
409
375
} else {
410
- const value = getValue ( node )
411
- if ( forceStringify ) {
412
- const strValue = JSON . stringify ( value )
413
- const { code, map } = codegenFn ( strValue , options , pathStack )
414
- sourceMap && map != null && codeMaps . set ( strValue , map )
415
- generator . push ( `${ code } ` , node , strValue )
416
- } else {
417
- generator . push ( `${ JSON . stringify ( value ) } ` )
418
- }
376
+ generator . push ( strValue )
419
377
}
420
-
421
378
skipStack . push ( false )
422
379
} else {
423
380
// for Regex, function, etc.
424
381
skipStack . push ( true )
425
382
}
426
- itemsCountStack [ lastIndex ] = -- itemsCountStack [ lastIndex ]
427
383
}
428
384
break
429
385
}
@@ -438,14 +394,14 @@ function _generate(
438
394
leave ( node : Node , parent : Node ) {
439
395
switch ( node . type ) {
440
396
case 'Program' :
441
- if ( type === 'sfc' ) {
397
+ if ( type === 'plain' ) {
398
+ generator . push ( '\n' )
399
+ generator . push ( 'export default resource' )
400
+ } else if ( type === 'sfc' ) {
442
401
generator . deindent ( )
443
- generator . push ( `})` )
402
+ generator . push ( '})' )
444
403
generator . deindent ( )
445
- generator . pushline ( `}` )
446
- } else if ( type === 'plain' ) {
447
- generator . push ( `\n` )
448
- generator . push ( 'export default resource' )
404
+ generator . pushline ( '}' )
449
405
}
450
406
break
451
407
case 'ObjectExpression' :
@@ -454,56 +410,31 @@ function _generate(
454
410
propsCountStack . pop ( )
455
411
}
456
412
generator . deindent ( )
457
- generator . push ( `}` )
458
- if ( parent ?. type === 'ArrayExpression' ) {
459
- if ( itemsCountStack [ itemsCountStack . length - 1 ] !== 0 ) {
460
- pathStack . pop ( )
461
- if ( ! skipStack . pop ( ) ) {
462
- generator . pushline ( `,` )
463
- }
464
- }
465
- }
466
- break
467
- case 'Property' :
468
- if ( parent ?. type === 'ObjectExpression' ) {
469
- if ( propsCountStack [ propsCountStack . length - 1 ] !== 0 ) {
470
- pathStack . pop ( )
471
- if ( ! skipStack . pop ( ) ) {
472
- generator . pushline ( `,` )
473
- }
474
- }
475
- }
413
+ generator . push ( '}' )
476
414
break
477
415
case 'ArrayExpression' :
478
416
if ( itemsCountStack [ itemsCountStack . length - 1 ] === 0 ) {
479
417
pathStack . pop ( )
480
418
itemsCountStack . pop ( )
481
419
}
482
420
generator . deindent ( )
483
- generator . push ( `]` )
484
- if ( parent ?. type === 'ArrayExpression' ) {
485
- if ( itemsCountStack [ itemsCountStack . length - 1 ] !== 0 ) {
486
- pathStack . pop ( )
487
- if ( ! skipStack . pop ( ) ) {
488
- generator . pushline ( `,` )
489
- }
490
- }
491
- }
492
- break
493
- case 'Literal' :
494
- if ( parent ?. type === 'ArrayExpression' ) {
495
- if ( itemsCountStack [ itemsCountStack . length - 1 ] !== 0 ) {
496
- pathStack . pop ( )
497
- }
498
-
499
- if ( ! skipStack . pop ( ) ) {
500
- generator . pushline ( `,` )
501
- }
502
- }
421
+ generator . push ( ']' )
503
422
break
504
423
default :
505
424
break
506
425
}
426
+
427
+ if (
428
+ parent ?. type === 'ArrayExpression' ||
429
+ parent ?. type === 'ObjectExpression'
430
+ ) {
431
+ const stackArr =
432
+ node . type === 'Property' ? propsCountStack : itemsCountStack
433
+ if ( stackArr [ stackArr . length - 1 ] !== 0 ) {
434
+ pathStack . pop ( )
435
+ ! skipStack . pop ( ) && generator . pushline ( ',' )
436
+ }
437
+ }
507
438
}
508
439
} )
509
440
0 commit comments