@@ -91,7 +91,7 @@ export function generate(
91
91
let expectedErrorStart : undefined | number ;
92
92
let expectedErrorNode : CompilerDOM . CommentNode | undefined ;
93
93
94
- const componentVars = generateComponentVars ( ) ;
94
+ generatePreResolveComponents ( ) ;
95
95
96
96
if ( sfc . templateAst ) {
97
97
visitNode ( sfc . templateAst , undefined , undefined , undefined ) ;
@@ -176,11 +176,22 @@ export function generate(
176
176
codes . push ( '}\n' ) ;
177
177
}
178
178
179
- function generateComponentVars ( ) {
179
+ function toCanonicalComponentName ( tagText : string ) {
180
+ return validTsVar . test ( tagText ) ? tagText : capitalize ( camelize ( tagText . replace ( / : / g, '-' ) ) ) ;
181
+ }
182
+
183
+ function getPossibleOriginalComponentName ( tagText : string ) {
184
+ return [ ...new Set ( [
185
+ // order is important: https://github.com/vuejs/language-tools/issues/2010
186
+ capitalize ( camelize ( tagText ) ) ,
187
+ camelize ( tagText ) ,
188
+ tagText ,
189
+ ] ) ] ;
190
+ }
180
191
181
- const data : Record < string , string > = { } ;
192
+ function generatePreResolveComponents ( ) {
182
193
183
- codes . push ( `let __VLS_templateComponents !: {}\n` ) ;
194
+ codes . push ( `let __VLS_resolvedLocalAndGlobalComponents !: {}\n` ) ;
184
195
185
196
for ( const tagName in tagNames ) {
186
197
@@ -191,18 +202,14 @@ export function generate(
191
202
if ( isNamespacedTag )
192
203
continue ;
193
204
194
- const validName = validTsVar . test ( tagName ) ? tagName : capitalize ( camelize ( tagName . replace ( / : / g, '-' ) ) ) ;
195
-
196
205
codes . push (
197
- `& __VLS_WithComponent<'${ validName } ', typeof __VLS_localComponents, ` ,
206
+ `& __VLS_WithComponent<'${ toCanonicalComponentName ( tagName ) } ', typeof __VLS_localComponents, ` ,
198
207
// order is important: https://github.com/vuejs/language-tools/issues/2010
199
208
`"${ capitalize ( camelize ( tagName ) ) } ", ` ,
200
209
`"${ camelize ( tagName ) } ", ` ,
201
210
`"${ tagName } "` ,
202
211
'>\n' ,
203
212
) ;
204
-
205
- data [ tagName ] = validName ;
206
213
}
207
214
208
215
codes . push ( `;\n` ) ;
@@ -211,12 +218,7 @@ export function generate(
211
218
212
219
const tagOffsets = tagNames [ tagName ] ;
213
220
const tagRanges : [ number , number ] [ ] = tagOffsets . map ( offset => [ offset , offset + tagName . length ] ) ;
214
- const names = new Set ( nativeTags . has ( tagName ) ? [ tagName ] : [
215
- // order is important: https://github.com/vuejs/language-tools/issues/2010
216
- capitalize ( camelize ( tagName ) ) ,
217
- camelize ( tagName ) ,
218
- tagName ,
219
- ] ) ;
221
+ const names = nativeTags . has ( tagName ) ? [ tagName ] : getPossibleOriginalComponentName ( tagName ) ;
220
222
221
223
for ( const name of names ) {
222
224
for ( const tagRange of tagRanges ) {
@@ -240,32 +242,34 @@ export function generate(
240
242
}
241
243
codes . push ( '\n' ) ;
242
244
243
- const validName = data [ tagName ] ;
245
+ if ( nativeTags . has ( tagName ) )
246
+ continue ;
244
247
245
- if ( validName ) {
246
- codes . push (
247
- '// @ts-ignore\n' , // #2304
248
- '[' ,
249
- ) ;
250
- for ( const tagRange of tagRanges ) {
251
- codes . push ( [
252
- validName ,
253
- 'template' ,
254
- tagRange ,
255
- {
256
- completion : {
257
- additional : true ,
258
- autoImportOnly : true ,
259
- } ,
248
+ const isNamespacedTag = tagName . indexOf ( '.' ) >= 0 ;
249
+ if ( isNamespacedTag )
250
+ continue ;
251
+
252
+ codes . push (
253
+ '// @ts-ignore\n' , // #2304
254
+ '[' ,
255
+ ) ;
256
+ const validName = toCanonicalComponentName ( tagName ) ;
257
+ for ( const tagRange of tagRanges ) {
258
+ codes . push ( [
259
+ validName ,
260
+ 'template' ,
261
+ tagRange ,
262
+ {
263
+ completion : {
264
+ additional : true ,
265
+ autoImportOnly : true ,
260
266
} ,
261
- ] ) ;
262
- codes . push ( ',' ) ;
263
- }
264
- codes . push ( `];\n` ) ;
267
+ } ,
268
+ ] ) ;
269
+ codes . push ( ',' ) ;
265
270
}
271
+ codes . push ( `];\n` ) ;
266
272
}
267
-
268
- return data ;
269
273
}
270
274
271
275
function collectTagOffsets ( ) {
@@ -669,11 +673,12 @@ export function generate(
669
673
';\n' ,
670
674
) ;
671
675
}
672
- else if ( componentVars [ tag ] ) {
673
- codes . push ( `const ${ var_originalComponent } = __VLS_templateComponents['${ componentVars [ tag ] } '];\n` ) ;
674
- }
675
676
else {
676
- codes . push ( `const ${ var_originalComponent } = {} as any;\n` ) ;
677
+ codes . push ( `let ${ var_originalComponent } !: ` ) ;
678
+ for ( const componentName of getPossibleOriginalComponentName ( tag ) ) {
679
+ codes . push ( `'${ componentName } ' extends keyof typeof __VLS_ctx ? typeof __VLS_ctx${ validTsVar . test ( componentName ) ? `.${ componentName } ` : `['${ componentName } ']` } : ` ) ;
680
+ }
681
+ codes . push ( `typeof __VLS_resolvedLocalAndGlobalComponents['${ toCanonicalComponentName ( tag ) } '];\n` ) ;
677
682
}
678
683
679
684
codes . push (
@@ -693,22 +698,30 @@ export function generate(
693
698
codes . push ( ');\n' ) ;
694
699
695
700
for ( const offset of tagOffsets ) {
696
- if ( isNamespacedTag ) {
701
+ if ( isNamespacedTag || dynamicTagExp ) {
697
702
continue ;
698
703
}
699
- else if ( dynamicTagExp ) {
700
- continue ;
704
+ else if ( isIntrinsicElement ) {
705
+ codes . push ( `({} as __VLS_IntrinsicElements).` ) ;
706
+ codes . push (
707
+ [
708
+ tag ,
709
+ 'template' ,
710
+ [ offset , offset + tag . length ] ,
711
+ {
712
+ ...capabilitiesPresets . tagHover ,
713
+ ...capabilitiesPresets . diagnosticOnly ,
714
+ } ,
715
+ ] ,
716
+ ';\n' ,
717
+ ) ;
701
718
}
702
719
else {
703
- if ( isIntrinsicElement ) {
704
- codes . push ( `({} as __VLS_IntrinsicElements).` ) ;
705
- }
706
- else {
707
- codes . push ( `__VLS_templateComponents.` ) ;
708
- }
720
+ const key = toCanonicalComponentName ( tag ) ;
721
+ codes . push ( `({} as { ${ key } : typeof ${ var_originalComponent } }).` ) ;
709
722
codes . push (
710
723
[
711
- componentVars [ tag ] ?? tag ,
724
+ key ,
712
725
'template' ,
713
726
[ offset , offset + tag . length ] ,
714
727
{
0 commit comments