@@ -8,7 +8,6 @@ import { generateCamelized } from './camelized';
8
8
import type { TemplateCodegenContext } from './context' ;
9
9
import type { TemplateCodegenOptions } from './index' ;
10
10
import { generateInterpolation } from './interpolation' ;
11
- import { generateObjectProperty } from './objectProperty' ;
12
11
13
12
export function * generateElementEvents (
14
13
options : TemplateCodegenOptions ,
@@ -18,51 +17,40 @@ export function* generateElementEvents(
18
17
componentInstanceVar : string ,
19
18
emitVar : string ,
20
19
eventsVar : string ,
21
- used : ( ) => void ,
22
20
) : Generator < Code > {
21
+ let usedComponentEventsVar = false ;
22
+ let propsVar : string | undefined ;
23
23
for ( const prop of node . props ) {
24
24
if (
25
25
prop . type === CompilerDOM . NodeTypes . DIRECTIVE
26
26
&& prop . name === 'on'
27
27
&& prop . arg ?. type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION
28
+ && ! prop . arg . loc . source . startsWith ( '[' )
29
+ && ! prop . arg . loc . source . endsWith ( ']' )
28
30
) {
29
- used ( ) ;
30
- const eventVar = ctx . getInternalVariable ( ) ;
31
- yield `let ${ eventVar } = {${ newLine } ` ;
32
- yield `/**__VLS_emit,${ emitVar } ,${ prop . arg . loc . source } */${ newLine } ` ;
33
- yield `'${ prop . arg . loc . source } ': __VLS_pickEvent(` ;
34
- yield `${ eventsVar } ['${ prop . arg . loc . source } '], ` ;
35
- yield `({} as __VLS_FunctionalComponentProps<typeof ${ componentVar } , typeof ${ componentInstanceVar } >)` ;
36
- yield * generateEventArg ( options , ctx , prop . arg , true , false ) ;
37
- yield `) }${ endOfLine } ` ;
38
- yield `${ eventVar } = { ` ;
39
- if ( prop . arg . loc . source . startsWith ( '[' ) && prop . arg . loc . source . endsWith ( ']' ) ) {
40
- yield `[(` ;
41
- yield * generateInterpolation (
42
- options ,
43
- ctx ,
44
- prop . arg . loc . source . slice ( 1 , - 1 ) ,
45
- prop . arg . loc ,
46
- prop . arg . loc . start . offset + 1 ,
47
- ctx . codeFeatures . all ,
48
- '' ,
49
- '' ,
50
- ) ;
51
- yield `)!]` ;
31
+ usedComponentEventsVar = true ;
32
+ if ( ! propsVar ) {
33
+ propsVar = ctx . getInternalVariable ( ) ;
34
+ yield `let ${ propsVar } !: __VLS_FunctionalComponentProps<typeof ${ componentVar } , typeof ${ componentInstanceVar } >${ endOfLine } ` ;
52
35
}
53
- else {
54
- yield * generateObjectProperty (
55
- options ,
56
- ctx ,
57
- prop . arg . loc . source ,
58
- prop . arg . loc . start . offset ,
59
- ctx . codeFeatures . withoutHighlightAndCompletionAndNavigation ,
60
- prop . arg . loc
61
- ) ;
36
+ const originalPropName = camelize ( 'on-' + prop . arg . loc . source ) ;
37
+ yield `const ${ ctx . getInternalVariable ( ) } : ` ;
38
+ if ( ! options . vueCompilerOptions . strictTemplates ) {
39
+ yield `Record<string, unknown> & ` ;
62
40
}
41
+ yield `Partial<` ;
42
+ yield `__VLS_IsAny<__VLS_AsFunctionOrAny<typeof ${ propsVar } ['${ originalPropName } ']>> extends false${ newLine } ` ;
43
+ yield `? typeof ${ propsVar } ${ newLine } ` ;
44
+ yield `: __VLS_IsAny<typeof ${ eventsVar } ['${ prop . arg . loc . source } ']> extends false${ newLine } ` ;
45
+ yield `? {${ newLine } ` ;
46
+ yield `/**__VLS_emit,${ emitVar } ,${ prop . arg . loc . source } */${ newLine } ` ;
47
+ yield `'${ originalPropName } ': typeof ${ eventsVar } ['${ prop . arg . loc . source } ']${ newLine } ` ;
48
+ yield `}${ newLine } ` ;
49
+ yield `: typeof ${ propsVar } ${ newLine } ` ;
50
+ yield `> = {${ newLine } ` ;
51
+ yield * generateEventArg ( options , ctx , prop . arg , true ) ;
63
52
yield `: ` ;
64
53
yield * generateEventExpression ( options , ctx , prop ) ;
65
- yield newLine ;
66
54
yield `}${ endOfLine } ` ;
67
55
}
68
56
else if (
@@ -85,6 +73,7 @@ export function* generateElementEvents(
85
73
yield endOfLine ;
86
74
}
87
75
}
76
+ return usedComponentEventsVar ;
88
77
}
89
78
90
79
const eventArgFeatures : VueCodeInformation = {
@@ -108,7 +97,6 @@ export function* generateEventArg(
108
97
options : TemplateCodegenOptions ,
109
98
ctx : TemplateCodegenContext ,
110
99
arg : CompilerDOM . SimpleExpressionNode ,
111
- access : boolean ,
112
100
enableHover : boolean ,
113
101
) : Generator < Code > {
114
102
const features = enableHover
@@ -132,9 +120,6 @@ export function* generateEventArg(
132
120
yield `]` ;
133
121
}
134
122
else if ( variableNameRegex . test ( camelize ( arg . loc . source ) ) ) {
135
- if ( access ) {
136
- yield `.` ;
137
- }
138
123
yield [ '' , 'template' , arg . loc . start . offset , features ] ;
139
124
yield `on` ;
140
125
yield * generateCamelized (
@@ -144,9 +129,6 @@ export function* generateEventArg(
144
129
) ;
145
130
}
146
131
else {
147
- if ( access ) {
148
- yield `[` ;
149
- }
150
132
yield * wrapWith (
151
133
arg . loc . start . offset ,
152
134
arg . loc . end . offset ,
@@ -161,9 +143,6 @@ export function* generateEventArg(
161
143
) ,
162
144
`'` ,
163
145
) ;
164
- if ( access ) {
165
- yield `]` ;
166
- }
167
146
}
168
147
}
169
148
0 commit comments