@@ -49,9 +49,9 @@ export function generate(
49
49
sourceLang : string ,
50
50
templateAst : CompilerDOM . RootNode ,
51
51
isVue2 : boolean ,
52
+ allowTypeNarrowingInEventExpressions : boolean ,
52
53
cssScopedClasses : string [ ] = [ ] ,
53
54
htmlToTemplate : ( htmlStart : number , htmlEnd : number ) => { start : number , end : number ; } | undefined ,
54
- isScriptSetup : boolean ,
55
55
searchTexts : {
56
56
getEmitCompletion ( tag : string ) : string ,
57
57
getPropsCompletion ( tag : string ) : string ,
@@ -91,6 +91,7 @@ export function generate(
91
91
const localVars : Record < string , number > = { } ;
92
92
const identifiers = new Set < string > ( ) ;
93
93
const scopedClasses : { className : string , offset : number ; } [ ] = [ ] ;
94
+ const blockConditions : string [ ] = [ ] ;
94
95
95
96
tsFormatCodeGen . addText ( 'export { };\n' ) ;
96
97
@@ -393,6 +394,9 @@ export function generate(
393
394
}
394
395
else if ( node . type === CompilerDOM . NodeTypes . IF ) {
395
396
// v-if / v-else-if / v-else
397
+
398
+ let originalBlockConditionsLength = blockConditions . length ;
399
+
396
400
for ( let i = 0 ; i < node . branches . length ; i ++ ) {
397
401
398
402
const branch = node . branches [ i ] ;
@@ -404,6 +408,8 @@ export function generate(
404
408
else
405
409
tsCodeGen . addText ( 'else' ) ;
406
410
411
+ let addedBlockCondition = false ;
412
+
407
413
if ( branch . condition ?. type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION ) {
408
414
tsCodeGen . addText ( ` ` ) ;
409
415
writeInterpolation (
@@ -421,13 +427,24 @@ export function generate(
421
427
branch . condition . loc . start . offset ,
422
428
formatBrackets . round ,
423
429
) ;
430
+
431
+ if ( allowTypeNarrowingInEventExpressions ) {
432
+ blockConditions . push ( branch . condition . content ) ;
433
+ addedBlockCondition = true ;
434
+ }
424
435
}
425
436
tsCodeGen . addText ( ` {\n` ) ;
426
437
for ( const childNode of branch . children ) {
427
438
visitNode ( childNode , parentEl ) ;
428
439
}
429
440
tsCodeGen . addText ( '}\n' ) ;
441
+
442
+ if ( addedBlockCondition ) {
443
+ blockConditions [ blockConditions . length - 1 ] = `!(${ blockConditions [ blockConditions . length - 1 ] } )` ;
444
+ }
430
445
}
446
+
447
+ blockConditions . length = originalBlockConditionsLength ;
431
448
}
432
449
else if ( node . type === CompilerDOM . NodeTypes . FOR ) {
433
450
// v-for
@@ -747,8 +764,17 @@ export function generate(
747
764
const _exp = prop . exp ;
748
765
const expIndex = jsChildNode . children . findIndex ( child => typeof child === 'object' && child . type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION && child . content === _exp . content ) ;
749
766
const expNode = jsChildNode . children [ expIndex ] as CompilerDOM . SimpleExpressionNode ;
750
- const prefix = jsChildNode . children . filter ( ( child , i ) => typeof child === 'string' && i < expIndex ) . map ( child => child as string ) . join ( '' ) ;
751
- const suffix = jsChildNode . children . filter ( ( child , i ) => typeof child === 'string' && i > expIndex ) . map ( child => child as string ) . join ( '' ) ;
767
+ let prefix = jsChildNode . children . filter ( ( child , i ) => typeof child === 'string' && i < expIndex ) . map ( child => child as string ) . join ( '' ) ;
768
+ let suffix = jsChildNode . children . filter ( ( child , i ) => typeof child === 'string' && i > expIndex ) . map ( child => child as string ) . join ( '' ) ;
769
+
770
+ if ( prefix && blockConditions . length ) {
771
+ prefix = prefix . replace ( '(' , '{ ' ) ;
772
+ suffix = suffix . replace ( ')' , '} ' ) ;
773
+ prefix += '\n' ;
774
+ for ( const blockCondition of blockConditions ) {
775
+ prefix += `if (!(${ blockCondition } )) return;\n` ;
776
+ }
777
+ }
752
778
753
779
writeInterpolation (
754
780
expNode . content ,
0 commit comments