1
- import type { ScriptRanges } from '../../parsers/scriptRanges' ;
2
1
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges' ;
3
2
import type { Code , Sfc } from '../../types' ;
4
3
import { endOfLine , generateSfcBlockSection , newLine } from '../common' ;
@@ -31,10 +30,15 @@ export function* generateComponent(
31
30
yield `}${ endOfLine } ` ;
32
31
yield `},${ newLine } ` ;
33
32
if ( ! ctx . bypassDefineComponent ) {
34
- yield * generateScriptSetupOptions ( options , ctx , scriptSetup , scriptSetupRanges , true ) ;
33
+ const emitOptionCodes = [ ...generateEmitsOption ( options , scriptSetup , scriptSetupRanges ) ] ;
34
+ for ( const code of emitOptionCodes ) {
35
+ yield code ;
36
+ }
37
+ yield * generatePropsOption ( options , ctx , scriptSetup , scriptSetupRanges , ! ! emitOptionCodes . length , true ) ;
35
38
}
36
- if ( options . sfc . script && options . scriptRanges ) {
37
- yield * generateScriptOptions ( options . sfc . script , options . scriptRanges ) ;
39
+ if ( options . sfc . script && options . scriptRanges ?. exportDefault ?. args ) {
40
+ const { args } = options . scriptRanges . exportDefault ;
41
+ yield generateSfcBlockSection ( options . sfc . script , args . start + 1 , args . end - 1 , codeFeatures . all ) ;
38
42
}
39
43
if ( options . vueCompilerOptions . target >= 3.5 && scriptSetupRanges . templateRefs . length ) {
40
44
yield `__typeRefs: {} as __VLS_Refs,${ newLine } ` ;
@@ -55,153 +59,124 @@ export function* generateComponentSetupReturns(scriptSetupRanges: ScriptSetupRan
55
59
}
56
60
}
57
61
58
- export function * generateScriptOptions (
59
- script : NonNullable < Sfc [ 'script' ] > ,
60
- scriptRanges : ScriptRanges
61
- ) : Generator < Code > {
62
- if ( scriptRanges . exportDefault ?. args ) {
63
- yield generateSfcBlockSection ( script , scriptRanges . exportDefault . args . start + 1 , scriptRanges . exportDefault . args . end - 1 , codeFeatures . all ) ;
64
- }
65
- }
66
-
67
- export function * generateScriptSetupOptions (
68
- options : ScriptCodegenOptions ,
69
- ctx : ScriptCodegenContext ,
70
- scriptSetup : NonNullable < Sfc [ 'scriptSetup' ] > ,
71
- scriptSetupRanges : ScriptSetupRanges ,
72
- inheritAttrs : boolean
73
- ) : Generator < Code > {
74
- const emitOptionCodes = [ ...generateEmitsOption ( options , scriptSetup , scriptSetupRanges ) ] ;
75
- for ( const code of emitOptionCodes ) {
76
- yield code ;
77
- }
78
-
79
- if ( options . vueCompilerOptions . target >= 3.5 ) {
80
- const types = [ ] ;
81
- if ( inheritAttrs && options . templateCodegen ?. inheritedAttrVars . size && ! emitOptionCodes . length ) {
82
- types . push ( `{} as ReturnType<typeof __VLS_template>['attrs']` ) ;
83
- }
84
- if ( ctx . generatedPropsType ) {
85
- types . push ( `{} as __VLS_PublicProps` ) ;
86
- }
87
- if ( types . length === 1 ) {
88
- yield `__typeProps: ${ types [ 0 ] } ,${ newLine } ` ;
89
- }
90
- else if ( types . length >= 2 ) {
91
- yield `__typeProps: {${ newLine } ` ;
92
- for ( const type of types ) {
93
- yield `...${ type } ,${ newLine } ` ;
94
- }
95
- yield `},${ newLine } ` ;
96
- }
97
- }
98
- if ( options . vueCompilerOptions . target < 3.5 || ! ctx . generatedPropsType || scriptSetupRanges . props . withDefaults ) {
99
- const codegens : ( ( ) => Generator < Code > ) [ ] = [ ] ;
100
-
101
- if ( inheritAttrs && options . templateCodegen ?. inheritedAttrVars . size && ! emitOptionCodes . length ) {
102
- codegens . push ( function * ( ) {
103
- yield `{} as ${ ctx . helperTypes . TypePropsToOption . name } <__VLS_PickNotAny<${ ctx . helperTypes . OmitIndexSignature . name } <ReturnType<typeof __VLS_template>['attrs']>, {}>>` ;
104
- } ) ;
105
- }
106
-
107
- if ( ctx . generatedPropsType ) {
108
- codegens . push ( function * ( ) {
109
- yield `{} as ` ;
110
- if ( scriptSetupRanges . props . withDefaults ?. arg ) {
111
- yield `${ ctx . helperTypes . WithDefaults . name } <` ;
112
- }
113
- yield `${ ctx . helperTypes . TypePropsToOption . name } <` ;
114
- yield `__VLS_PublicProps>` ;
115
- if ( scriptSetupRanges . props . withDefaults ?. arg ) {
116
- yield `, typeof __VLS_withDefaultsArg>` ;
117
- }
118
- } ) ;
119
- }
120
- if ( scriptSetupRanges . props . define ?. arg ) {
121
- const { arg } = scriptSetupRanges . props . define ;
122
- codegens . push ( function * ( ) {
123
- yield generateSfcBlockSection ( scriptSetup , arg . start , arg . end , codeFeatures . navigation ) ;
124
- } ) ;
125
- }
126
-
127
- if ( codegens . length === 1 ) {
128
- yield `props: ` ;
129
- for ( const generate of codegens ) {
130
- yield * generate ( ) ;
131
- }
132
- yield `,${ newLine } ` ;
133
- }
134
- else if ( codegens . length >= 2 ) {
135
- yield `props: {${ newLine } ` ;
136
- for ( const generate of codegens ) {
137
- yield `...` ;
138
- yield * generate ( ) ;
139
- yield `,${ newLine } ` ;
140
- }
141
- yield `},${ newLine } ` ;
142
- }
143
- }
144
- }
145
-
146
62
export function * generateEmitsOption (
147
63
options : ScriptCodegenOptions ,
148
64
scriptSetup : NonNullable < Sfc [ 'scriptSetup' ] > ,
149
65
scriptSetupRanges : ScriptSetupRanges
150
66
) : Generator < Code > {
151
67
const codes : {
152
- optionExp ?: Code [ ] ,
153
- typeOptionType ?: Code [ ] ,
68
+ optionExp ?: Code ,
69
+ typeOptionType ?: Code ,
154
70
} [ ] = [ ] ;
155
71
if ( scriptSetupRanges . defineProp . some ( p => p . isModel ) ) {
156
72
codes . push ( {
157
- optionExp : [ `{} as __VLS_NormalizeEmits<__VLS_ModelEmitsType>` ] ,
158
- typeOptionType : [ `__VLS_ModelEmitsType` ] ,
73
+ optionExp : `{} as __VLS_NormalizeEmits<__VLS_ModelEmitsType>` ,
74
+ typeOptionType : `__VLS_ModelEmitsType` ,
159
75
} ) ;
160
76
}
161
77
if ( scriptSetupRanges . emits . define ) {
162
78
const { typeArg, hasUnionTypeArg } = scriptSetupRanges . emits . define ;
163
79
codes . push ( {
164
- optionExp : [ `{} as __VLS_NormalizeEmits<typeof ` , scriptSetupRanges . emits . name ?? '__VLS_emit' , `>` ] ,
165
- typeOptionType : typeArg && ! hasUnionTypeArg ? [ scriptSetup . content . slice ( typeArg . start , typeArg . end ) ] : undefined ,
80
+ optionExp : `{} as __VLS_NormalizeEmits<typeof ${ scriptSetupRanges . emits . name ?? '__VLS_emit' } >` ,
81
+ typeOptionType : typeArg && ! hasUnionTypeArg
82
+ ? scriptSetup . content . slice ( typeArg . start , typeArg . end )
83
+ : undefined ,
166
84
} ) ;
167
85
}
168
86
if ( options . vueCompilerOptions . target >= 3.5 && codes . every ( code => code . typeOptionType ) ) {
169
87
if ( codes . length === 1 ) {
170
88
yield `__typeEmits: {} as ` ;
171
- for ( const code of codes [ 0 ] . typeOptionType ! ) {
172
- yield code ;
173
- }
89
+ yield codes [ 0 ] . typeOptionType ! ;
174
90
yield `,${ newLine } ` ;
175
91
}
176
92
else if ( codes . length >= 2 ) {
177
93
yield `__typeEmits: {} as ` ;
178
- for ( const code of codes [ 0 ] . typeOptionType ! ) {
179
- yield code ;
180
- }
94
+ yield codes [ 0 ] . typeOptionType ! ;
181
95
for ( let i = 1 ; i < codes . length ; i ++ ) {
182
96
yield ` & ` ;
183
- for ( const code of codes [ i ] . typeOptionType ! ) {
184
- yield code ;
185
- }
97
+ yield codes [ i ] . typeOptionType ! ;
186
98
}
187
99
yield `,${ newLine } ` ;
188
100
}
189
101
}
190
102
else if ( codes . every ( code => code . optionExp ) ) {
191
103
if ( codes . length === 1 ) {
192
104
yield `emits: ` ;
193
- for ( const code of codes [ 0 ] . optionExp ! ) {
194
- yield code ;
195
- }
105
+ yield codes [ 0 ] . optionExp ! ;
196
106
yield `,${ newLine } ` ;
197
107
}
198
108
else if ( codes . length >= 2 ) {
199
109
yield `emits: {${ newLine } ` ;
200
110
for ( const code of codes ) {
201
111
yield `...` ;
202
- for ( const c of code . optionExp ! ) {
203
- yield c ;
204
- }
112
+ yield code . optionExp ! ;
113
+ yield `,${ newLine } ` ;
114
+ }
115
+ yield `},${ newLine } ` ;
116
+ }
117
+ }
118
+ }
119
+
120
+ export function * generatePropsOption (
121
+ options : ScriptCodegenOptions ,
122
+ ctx : ScriptCodegenContext ,
123
+ scriptSetup : NonNullable < Sfc [ 'scriptSetup' ] > ,
124
+ scriptSetupRanges : ScriptSetupRanges ,
125
+ hasEmitsOption : boolean ,
126
+ inheritAttrs : boolean
127
+ ) : Generator < Code > {
128
+ const optionExpCodes : Code [ ] = [ ] ;
129
+ const typeOptionExpCodes : Code [ ] = [ ] ;
130
+
131
+ if ( inheritAttrs && options . templateCodegen ?. inheritedAttrVars . size && ! hasEmitsOption ) {
132
+ optionExpCodes . push ( `{} as ${ ctx . helperTypes . TypePropsToOption . name } <__VLS_PickNotAny<${ ctx . helperTypes . OmitIndexSignature . name } <ReturnType<typeof __VLS_template>['attrs']>, {}>>` ) ;
133
+ typeOptionExpCodes . push ( `{} as ReturnType<typeof __VLS_template>['attrs']` ) ;
134
+ }
135
+ if ( ctx . generatedPropsType ) {
136
+ optionExpCodes . push ( [
137
+ `{} as ` ,
138
+ scriptSetupRanges . props . withDefaults ?. arg ? `${ ctx . helperTypes . WithDefaults . name } <` : '' ,
139
+ `${ ctx . helperTypes . TypePropsToOption . name } <__VLS_PublicProps>` ,
140
+ scriptSetupRanges . props . withDefaults ?. arg ? `, typeof __VLS_withDefaultsArg>` : '' ,
141
+ ] . join ( '' ) ) ;
142
+ typeOptionExpCodes . push ( `{} as __VLS_PublicProps` ) ;
143
+ }
144
+
145
+ if ( scriptSetupRanges . props . define ?. arg ) {
146
+ const { arg } = scriptSetupRanges . props . define ;
147
+ optionExpCodes . push ( generateSfcBlockSection ( scriptSetup , arg . start , arg . end , codeFeatures . navigation ) ) ;
148
+ }
149
+
150
+ const useTypeOption = options . vueCompilerOptions . target >= 3.5 && typeOptionExpCodes . length ;
151
+ const useOption = ( ! useTypeOption || scriptSetupRanges . props . withDefaults ) && optionExpCodes . length ;
152
+
153
+ if ( useTypeOption ) {
154
+ if ( typeOptionExpCodes . length === 1 ) {
155
+ yield `__typeProps: ` ;
156
+ yield typeOptionExpCodes [ 0 ] ;
157
+ yield `,${ newLine } ` ;
158
+ }
159
+ else if ( typeOptionExpCodes . length >= 2 ) {
160
+ yield `__typeProps: {${ newLine } ` ;
161
+ for ( const code of typeOptionExpCodes ) {
162
+ yield `...` ;
163
+ yield code ;
164
+ yield `,${ newLine } ` ;
165
+ }
166
+ yield `},${ newLine } ` ;
167
+ }
168
+ }
169
+ if ( useOption ) {
170
+ if ( optionExpCodes . length === 1 ) {
171
+ yield `props: ` ;
172
+ yield optionExpCodes [ 0 ] ;
173
+ yield `,${ newLine } ` ;
174
+ }
175
+ else if ( optionExpCodes . length >= 2 ) {
176
+ yield `props: {${ newLine } ` ;
177
+ for ( const code of optionExpCodes ) {
178
+ yield `...` ;
179
+ yield code ;
205
180
yield `,${ newLine } ` ;
206
181
}
207
182
yield `},${ newLine } ` ;
0 commit comments