@@ -64,7 +64,7 @@ export function* generateScriptSetup(
64
64
emitTypes . push ( `typeof ${ scriptSetupRanges . emits . name ?? '__VLS_emit' } ` ) ;
65
65
}
66
66
if ( scriptSetupRanges . defineProp . some ( p => p . isModel ) ) {
67
- emitTypes . push ( `__VLS_ModelEmitsType ` ) ;
67
+ emitTypes . push ( `typeof __VLS_modelEmit ` ) ;
68
68
}
69
69
70
70
yield ` return {} as {${ newLine } `
@@ -130,45 +130,15 @@ function* generateSetupFunction(
130
130
ctx . scriptSetupGeneratedOffset = options . getGeneratedLength ( ) - scriptSetupRanges . importSectionEndOffset ;
131
131
132
132
let setupCodeModifies : [ Code [ ] , number , number ] [ ] = [ ] ;
133
- const propsRange = scriptSetupRanges . props . withDefaults ?? scriptSetupRanges . props . define ;
134
- if ( propsRange && scriptSetupRanges . props . define ) {
135
- const statement = scriptSetupRanges . props . define . statement ;
136
- if ( scriptSetupRanges . props . define . typeArg ) {
137
- setupCodeModifies . push ( [ [
138
- `let __VLS_typeProps!: ` ,
139
- generateSfcBlockSection ( scriptSetup , scriptSetupRanges . props . define . typeArg . start , scriptSetupRanges . props . define . typeArg . end , codeFeatures . all ) ,
140
- endOfLine ,
141
- ] , statement . start , statement . start ] ) ;
142
- setupCodeModifies . push ( [ [ `typeof __VLS_typeProps` ] , scriptSetupRanges . props . define . typeArg . start , scriptSetupRanges . props . define . typeArg . end ] ) ;
143
- }
144
- if ( ! scriptSetupRanges . props . name ) {
145
- if ( statement . start === propsRange . start && statement . end === propsRange . end ) {
146
- setupCodeModifies . push ( [ [ `const __VLS_props = ` ] , propsRange . start , propsRange . start ] ) ;
147
- }
148
- else {
149
- if ( scriptSetupRanges . props . define . typeArg ) {
150
- setupCodeModifies . push ( [ [
151
- `const __VLS_props = ` ,
152
- generateSfcBlockSection ( scriptSetup , propsRange . start , scriptSetupRanges . props . define . typeArg . start , codeFeatures . all ) ,
153
- ] , statement . start , scriptSetupRanges . props . define . typeArg . start ] ) ;
154
- setupCodeModifies . push ( [ [
155
- generateSfcBlockSection ( scriptSetup , scriptSetupRanges . props . define . typeArg . end , propsRange . end , codeFeatures . all ) ,
156
- `${ endOfLine } ` ,
157
- generateSfcBlockSection ( scriptSetup , statement . start , propsRange . start , codeFeatures . all ) ,
158
- `__VLS_props` ,
159
- ] , scriptSetupRanges . props . define . typeArg . end , propsRange . end ] ) ;
160
- }
161
- else {
162
- setupCodeModifies . push ( [ [
163
- `const __VLS_props = ` ,
164
- generateSfcBlockSection ( scriptSetup , propsRange . start , propsRange . end , codeFeatures . all ) ,
165
- `${ endOfLine } ` ,
166
- generateSfcBlockSection ( scriptSetup , statement . start , propsRange . start , codeFeatures . all ) ,
167
- `__VLS_props` ,
168
- ] , statement . start , propsRange . end ] ) ;
169
- }
170
- }
171
- }
133
+ if ( scriptSetupRanges . props . define ) {
134
+ setupCodeModifies . push ( ...generateDefineWithType (
135
+ scriptSetup ,
136
+ scriptSetupRanges . props . name ,
137
+ scriptSetupRanges . props . define ,
138
+ scriptSetupRanges . props . withDefaults ?? scriptSetupRanges . props . define ,
139
+ '__VLS_props' ,
140
+ '__VLS_Props'
141
+ ) ) ;
172
142
}
173
143
if ( scriptSetupRanges . slots . define ) {
174
144
if ( scriptSetupRanges . slots . isObjectBindingPattern ) {
@@ -181,8 +151,15 @@ function* generateSetupFunction(
181
151
setupCodeModifies . push ( [ [ `const __VLS_slots = ` ] , scriptSetupRanges . slots . define . start , scriptSetupRanges . slots . define . start ] ) ;
182
152
}
183
153
}
184
- if ( scriptSetupRanges . emits . define && ! scriptSetupRanges . emits . name ) {
185
- setupCodeModifies . push ( [ [ `const __VLS_emit = ` ] , scriptSetupRanges . emits . define . start , scriptSetupRanges . emits . define . start ] ) ;
154
+ if ( scriptSetupRanges . emits . define ) {
155
+ setupCodeModifies . push ( ...generateDefineWithType (
156
+ scriptSetup ,
157
+ scriptSetupRanges . emits . name ,
158
+ scriptSetupRanges . emits . define ,
159
+ scriptSetupRanges . emits . define ,
160
+ '__VLS_emit' ,
161
+ '__VLS_Emit'
162
+ ) ) ;
186
163
}
187
164
if ( scriptSetupRanges . expose . define ) {
188
165
if ( scriptSetupRanges . expose . define ?. typeArg ) {
@@ -217,30 +194,21 @@ function* generateSetupFunction(
217
194
}
218
195
if ( scriptSetupRanges . cssModules . length ) {
219
196
for ( const { exp, arg } of scriptSetupRanges . cssModules ) {
220
- if ( arg ) {
221
- setupCodeModifies . push ( [
222
- [
223
- ` as Omit<__VLS_StyleModules, '$style'>[` ,
224
- generateSfcBlockSection ( scriptSetup , arg . start , arg . end , codeFeatures . all ) ,
225
- `]`
226
- ] ,
227
- exp . end ,
228
- exp . end
229
- ] ) ;
230
- }
231
- else {
232
- setupCodeModifies . push ( [
233
- [
234
- ` as __VLS_StyleModules[` ,
235
- [ '' , scriptSetup . name , exp . start , codeFeatures . verification ] ,
236
- `'$style'` ,
237
- [ '' , scriptSetup . name , exp . end , codeFeatures . verification ] ,
238
- `]`
239
- ] ,
240
- exp . end ,
241
- exp . end
242
- ] ) ;
243
- }
197
+ setupCodeModifies . push ( [
198
+ arg ? [
199
+ ` as Omit<__VLS_StyleModules, '$style'>[` ,
200
+ generateSfcBlockSection ( scriptSetup , arg . start , arg . end , codeFeatures . all ) ,
201
+ `]`
202
+ ] : [
203
+ ` as __VLS_StyleModules[` ,
204
+ [ '' , scriptSetup . name , exp . start , codeFeatures . verification ] ,
205
+ `'$style'` ,
206
+ [ '' , scriptSetup . name , exp . end , codeFeatures . verification ] ,
207
+ `]`
208
+ ] ,
209
+ exp . end ,
210
+ exp . end
211
+ ] ) ;
244
212
}
245
213
}
246
214
for ( const { define } of scriptSetupRanges . templateRefs ) {
@@ -258,25 +226,15 @@ function* generateSetupFunction(
258
226
}
259
227
setupCodeModifies = setupCodeModifies . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ;
260
228
261
- if ( setupCodeModifies . length ) {
262
- yield generateSfcBlockSection ( scriptSetup , scriptSetupRanges . importSectionEndOffset , setupCodeModifies [ 0 ] [ 1 ] , codeFeatures . all ) ;
263
- while ( setupCodeModifies . length ) {
264
- const [ codes , _start , end ] = setupCodeModifies . shift ( ) ! ;
265
- for ( const code of codes ) {
266
- yield code ;
267
- }
268
- if ( setupCodeModifies . length ) {
269
- const nextStart = setupCodeModifies [ 0 ] [ 1 ] ;
270
- yield generateSfcBlockSection ( scriptSetup , end , nextStart , codeFeatures . all ) ;
271
- }
272
- else {
273
- yield generateSfcBlockSection ( scriptSetup , end , scriptSetup . content . length , codeFeatures . all ) ;
274
- }
229
+ let nextStart = scriptSetupRanges . importSectionEndOffset ;
230
+ for ( const [ codes , start , end ] of setupCodeModifies ) {
231
+ yield generateSfcBlockSection ( scriptSetup , nextStart , start , codeFeatures . all ) ;
232
+ for ( const code of codes ) {
233
+ yield code ;
275
234
}
235
+ nextStart = end ;
276
236
}
277
- else {
278
- yield generateSfcBlockSection ( scriptSetup , scriptSetupRanges . importSectionEndOffset , scriptSetup . content . length , codeFeatures . all ) ;
279
- }
237
+ yield generateSfcBlockSection ( scriptSetup , nextStart , scriptSetup . content . length , codeFeatures . all ) ;
280
238
281
239
yield * generateScriptSectionPartiallyEnding ( scriptSetup . name , scriptSetup . content . length , '#3632/scriptSetup.vue' ) ;
282
240
@@ -288,7 +246,7 @@ function* generateSetupFunction(
288
246
}
289
247
290
248
yield * generateComponentProps ( options , ctx , scriptSetup , scriptSetupRanges , definePropMirrors ) ;
291
- yield * generateModelEmits ( options , scriptSetup , scriptSetupRanges ) ;
249
+ yield * generateModelEmit ( scriptSetup , scriptSetupRanges ) ;
292
250
yield `function __VLS_template() {${ newLine } ` ;
293
251
const templateCodegenCtx = yield * generateTemplate ( options , ctx ) ;
294
252
yield `}${ endOfLine } ` ;
@@ -311,6 +269,54 @@ function* generateSetupFunction(
311
269
}
312
270
}
313
271
272
+ function * generateDefineWithType (
273
+ scriptSetup : NonNullable < Sfc [ 'scriptSetup' ] > ,
274
+ name : string | undefined ,
275
+ define : {
276
+ statement : TextRange ,
277
+ typeArg ?: TextRange
278
+ } ,
279
+ expression : TextRange ,
280
+ defaultName : string ,
281
+ typeName : string
282
+ ) : Generator < [ Code [ ] , number , number ] > {
283
+ const { statement, typeArg } = define ;
284
+ if ( typeArg ) {
285
+ yield [ [
286
+ `type ${ typeName } = ` ,
287
+ generateSfcBlockSection ( scriptSetup , typeArg . start , typeArg . end , codeFeatures . all ) ,
288
+ endOfLine ,
289
+ ] , statement . start , statement . start ] ;
290
+ yield [ [ typeName ] , typeArg . start , typeArg . end ] ;
291
+ }
292
+ if ( ! name ) {
293
+ if ( statement . start === expression . start && statement . end === expression . end ) {
294
+ yield [ [ `const ${ defaultName } = ` ] , expression . start , expression . start ] ;
295
+ }
296
+ else if ( typeArg ) {
297
+ yield [ [
298
+ `const ${ defaultName } = ` ,
299
+ generateSfcBlockSection ( scriptSetup , expression . start , typeArg . start , codeFeatures . all )
300
+ ] , statement . start , typeArg . start ] ;
301
+ yield [ [
302
+ generateSfcBlockSection ( scriptSetup , typeArg . end , expression . end , codeFeatures . all ) ,
303
+ endOfLine ,
304
+ generateSfcBlockSection ( scriptSetup , statement . start , expression . start , codeFeatures . all ) ,
305
+ defaultName
306
+ ] , typeArg . end , expression . end ] ;
307
+ }
308
+ else {
309
+ yield [ [
310
+ `const ${ defaultName } = ` ,
311
+ generateSfcBlockSection ( scriptSetup , expression . start , expression . end , codeFeatures . all ) ,
312
+ endOfLine ,
313
+ generateSfcBlockSection ( scriptSetup , statement . start , expression . start , codeFeatures . all ) ,
314
+ defaultName
315
+ ] , statement . start , expression . end ] ;
316
+ }
317
+ }
318
+ }
319
+
314
320
function * generateComponentProps (
315
321
options : ScriptCodegenOptions ,
316
322
ctx : ScriptCodegenContext ,
@@ -326,7 +332,7 @@ function* generateComponentProps(
326
332
yield `,${ newLine } ` ;
327
333
}
328
334
329
- yield * generateEmitsOption ( options , scriptSetup , scriptSetupRanges ) ;
335
+ yield * generateEmitsOption ( options , scriptSetupRanges ) ;
330
336
331
337
yield `})${ endOfLine } ` ;
332
338
@@ -422,40 +428,29 @@ function* generateComponentProps(
422
428
yield ` & ` ;
423
429
}
424
430
ctx . generatedPropsType = true ;
425
- yield `typeof __VLS_typeProps ` ;
431
+ yield `__VLS_Props ` ;
426
432
}
427
433
if ( ! ctx . generatedPropsType ) {
428
434
yield `{}` ;
429
435
}
430
436
yield endOfLine ;
431
437
}
432
438
433
- function * generateModelEmits (
434
- options : ScriptCodegenOptions ,
439
+ function * generateModelEmit (
435
440
scriptSetup : NonNullable < Sfc [ 'scriptSetup' ] > ,
436
441
scriptSetupRanges : ScriptSetupRanges
437
442
) : Generator < Code > {
438
443
const defineModels = scriptSetupRanges . defineProp . filter ( p => p . isModel ) ;
439
444
if ( defineModels . length ) {
440
- const generateDefineModels = function * ( ) {
441
- for ( const defineModel of defineModels ) {
442
- const [ propName , localName ] = getPropAndLocalName ( scriptSetup , defineModel ) ;
443
- yield `'update:${ propName } ': [${ propName } :` ;
444
- yield * generateDefinePropType ( scriptSetup , propName , localName , defineModel ) ;
445
- yield `]${ endOfLine } ` ;
446
- }
447
- } ;
448
- if ( options . vueCompilerOptions . target >= 3.5 ) {
449
- yield `type __VLS_ModelEmitsType = {${ newLine } ` ;
450
- yield * generateDefineModels ( ) ;
451
- yield `}${ endOfLine } ` ;
452
- }
453
- else {
454
- yield `const __VLS_modelEmitsType = (await import('${ options . vueCompilerOptions . lib } ')).defineEmits<{${ newLine } ` ;
455
- yield * generateDefineModels ( ) ;
456
- yield `}>()${ endOfLine } ` ;
457
- yield `type __VLS_ModelEmitsType = typeof __VLS_modelEmitsType${ endOfLine } ` ;
445
+ yield `type __VLS_ModelEmit = {${ newLine } ` ;
446
+ for ( const defineModel of defineModels ) {
447
+ const [ propName , localName ] = getPropAndLocalName ( scriptSetup , defineModel ) ;
448
+ yield `'update:${ propName } ': [${ propName } :` ;
449
+ yield * generateDefinePropType ( scriptSetup , propName , localName , defineModel ) ;
450
+ yield `]${ endOfLine } ` ;
458
451
}
452
+ yield `}${ endOfLine } ` ;
453
+ yield `const __VLS_modelEmit = defineEmits<__VLS_ModelEmit>()${ endOfLine } ` ;
459
454
}
460
455
}
461
456
0 commit comments