@@ -58,6 +58,10 @@ export interface CodegenContext extends Required<CodegenOptions> {
58
58
loc ?: SourceLocation ,
59
59
name ?: string ,
60
60
) : void
61
+ pushMulti (
62
+ codes : [ left : string , right : string , segment ?: string ] ,
63
+ ...fn : Array < false | ( ( ) => void ) >
64
+ ) : void
61
65
indent ( ) : void
62
66
deindent ( ) : void
63
67
newline ( ) : void
@@ -170,6 +174,15 @@ function createCodegenContext(
170
174
context . newline ( )
171
175
context . push ( code , newlineIndex , node )
172
176
} ,
177
+ pushMulti ( [ left , right , seg ] , ...fns ) {
178
+ fns = fns . filter ( Boolean )
179
+ context . push ( left )
180
+ for ( let i = 0 ; i < fns . length ; i ++ ) {
181
+ ; ( fns [ i ] as ( ) => void ) ( )
182
+ if ( seg && i < fns . length - 1 ) context . push ( seg )
183
+ }
184
+ context . push ( right )
185
+ } ,
173
186
indent ( ) {
174
187
++ context . indentLevel
175
188
} ,
@@ -435,56 +448,58 @@ function genAppendNode(oper: AppendNodeIRNode, context: CodegenContext) {
435
448
}
436
449
437
450
function genSetEvent ( oper : SetEventIRNode , context : CodegenContext ) {
438
- const { vaporHelper, push, pushWithNewline } = context
439
-
440
- pushWithNewline ( `${ vaporHelper ( 'on' ) } (n${ oper . element } , ` )
441
-
442
- // 2nd arg: event name
443
- if ( oper . keyOverride ) {
444
- const find = JSON . stringify ( oper . keyOverride [ 0 ] )
445
- const replacement = JSON . stringify ( oper . keyOverride [ 1 ] )
446
- push ( '(' )
447
- genExpression ( oper . key , context )
448
- push ( `) === ${ find } ? ${ replacement } : (` )
449
- genExpression ( oper . key , context )
450
- push ( ')' )
451
- } else {
452
- genExpression ( oper . key , context )
453
- }
454
- push ( ', ' )
455
-
451
+ const { vaporHelper, push, pushWithNewline, pushMulti : pushMulti } = context
456
452
const { keys, nonKeys, options } = oper . modifiers
457
453
458
- // 3rd arg: event handler
459
- if ( oper . value && oper . value . content . trim ( ) ) {
460
- if ( keys . length ) {
461
- push ( `${ vaporHelper ( 'withKeys' ) } (` )
462
- }
463
- if ( nonKeys . length ) {
464
- push ( `${ vaporHelper ( 'withModifiers' ) } (` )
465
- }
466
- push ( '(...args) => (' )
467
- genExpression ( oper . value , context )
468
- push ( ' && ' )
469
- genExpression ( oper . value , context )
470
- push ( '(...args))' )
471
-
472
- if ( nonKeys . length ) {
473
- push ( `, ${ genArrayExpression ( nonKeys ) } )` )
474
- }
475
- if ( keys . length ) {
476
- push ( `, ${ genArrayExpression ( keys ) } )` )
477
- }
478
- } else {
479
- push ( '() => {}' )
480
- }
481
-
482
- // 4th arg, gen options
483
- if ( options . length ) {
484
- push ( `, { ${ options . map ( ( v ) => `${ v } : true` ) . join ( ', ' ) } }` )
485
- }
486
-
487
- push ( ')' )
454
+ pushWithNewline ( vaporHelper ( 'on' ) )
455
+ pushMulti (
456
+ [ '(' , ')' , ', ' ] ,
457
+ // 1st arg: event name
458
+ ( ) => push ( `n${ oper . element } ` ) ,
459
+ // 2nd arg: event name
460
+ ( ) => {
461
+ if ( oper . keyOverride ) {
462
+ const find = JSON . stringify ( oper . keyOverride [ 0 ] )
463
+ const replacement = JSON . stringify ( oper . keyOverride [ 1 ] )
464
+ pushMulti ( [ '(' , ')' ] , ( ) => genExpression ( oper . key , context ) )
465
+ push ( ` === ${ find } ? ${ replacement } : ` )
466
+ pushMulti ( [ '(' , ')' ] , ( ) => genExpression ( oper . key , context ) )
467
+ } else {
468
+ genExpression ( oper . key , context )
469
+ }
470
+ } ,
471
+ // 3rd arg: event handler
472
+ ( ) => {
473
+ if ( oper . value && oper . value . content . trim ( ) ) {
474
+ const pushWithKeys = ( fn : ( ) => void ) => {
475
+ push ( `${ vaporHelper ( 'withKeys' ) } (` )
476
+ fn ( )
477
+ push ( `, ${ genArrayExpression ( keys ) } )` )
478
+ }
479
+ const pushWithModifiers = ( fn : ( ) => void ) => {
480
+ push ( `${ vaporHelper ( 'withModifiers' ) } (` )
481
+ fn ( )
482
+ push ( `, ${ genArrayExpression ( nonKeys ) } )` )
483
+ }
484
+ const pushNoop = ( fn : ( ) => void ) => fn ( )
485
+
486
+ ; ( keys . length ? pushWithKeys : pushNoop ) ( ( ) =>
487
+ ( nonKeys . length ? pushWithModifiers : pushNoop ) ( ( ) => {
488
+ push ( '(...args) => (' )
489
+ genExpression ( oper . value ! , context )
490
+ push ( ' && ' )
491
+ genExpression ( oper . value ! , context )
492
+ push ( '(...args))' )
493
+ } ) ,
494
+ )
495
+ } else {
496
+ push ( '() => {}' )
497
+ }
498
+ } ,
499
+ // 4th arg, gen options
500
+ ! ! options . length &&
501
+ ( ( ) => push ( `{ ${ options . map ( ( v ) => `${ v } : true` ) . join ( ', ' ) } }` ) ) ,
502
+ )
488
503
}
489
504
490
505
function genWithDirective ( oper : WithDirectiveIRNode , context : CodegenContext ) {
0 commit comments