@@ -20,13 +20,17 @@ export function* generateElementEvents(
20
20
let emitVar : string | undefined ;
21
21
let eventsVar : string | undefined ;
22
22
let propsVar : string | undefined ;
23
+
23
24
for ( const prop of node . props ) {
24
25
if (
25
26
prop . type === CompilerDOM . NodeTypes . DIRECTIVE
26
- && prop . name === 'on'
27
- && prop . arg ?. type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION
28
- && ! prop . arg . loc . source . startsWith ( '[' )
29
- && ! prop . arg . loc . source . endsWith ( ']' )
27
+ && (
28
+ prop . name === 'model'
29
+ || prop . name === 'on'
30
+ && prop . arg ?. type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION
31
+ && ! prop . arg . loc . source . startsWith ( '[' )
32
+ && ! prop . arg . loc . source . endsWith ( ']' )
33
+ )
30
34
) {
31
35
ctx . currentComponent ! . used = true ;
32
36
if ( ! emitVar ) {
@@ -37,21 +41,33 @@ export function* generateElementEvents(
37
41
yield `let ${ eventsVar } !: __VLS_NormalizeEmits<typeof ${ emitVar } >${ endOfLine } ` ;
38
42
yield `let ${ propsVar } !: __VLS_FunctionalComponentProps<typeof ${ componentFunctionalVar } , typeof ${ componentVNodeVar } >${ endOfLine } ` ;
39
43
}
40
- let source = prop . arg . loc . source ;
41
- let start = prop . arg . loc . start . offset ;
42
- let propPrefix = 'on' ;
44
+
45
+ let source = prop . arg ?. loc . source ?? 'model-value' ;
46
+ let start = prop . arg ?. loc . start . offset ;
47
+ let propPrefix = 'on-' ;
43
48
let emitPrefix = '' ;
44
- if ( source . startsWith ( 'vue:' ) ) {
49
+ if ( prop . name === 'model' ) {
50
+ propPrefix = 'onUpdate:' ;
51
+ emitPrefix = 'update:' ;
52
+ }
53
+ else if ( source . startsWith ( 'vue:' ) ) {
45
54
source = source . slice ( 'vue:' . length ) ;
46
- start = start + 'vue:' . length ;
47
- propPrefix = 'onVnode' ;
55
+ start = start ! + 'vue:' . length ;
56
+ propPrefix = 'onVnode- ' ;
48
57
emitPrefix = 'vnode-' ;
49
58
}
50
- yield `const ${ ctx . getInternalVariable ( ) } : __VLS_NormalizeComponentEvent<typeof ${ propsVar } , typeof ${ eventsVar } , '${ camelize ( propPrefix + '-' + source ) } ', '${ emitPrefix } ${ source } ', '${ camelize ( emitPrefix + source ) } '> = {${ newLine } ` ;
51
- yield * generateEventArg ( ctx , source , start , propPrefix ) ;
52
- yield `: ` ;
53
- yield * generateEventExpression ( options , ctx , prop ) ;
54
- yield `}${ endOfLine } ` ;
59
+
60
+ yield `(): __VLS_NormalizeComponentEvent<typeof ${ propsVar } , typeof ${ eventsVar } , '${ camelize ( propPrefix + source ) } ', '${ emitPrefix + source } ', '${ camelize ( emitPrefix + source ) } '> => ({${ newLine } ` ;
61
+ if ( prop . name === 'on' ) {
62
+ yield * generateEventArg ( ctx , source , start ! , propPrefix . slice ( 0 , - 1 ) ) ;
63
+ yield `: ` ;
64
+ yield * generateEventExpression ( options , ctx , prop ) ;
65
+ }
66
+ else {
67
+ yield `'${ camelize ( propPrefix + source ) } ': ` ;
68
+ yield * generateModelEventExpression ( options , ctx , prop ) ;
69
+ }
70
+ yield `})${ endOfLine } ` ;
55
71
}
56
72
}
57
73
}
@@ -159,6 +175,29 @@ export function* generateEventExpression(
159
175
}
160
176
}
161
177
178
+ export function * generateModelEventExpression (
179
+ options : TemplateCodegenOptions ,
180
+ ctx : TemplateCodegenContext ,
181
+ prop : CompilerDOM . DirectiveNode
182
+ ) : Generator < Code > {
183
+ if ( prop . exp ?. type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION ) {
184
+ yield `(...[$event]) => (` ;
185
+ yield * generateInterpolation (
186
+ options ,
187
+ ctx ,
188
+ 'template' ,
189
+ ctx . codeFeatures . verification ,
190
+ prop . exp . content ,
191
+ prop . exp . loc . start . offset ,
192
+ prop . exp . loc
193
+ )
194
+ yield ` = $event)` ;
195
+ }
196
+ else {
197
+ yield `() => {}` ;
198
+ }
199
+ }
200
+
162
201
export function isCompoundExpression ( ts : typeof import ( 'typescript' ) , ast : ts . SourceFile ) {
163
202
let result = true ;
164
203
if ( ast . statements . length === 0 ) {
0 commit comments