@@ -76,15 +76,18 @@ class __sveltets_Render${genericsDef} {
76
76
const svelteComponentClass = noSvelteComponentTyped
77
77
? 'SvelteComponent'
78
78
: 'SvelteComponentTyped' ;
79
+ const [ PropsName ] = addTypeExport ( str , className , 'Props' ) ;
80
+ const [ EventsName ] = addTypeExport ( str , className , 'Events' ) ;
81
+ const [ SlotsName ] = addTypeExport ( str , className , 'Slots' ) ;
79
82
80
83
if ( mode === 'dts' ) {
81
84
statement +=
82
- `export type ${ className } Props ${ genericsDef } = ${ returnType ( 'props' ) } ;\n` +
83
- `export type ${ className } Events ${ genericsDef } = ${ returnType ( 'events' ) } ;\n` +
84
- `export type ${ className } Slots ${ genericsDef } = ${ returnType ( 'slots' ) } ;\n` +
85
+ `export type ${ PropsName } ${ genericsDef } = ${ returnType ( 'props' ) } ;\n` +
86
+ `export type ${ EventsName } ${ genericsDef } = ${ returnType ( 'events' ) } ;\n` +
87
+ `export type ${ SlotsName } ${ genericsDef } = ${ returnType ( 'slots' ) } ;\n` +
85
88
`\n${ doc } export default class${
86
89
className ? ` ${ className } ` : ''
87
- } ${ genericsDef } extends ${ svelteComponentClass } <${ className } Props ${ genericsRef } , ${ className } Events ${ genericsRef } , ${ className } Slots ${ genericsRef } > {` +
90
+ } ${ genericsDef } extends ${ svelteComponentClass } <${ PropsName } ${ genericsRef } , ${ EventsName } ${ genericsRef } , ${ SlotsName } ${ genericsRef } > {` +
88
91
exportedNames . createClassGetters ( ) +
89
92
( usesAccessors ? exportedNames . createClassAccessors ( ) : '' ) +
90
93
'\n}' ;
@@ -128,32 +131,21 @@ function addSimpleComponentExport({
128
131
129
132
let statement : string ;
130
133
if ( mode === 'dts' && isTsFile ) {
131
- const addTypeExport = ( type : string ) => {
132
- const exportName = className + type ;
133
-
134
- if ( ! str . original . includes ( exportName ) ) {
135
- return `export type ${ exportName } = typeof __propDef.${ type . toLowerCase ( ) } ;\n` ;
136
- }
137
-
138
- let replacement = exportName + '_' ;
139
- while ( str . original . includes ( replacement ) ) {
140
- replacement += '_' ;
141
- }
142
- return `type ${ replacement } = typeof __propDef.${ type . toLowerCase ( ) } ;\nexport { ${ replacement } as ${ exportName } };\n` ;
143
- } ;
144
-
145
134
const svelteComponentClass = noSvelteComponentTyped
146
135
? 'SvelteComponent'
147
136
: 'SvelteComponentTyped' ;
137
+ const [ PropsName , PropsExport ] = addTypeExport ( str , className , 'Props' ) ;
138
+ const [ EventsName , EventsExport ] = addTypeExport ( str , className , 'Events' ) ;
139
+ const [ SlotsName , SlotsExport ] = addTypeExport ( str , className , 'Slots' ) ;
148
140
149
141
statement =
150
142
`\nconst __propDef = ${ propDef } ;\n` +
151
- addTypeExport ( 'Props' ) +
152
- addTypeExport ( 'Events' ) +
153
- addTypeExport ( 'Slots' ) +
143
+ PropsExport +
144
+ EventsExport +
145
+ SlotsExport +
154
146
`\n${ doc } export default class${
155
147
className ? ` ${ className } ` : ''
156
- } extends ${ svelteComponentClass } <${ className } Props , ${ className } Events , ${ className } Slots > {` +
148
+ } extends ${ svelteComponentClass } <${ PropsName } , ${ EventsName } , ${ SlotsName } > {` +
157
149
exportedNames . createClassGetters ( ) +
158
150
( usesAccessors ? exportedNames . createClassAccessors ( ) : '' ) +
159
151
'\n}' ;
@@ -182,6 +174,44 @@ function addSimpleComponentExport({
182
174
str . append ( statement ) ;
183
175
}
184
176
177
+ function addTypeExport (
178
+ str : MagicString ,
179
+ className : string ,
180
+ type : string
181
+ ) : [ name : string , exportstring : string ] {
182
+ const exportName = className + type ;
183
+
184
+ if ( ! new RegExp ( `\\W${ exportName } \\W` ) . test ( str . original ) ) {
185
+ return [
186
+ exportName ,
187
+ `export type ${ exportName } = typeof __propDef.${ type . toLowerCase ( ) } ;\n`
188
+ ] ;
189
+ }
190
+
191
+ let replacement = exportName + '_' ;
192
+ while ( str . original . includes ( replacement ) ) {
193
+ replacement += '_' ;
194
+ }
195
+
196
+ if (
197
+ // Check if there's already an export with the same name
198
+ ! new RegExp (
199
+ `export ((const|let|var|class|interface|type) ${ exportName } \\W|{[^}]*?${ exportName } (}|\\W.*?}))`
200
+ ) . test ( str . original )
201
+ ) {
202
+ return [
203
+ replacement ,
204
+ `type ${ replacement } = typeof __propDef.${ type . toLowerCase ( ) } ;\nexport { ${ replacement } as ${ exportName } };\n`
205
+ ] ;
206
+ } else {
207
+ return [
208
+ replacement ,
209
+ // we assume that the author explicitly named the type the same and don't export the generated type (which has an unstable name)
210
+ `type ${ replacement } = typeof __propDef.${ type . toLowerCase ( ) } ;\n`
211
+ ] ;
212
+ }
213
+ }
214
+
185
215
function events ( strictEvents : boolean , renderStr : string ) {
186
216
return strictEvents ? renderStr : `__sveltets_2_with_any_event(${ renderStr } )` ;
187
217
}
0 commit comments