@@ -23,8 +23,7 @@ export function* generateComponent(
2323 options : TemplateCodegenOptions ,
2424 ctx : TemplateCodegenContext ,
2525 node : CompilerDOM . ElementNode ,
26- currentComponent : CompilerDOM . ElementNode | undefined ,
27- componentCtxVar : string | undefined
26+ currentComponent : CompilerDOM . ElementNode | undefined
2827) : Generator < Code > {
2928 const startTagOffset = node . loc . start . offset + options . template . content . substring ( node . loc . start . offset ) . indexOf ( node . tag ) ;
3029 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(
137136 yield `)${ endOfLine } ` ;
138137 }
139138 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 ,
152142 startTagOffset ,
153- ctx . codeFeatures . verification
143+ {
144+ // with hover support
145+ ...ctx . codeFeatures . withoutHighlightAndCompletionAndNavigation ,
146+ ...ctx . codeFeatures . verification ,
147+ }
154148 ) ;
155- yield endOfLine ;
149+ yield ` ${ endOfLine } ` ;
156150
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- }
167151 const camelizedTag = camelize ( node . tag ) ;
168152 if ( variableNameRegex . test ( camelizedTag ) ) {
169153 // renaming / find references support
154+ yield `/** @type { [` ;
170155 for ( const tagOffset of tagOffsets ) {
171156 for ( const shouldCapitalize of ( node . tag [ 0 ] === node . tag [ 0 ] . toUpperCase ( ) ? [ false ] : [ true , false ] ) ) {
172157 const expectName = shouldCapitalize ? capitalize ( camelizedTag ) : camelizedTag ;
173- yield `__VLS_components.` ;
158+ yield `typeof __VLS_components.` ;
174159 yield * generateCamelized (
175160 shouldCapitalize ? capitalize ( node . tag ) : node . tag ,
176161 tagOffset ,
@@ -181,27 +166,25 @@ export function* generateComponent(
181166 } ,
182167 }
183168 ) ;
184- yield `; ` ;
169+ yield `, ` ;
185170 }
186171 }
187- yield `${ newLine } ` ;
172+ yield `] } */ ${ newLine } ` ;
188173 // 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
192176 yield * generateCamelized (
193177 capitalize ( node . tag ) ,
194- tagOffset ,
178+ startTagOffset ,
195179 {
196180 completion : {
197181 isAdditional : true ,
198182 onlyImport : true ,
199183 } ,
200184 }
201185 ) ;
202- yield `, ` ;
186+ yield `${ endOfLine } ` ;
203187 }
204- yield `]${ endOfLine } ` ;
205188 }
206189 }
207190 else {
@@ -213,38 +196,17 @@ export function* generateComponent(
213196 yield * generateElementProps ( options , ctx , node , props , false ) ;
214197 yield `}))${ endOfLine } ` ;
215198
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 } ` ;
246209
247- componentCtxVar = var_defineComponentCtx ;
248210 currentComponent = node ;
249211
250212 for ( const failedExp of propsFailedExps ) {
@@ -262,28 +224,14 @@ export function* generateComponent(
262224 }
263225
264226 const refName = yield * generateVScope ( options , ctx , node , props ) ;
227+ if ( refName ) {
228+ ctx . usedComponentCtxVars . add ( var_defineComponentCtx ) ;
229+ }
265230
266- ctx . usedComponentCtxVars . add ( componentCtxVar ) ;
267231 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- }
285232 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 } ` ;
287235 yield `let ${ var_componentEvents } !: __VLS_NormalizeEmits<typeof ${ var_componentEmit } >${ endOfLine } ` ;
288236 }
289237
@@ -301,10 +249,27 @@ export function* generateComponent(
301249
302250 const slotDir = node . props . find ( p => p . type === CompilerDOM . NodeTypes . DIRECTIVE && p . name === 'slot' ) as CompilerDOM . DirectiveNode ;
303251 if ( slotDir ) {
304- yield * generateComponentSlot ( options , ctx , node , slotDir , currentComponent , componentCtxVar ) ;
252+ yield * generateComponentSlot ( options , ctx , node , slotDir , currentComponent , var_defineComponentCtx ) ;
305253 }
306254 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+ }
308273 }
309274}
310275
0 commit comments