11import * as CompilerDOM from '@vue/compiler-dom' ;
2+ import { replaceSourceRange } from 'muggle-string' ;
23import type * as ts from 'typescript' ;
34import type { Code } from '../../types' ;
45import { collectBindingNames } from '../../utils/collectBindings' ;
5- import { getStartEnd } from '../../utils/shared' ;
66import { codeFeatures } from '../codeFeatures' ;
77import { createTsAst , endOfLine , newLine } from '../utils' ;
88import { wrapWith } from '../utils/wrapWith' ;
99import type { TemplateCodegenContext } from './context' ;
1010import { generateElementChildren } from './elementChildren' ;
1111import type { TemplateCodegenOptions } from './index' ;
12+ import { generateInterpolation } from './interpolation' ;
1213import { generateObjectProperty } from './objectProperty' ;
1314
1415export function * generateVSlot (
@@ -67,7 +68,7 @@ export function* generateVSlot(
6768 if ( slotDir ?. exp ?. type === CompilerDOM . NodeTypes . SIMPLE_EXPRESSION ) {
6869 const slotAst = createTsAst ( options . ts , ctx . inlineTsAsts , `(${ slotDir . exp . content } ) => {}` ) ;
6970 slotBlockVars . push ( ...collectBindingNames ( options . ts , slotAst , slotAst ) ) ;
70- yield * generateSlotParameters ( options , slotAst , slotDir . exp , slotVar ) ;
71+ yield * generateSlotParameters ( options , ctx , slotAst , slotDir . exp , slotVar ) ;
7172 }
7273
7374 for ( const varName of slotBlockVars ) {
@@ -107,6 +108,7 @@ export function* generateVSlot(
107108
108109function * generateSlotParameters (
109110 options : TemplateCodegenOptions ,
111+ ctx : TemplateCodegenContext ,
110112 ast : ts . SourceFile ,
111113 exp : CompilerDOM . SimpleExpressionNode ,
112114 slotVar : string ,
@@ -120,31 +122,42 @@ function* generateSlotParameters(
120122
121123 const { expression } = statement ;
122124 const startOffset = exp . loc . start . offset - 1 ;
123- const modifies : [ Code [ ] , number , number ] [ ] = [ ] ;
124125 const types : ( Code | null ) [ ] = [ ] ;
125126
127+ const interpolation = [ ...generateInterpolation (
128+ options ,
129+ ctx ,
130+ 'template' ,
131+ codeFeatures . all ,
132+ ast . text ,
133+ startOffset ,
134+ ) ] ;
135+
136+ replaceSourceRange ( interpolation , 'template' , startOffset , startOffset + `(` . length ) ;
137+ replaceSourceRange (
138+ interpolation ,
139+ 'template' ,
140+ startOffset + ast . text . length - `) => {}` . length ,
141+ startOffset + ast . text . length ,
142+ ) ;
143+
126144 for ( const { name, type } of expression . parameters ) {
127145 if ( type ) {
128- modifies . push ( [
129- [ `` ] ,
130- name . end ,
131- type . end ,
146+ types . push ( [
147+ ast . text . slice ( name . end , type . end ) ,
148+ 'template' ,
149+ startOffset + name . end ,
150+ codeFeatures . all ,
132151 ] ) ;
133- types . push ( chunk ( getStartEnd ( ts , type , ast ) . start , type . end ) ) ;
152+ replaceSourceRange ( interpolation , 'template' , startOffset + name . end , startOffset + type . end ) ;
134153 }
135154 else {
136155 types . push ( null ) ;
137156 }
138157 }
139158
140159 yield `const [` ;
141- let nextStart = 1 ;
142- for ( const [ codes , start , end ] of modifies ) {
143- yield chunk ( nextStart , start ) ;
144- yield * codes ;
145- nextStart = end ;
146- }
147- yield chunk ( nextStart , expression . equalsGreaterThanToken . pos - 1 ) ;
160+ yield * interpolation ;
148161 yield `] = __VLS_getSlotParameters(${ slotVar } !` ;
149162
150163 if ( types . some ( t => t ) ) {
@@ -154,18 +167,9 @@ function* generateSlotParameters(
154167 exp . loc . end . offset ,
155168 codeFeatures . verification ,
156169 `(` ,
157- ...types . flatMap ( type => type ? [ `_: ` , type , `, ` ] : `_, ` ) ,
170+ ...types . flatMap ( type => type ? [ `_` , type , `, ` ] : `_, ` ) ,
158171 `) => [] as any` ,
159172 ) ;
160173 }
161174 yield `)${ endOfLine } ` ;
162-
163- function chunk ( start : number , end : number ) : Code {
164- return [
165- ast . text . slice ( start , end ) ,
166- 'template' ,
167- startOffset + start ,
168- codeFeatures . all ,
169- ] ;
170- }
171175}
0 commit comments