@@ -33,9 +33,10 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
3333 if ( options . propsAssignName ) {
3434 ctx . addLocalVariable ( options . propsAssignName ) ;
3535 }
36+ ctx . addLocalVariable ( '$el' ) ;
3637 ctx . addLocalVariable ( '$refs' ) ;
3738
38- yield * generatePreResolveComponents ( ) ;
39+ yield * generatePreResolveComponents ( options ) ;
3940
4041 if ( options . template . ast ) {
4142 yield * generateTemplateChild ( options , ctx , options . template . ast , undefined , undefined , undefined ) ;
@@ -45,96 +46,104 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
4546
4647 if ( ! options . hasDefineSlots ) {
4748 yield `var __VLS_slots!:` ;
48- yield * generateSlotsType ( ) ;
49+ yield * generateSlotsType ( options , ctx ) ;
4950 yield endOfLine ;
5051 }
5152
52- yield * generateInheritedAttrs ( ) ;
53-
5453 yield * ctx . generateAutoImportCompletion ( ) ;
55-
56- yield * generateRefs ( ) ;
54+ yield * generateInheritedAttrs ( ctx ) ;
55+ yield * generateRefs ( ctx ) ;
56+ yield * generateRootEl ( ctx ) ;
5757
5858 return ctx ;
59+ }
5960
60- function * generateRefs ( ) : Generator < Code > {
61- yield `const __VLS_refs = {${ newLine } ` ;
62- for ( const [ name , [ varName , offset ] ] of ctx . templateRefs ) {
63- yield * generateStringLiteralKey (
64- name ,
65- offset ,
66- ctx . codeFeatures . navigationAndCompletion
67- )
68- yield `: ${ varName } ,${ newLine } ` ;
69- }
70- yield `}${ endOfLine } ` ;
71- yield `var $refs!: typeof __VLS_refs${ endOfLine } ` ;
61+ function * generateSlotsType ( options : TemplateCodegenOptions , ctx : TemplateCodegenContext ) : Generator < Code > {
62+ for ( const { expVar, varName } of ctx . dynamicSlots ) {
63+ ctx . hasSlot = true ;
64+ yield `Partial<Record<NonNullable<typeof ${ expVar } >, (_: typeof ${ varName } ) => any>> &${ newLine } ` ;
7265 }
73-
74- function * generateSlotsType ( ) : Generator < Code > {
75- for ( const { expVar, varName } of ctx . dynamicSlots ) {
76- ctx . hasSlot = true ;
77- yield `Partial<Record<NonNullable<typeof ${ expVar } >, (_: typeof ${ varName } ) => any>> &${ newLine } ` ;
66+ yield `{${ newLine } ` ;
67+ for ( const slot of ctx . slots ) {
68+ ctx . hasSlot = true ;
69+ if ( slot . name && slot . loc !== undefined ) {
70+ yield * generateObjectProperty (
71+ options ,
72+ ctx ,
73+ slot . name ,
74+ slot . loc ,
75+ ctx . codeFeatures . withoutHighlightAndCompletion ,
76+ slot . nodeLoc
77+ ) ;
7878 }
79- yield `{${ newLine } ` ;
80- for ( const slot of ctx . slots ) {
81- ctx . hasSlot = true ;
82- if ( slot . name && slot . loc !== undefined ) {
83- yield * generateObjectProperty (
84- options ,
85- ctx ,
86- slot . name ,
87- slot . loc ,
88- ctx . codeFeatures . withoutHighlightAndCompletion ,
89- slot . nodeLoc
90- ) ;
91- }
92- else {
93- yield * wrapWith (
94- slot . tagRange [ 0 ] ,
95- slot . tagRange [ 1 ] ,
96- ctx . codeFeatures . withoutHighlightAndCompletion ,
97- `default`
98- ) ;
99- }
100- yield `?(_: typeof ${ slot . varName } ): any,${ newLine } ` ;
79+ else {
80+ yield * wrapWith (
81+ slot . tagRange [ 0 ] ,
82+ slot . tagRange [ 1 ] ,
83+ ctx . codeFeatures . withoutHighlightAndCompletion ,
84+ `default`
85+ ) ;
10186 }
102- yield `}` ;
87+ yield `?(_: typeof ${ slot . varName } ): any, ${ newLine } ` ;
10388 }
89+ yield `}` ;
90+ }
10491
105- function * generateInheritedAttrs ( ) : Generator < Code > {
106- yield 'var __VLS_inheritedAttrs!: {}' ;
107- for ( const varName of ctx . inheritedAttrVars ) {
108- yield ` & typeof ${ varName } ` ;
109- }
110- yield endOfLine ;
92+ function * generateInheritedAttrs ( ctx : TemplateCodegenContext ) : Generator < Code > {
93+ yield 'var __VLS_inheritedAttrs!: {}' ;
94+ for ( const varName of ctx . inheritedAttrVars ) {
95+ yield ` & typeof ${ varName } ` ;
96+ }
97+ yield endOfLine ;
98+ }
99+
100+ function * generateRefs ( ctx : TemplateCodegenContext ) : Generator < Code > {
101+ yield `const __VLS_refs = {${ newLine } ` ;
102+ for ( const [ name , [ varName , offset ] ] of ctx . templateRefs ) {
103+ yield * generateStringLiteralKey (
104+ name ,
105+ offset ,
106+ ctx . codeFeatures . navigationAndCompletion
107+ ) ;
108+ yield `: ${ varName } ,${ newLine } ` ;
109+ }
110+ yield `}${ endOfLine } ` ;
111+ yield `var $refs!: typeof __VLS_refs${ endOfLine } ` ;
112+ }
113+
114+ function * generateRootEl ( ctx : TemplateCodegenContext ) : Generator < Code > {
115+ if ( ctx . singleRootElType ) {
116+ yield `var $el!: ${ ctx . singleRootElType } ${ endOfLine } ` ;
117+ }
118+ else {
119+ yield `var $el!: any${ endOfLine } ` ;
111120 }
121+ }
112122
113- function * generatePreResolveComponents ( ) : Generator < Code > {
114- yield `let __VLS_resolvedLocalAndGlobalComponents!: Required<{}` ;
115- if ( options . template . ast ) {
116- const components = new Set < string > ( ) ;
117- for ( const node of forEachElementNode ( options . template . ast ) ) {
118- if (
119- node . tagType === CompilerDOM . ElementTypes . COMPONENT
120- && node . tag . toLowerCase ( ) !== 'component'
121- && ! node . tag . includes ( '.' ) // namespace tag
122- ) {
123- if ( components . has ( node . tag ) ) {
124- continue ;
125- }
126- components . add ( node . tag ) ;
127- yield newLine ;
128- yield ` & __VLS_WithComponent<'${ getCanonicalComponentName ( node . tag ) } ', typeof __VLS_localComponents, ` ;
129- yield getPossibleOriginalComponentNames ( node . tag , false )
130- . map ( name => `"${ name } "` )
131- . join ( ', ' ) ;
132- yield `>` ;
123+ function * generatePreResolveComponents ( options : TemplateCodegenOptions ) : Generator < Code > {
124+ yield `let __VLS_resolvedLocalAndGlobalComponents!: Required<{}` ;
125+ if ( options . template . ast ) {
126+ const components = new Set < string > ( ) ;
127+ for ( const node of forEachElementNode ( options . template . ast ) ) {
128+ if (
129+ node . tagType === CompilerDOM . ElementTypes . COMPONENT
130+ && node . tag . toLowerCase ( ) !== 'component'
131+ && ! node . tag . includes ( '.' ) // namespace tag
132+ ) {
133+ if ( components . has ( node . tag ) ) {
134+ continue ;
133135 }
136+ components . add ( node . tag ) ;
137+ yield newLine ;
138+ yield ` & __VLS_WithComponent<'${ getCanonicalComponentName ( node . tag ) } ', typeof __VLS_localComponents, ` ;
139+ yield getPossibleOriginalComponentNames ( node . tag , false )
140+ . map ( name => `"${ name } "` )
141+ . join ( ', ' ) ;
142+ yield `>` ;
134143 }
135144 }
136- yield `>${ endOfLine } ` ;
137145 }
146+ yield `>${ endOfLine } ` ;
138147}
139148
140149export function * forEachElementNode ( node : CompilerDOM . RootNode | CompilerDOM . TemplateChildNode ) : Generator < CompilerDOM . ElementNode > {
0 commit comments