@@ -23,8 +23,7 @@ export function* generateComponent(
23
23
options : TemplateCodegenOptions ,
24
24
ctx : TemplateCodegenContext ,
25
25
node : CompilerDOM . ElementNode ,
26
- currentComponent : CompilerDOM . ElementNode | undefined ,
27
- componentCtxVar : string | undefined
26
+ currentComponent : CompilerDOM . ElementNode | undefined
28
27
) : Generator < Code > {
29
28
const startTagOffset = node . loc . start . offset + options . template . content . substring ( node . loc . start . offset ) . indexOf ( node . tag ) ;
30
29
const endTagOffset = ! node . isSelfClosing && options . template . lang === 'html' ? node . loc . start . offset + node . loc . source . lastIndexOf ( node . tag ) : undefined ;
@@ -137,40 +136,26 @@ export function* generateComponent(
137
136
yield `)${ endOfLine } ` ;
138
137
}
139
138
else if ( ! isComponentTag ) {
140
- yield `// @ts-ignore${ newLine } ` ;
141
- yield `const ${ var_originalComponent } = ({} as ` ;
142
- for ( const componentName of possibleOriginalNames ) {
143
- yield `'${ componentName } ' extends keyof typeof __VLS_ctx ? { '${ getCanonicalComponentName ( node . tag ) } ': typeof __VLS_ctx` ;
144
- yield * generatePropertyAccess ( options , ctx , componentName ) ;
145
- yield ` }: ` ;
146
- }
147
- yield `typeof __VLS_resolvedLocalAndGlobalComponents)${ newLine } ` ;
148
- yield * generatePropertyAccess (
149
- options ,
150
- ctx ,
151
- getCanonicalComponentName ( node . tag ) ,
139
+ yield `const ${ var_originalComponent } = __VLS_resolvedLocalAndGlobalComponents.` ;
140
+ yield * generateCanonicalComponentName (
141
+ node . tag ,
152
142
startTagOffset ,
153
- ctx . codeFeatures . verification
143
+ {
144
+ // with hover support
145
+ ...ctx . codeFeatures . withoutHighlightAndCompletionAndNavigation ,
146
+ ...ctx . codeFeatures . verification ,
147
+ }
154
148
) ;
155
- yield endOfLine ;
149
+ yield ` ${ endOfLine } ` ;
156
150
157
- // hover support
158
- for ( const offset of tagOffsets ) {
159
- yield `({} as { ${ getCanonicalComponentName ( node . tag ) } : typeof ${ var_originalComponent } }).` ;
160
- yield * generateCanonicalComponentName (
161
- node . tag ,
162
- offset ,
163
- ctx . codeFeatures . withoutHighlightAndCompletionAndNavigation
164
- ) ;
165
- yield endOfLine ;
166
- }
167
151
const camelizedTag = camelize ( node . tag ) ;
168
152
if ( variableNameRegex . test ( camelizedTag ) ) {
169
153
// renaming / find references support
154
+ yield `/** @type { [` ;
170
155
for ( const tagOffset of tagOffsets ) {
171
156
for ( const shouldCapitalize of ( node . tag [ 0 ] === node . tag [ 0 ] . toUpperCase ( ) ? [ false ] : [ true , false ] ) ) {
172
157
const expectName = shouldCapitalize ? capitalize ( camelizedTag ) : camelizedTag ;
173
- yield `__VLS_components.` ;
158
+ yield `typeof __VLS_components.` ;
174
159
yield * generateCamelized (
175
160
shouldCapitalize ? capitalize ( node . tag ) : node . tag ,
176
161
tagOffset ,
@@ -181,27 +166,25 @@ export function* generateComponent(
181
166
} ,
182
167
}
183
168
) ;
184
- yield `; ` ;
169
+ yield `, ` ;
185
170
}
186
171
}
187
- yield `${ newLine } ` ;
172
+ yield `] } */ ${ newLine } ` ;
188
173
// auto import support
189
- yield `// @ts-ignore${ newLine } ` ; // #2304
190
- yield `[` ;
191
- for ( const tagOffset of tagOffsets ) {
174
+ if ( options . edited ) {
175
+ yield `// @ts-ignore${ newLine } ` ; // #2304
192
176
yield * generateCamelized (
193
177
capitalize ( node . tag ) ,
194
- tagOffset ,
178
+ startTagOffset ,
195
179
{
196
180
completion : {
197
181
isAdditional : true ,
198
182
onlyImport : true ,
199
183
} ,
200
184
}
201
185
) ;
202
- yield `, ` ;
186
+ yield `${ endOfLine } ` ;
203
187
}
204
- yield `]${ endOfLine } ` ;
205
188
}
206
189
}
207
190
else {
@@ -213,38 +196,17 @@ export function* generateComponent(
213
196
yield * generateElementProps ( options , ctx , node , props , false ) ;
214
197
yield `}))${ endOfLine } ` ;
215
198
216
- if ( options . vueCompilerOptions . strictTemplates ) {
217
- // with strictTemplates, generate once for props type-checking + instance type
218
- yield `const ${ var_componentInstance } = ${ var_functionalComponent } (` ;
219
- yield * wrapWith (
220
- startTagOffset ,
221
- startTagOffset + node . tag . length ,
222
- ctx . codeFeatures . verification ,
223
- `{` ,
224
- ...generateElementProps ( options , ctx , node , props , true , propsFailedExps ) ,
225
- `}`
226
- ) ;
227
- yield `, ...__VLS_functionalComponentArgsRest(${ var_functionalComponent } ))${ endOfLine } ` ;
228
- }
229
- else {
230
- // without strictTemplates, this only for instance type
231
- yield `const ${ var_componentInstance } = ${ var_functionalComponent } ({` ;
232
- yield * generateElementProps ( options , ctx , node , props , false ) ;
233
- yield `}, ...__VLS_functionalComponentArgsRest(${ var_functionalComponent } ))${ endOfLine } ` ;
234
- // and this for props type-checking
235
- yield `({} as (props: __VLS_FunctionalComponentProps<typeof ${ var_originalComponent } , typeof ${ var_componentInstance } > & Record<string, unknown>) => void)(` ;
236
- yield * wrapWith (
237
- startTagOffset ,
238
- startTagOffset + node . tag . length ,
239
- ctx . codeFeatures . verification ,
240
- `{` ,
241
- ...generateElementProps ( options , ctx , node , props , true , propsFailedExps ) ,
242
- `}`
243
- ) ;
244
- yield `)${ endOfLine } ` ;
245
- }
199
+ yield `const ${ var_componentInstance } = ${ var_functionalComponent } (` ;
200
+ yield * wrapWith (
201
+ startTagOffset ,
202
+ startTagOffset + node . tag . length ,
203
+ ctx . codeFeatures . verification ,
204
+ `{` ,
205
+ ...generateElementProps ( options , ctx , node , props , true , propsFailedExps ) ,
206
+ `}`
207
+ ) ;
208
+ yield `, ...__VLS_functionalComponentArgsRest(${ var_functionalComponent } ))${ endOfLine } ` ;
246
209
247
- componentCtxVar = var_defineComponentCtx ;
248
210
currentComponent = node ;
249
211
250
212
for ( const failedExp of propsFailedExps ) {
@@ -262,28 +224,14 @@ export function* generateComponent(
262
224
}
263
225
264
226
const refName = yield * generateVScope ( options , ctx , node , props ) ;
227
+ if ( refName ) {
228
+ ctx . usedComponentCtxVars . add ( var_defineComponentCtx ) ;
229
+ }
265
230
266
- ctx . usedComponentCtxVars . add ( componentCtxVar ) ;
267
231
const usedComponentEventsVar = yield * generateElementEvents ( options , ctx , node , var_functionalComponent , var_componentInstance , var_componentEmit , var_componentEvents ) ;
268
-
269
- if ( var_defineComponentCtx && ctx . usedComponentCtxVars . has ( var_defineComponentCtx ) ) {
270
- yield `const ${ componentCtxVar } = __VLS_nonNullable(__VLS_pickFunctionalComponentCtx(${ var_originalComponent } , ${ var_componentInstance } ))${ endOfLine } ` ;
271
- if ( refName ) {
272
- yield `// @ts-ignore${ newLine } ` ;
273
- if ( node . codegenNode ?. type === CompilerDOM . NodeTypes . VNODE_CALL
274
- && node . codegenNode . props ?. type === CompilerDOM . NodeTypes . JS_OBJECT_EXPRESSION
275
- && node . codegenNode . props . properties . find ( ( { key } ) => key . type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION && key . content === 'ref_for' )
276
- ) {
277
- yield `(${ refName } ??= []).push(${ var_defineComponentCtx } )` ;
278
- } else {
279
- yield `${ refName } = ${ var_defineComponentCtx } ` ;
280
- }
281
-
282
- yield endOfLine ;
283
- }
284
- }
285
232
if ( usedComponentEventsVar ) {
286
- yield `let ${ var_componentEmit } !: typeof ${ componentCtxVar } .emit${ endOfLine } ` ;
233
+ ctx . usedComponentCtxVars . add ( var_defineComponentCtx ) ;
234
+ yield `let ${ var_componentEmit } !: typeof ${ var_defineComponentCtx } .emit${ endOfLine } ` ;
287
235
yield `let ${ var_componentEvents } !: __VLS_NormalizeEmits<typeof ${ var_componentEmit } >${ endOfLine } ` ;
288
236
}
289
237
@@ -301,10 +249,27 @@ export function* generateComponent(
301
249
302
250
const slotDir = node . props . find ( p => p . type === CompilerDOM . NodeTypes . DIRECTIVE && p . name === 'slot' ) as CompilerDOM . DirectiveNode ;
303
251
if ( slotDir ) {
304
- yield * generateComponentSlot ( options , ctx , node , slotDir , currentComponent , componentCtxVar ) ;
252
+ yield * generateComponentSlot ( options , ctx , node , slotDir , currentComponent , var_defineComponentCtx ) ;
305
253
}
306
254
else {
307
- yield * generateElementChildren ( options , ctx , node , currentComponent , componentCtxVar ) ;
255
+ yield * generateElementChildren ( options , ctx , node , currentComponent , var_defineComponentCtx ) ;
256
+ }
257
+
258
+ if ( ctx . usedComponentCtxVars . has ( var_defineComponentCtx ) ) {
259
+ yield `const ${ var_defineComponentCtx } = __VLS_pickFunctionalComponentCtx(${ var_originalComponent } , ${ var_componentInstance } )${ endOfLine } ` ;
260
+ if ( refName ) {
261
+ yield `// @ts-ignore${ newLine } ` ;
262
+ if ( node . codegenNode ?. type === CompilerDOM . NodeTypes . VNODE_CALL
263
+ && node . codegenNode . props ?. type === CompilerDOM . NodeTypes . JS_OBJECT_EXPRESSION
264
+ && node . codegenNode . props . properties . find ( ( { key } ) => key . type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION && key . content === 'ref_for' )
265
+ ) {
266
+ yield `(${ refName } ??= []).push(${ var_defineComponentCtx } )` ;
267
+ } else {
268
+ yield `${ refName } = ${ var_defineComponentCtx } ` ;
269
+ }
270
+
271
+ yield endOfLine ;
272
+ }
308
273
}
309
274
}
310
275
0 commit comments