@@ -102,13 +102,40 @@ export function migrate(source) {
102
102
state = { ...state , scope : analysis . template . scope } ;
103
103
walk ( parsed . fragment , state , template ) ;
104
104
105
+ let insertion_point = parsed . instance
106
+ ? /** @type {number } */ ( parsed . instance . content . start )
107
+ : 0 ;
108
+
109
+ const need_script =
110
+ state . legacy_imports . size > 0 ||
111
+ state . script_insertions . size > 0 ||
112
+ state . props . length > 0 ||
113
+ analysis . uses_rest_props ||
114
+ analysis . uses_props ;
115
+
116
+ if ( ! parsed . instance && need_script ) {
117
+ str . appendRight ( 0 , '<script>' ) ;
118
+ }
119
+
105
120
const specifiers = [ ...state . legacy_imports ] . map ( ( imported ) => {
106
121
const local = state . names [ imported ] ;
107
122
return imported === local ? imported : `${ imported } as ${ local } ` ;
108
123
} ) ;
109
124
110
- const legacy_import = `import { ${ specifiers . join ( ', ' ) } } from 'svelte/legacy';` ;
111
- let added_legacy_import = false ;
125
+ const legacy_import = `import { ${ specifiers . join ( ', ' ) } } from 'svelte/legacy';\n` ;
126
+
127
+ if ( state . legacy_imports . size > 0 ) {
128
+ str . appendRight ( insertion_point , `\n${ indent } ${ legacy_import } ` ) ;
129
+ }
130
+
131
+ if ( state . script_insertions . size > 0 ) {
132
+ str . appendRight (
133
+ insertion_point ,
134
+ `\n${ indent } ${ [ ...state . script_insertions ] . join ( `\n${ indent } ` ) } `
135
+ ) ;
136
+ }
137
+
138
+ insertion_point = state . props_insertion_point ;
112
139
113
140
if ( state . props . length > 0 || analysis . uses_rest_props || analysis . uses_props ) {
114
141
const has_many_props = state . props . length > 3 ;
@@ -138,7 +165,7 @@ export function migrate(source) {
138
165
139
166
if ( state . has_props_rune ) {
140
167
// some render tags or forwarded event attributes to add
141
- str . appendRight ( state . props_insertion_point , ` ${ props } ,` ) ;
168
+ str . appendRight ( insertion_point , ` ${ props } ,` ) ;
142
169
} else {
143
170
const uses_ts = parsed . instance ?. attributes . some (
144
171
( attr ) => attr . name === 'lang' && /** @type {any } */ ( attr ) . value [ 0 ] . data === 'ts'
@@ -177,20 +204,8 @@ export function migrate(source) {
177
204
props_declaration = `${ props_declaration } = $props();` ;
178
205
}
179
206
180
- if ( parsed . instance ) {
181
- props_declaration = `\n${ indent } ${ props_declaration } ` ;
182
- str . appendRight ( state . props_insertion_point , props_declaration ) ;
183
- } else {
184
- const imports = state . legacy_imports . size > 0 ? `${ indent } ${ legacy_import } \n` : '' ;
185
- const script_insertions =
186
- state . script_insertions . size > 0
187
- ? `\n${ indent } ${ [ ...state . script_insertions ] . join ( `\n${ indent } ` ) } `
188
- : '' ;
189
- str . prepend (
190
- `<script>\n${ imports } ${ indent } ${ props_declaration } ${ script_insertions } \n</script>\n\n`
191
- ) ;
192
- added_legacy_import = true ;
193
- }
207
+ props_declaration = `\n${ indent } ${ props_declaration } ` ;
208
+ str . appendRight ( insertion_point , props_declaration ) ;
194
209
}
195
210
}
196
211
@@ -235,24 +250,9 @@ export function migrate(source) {
235
250
}
236
251
}
237
252
238
- if ( state . legacy_imports . size > 0 && ! added_legacy_import ) {
239
- const script_insertions =
240
- state . script_insertions . size > 0
241
- ? `\n${ indent } ${ [ ...state . script_insertions ] . join ( `\n${ indent } ` ) } `
242
- : '' ;
243
-
244
- if ( parsed . instance ) {
245
- str . appendRight (
246
- /** @type {number } */ ( parsed . instance . content . start ) ,
247
- `\n${ indent } ${ legacy_import } ${ script_insertions } \n`
248
- ) ;
249
- } else {
250
- str . prepend (
251
- `<script>\n${ indent } ${ legacy_import } \n${ indent } ${ script_insertions } \n</script>\n\n`
252
- ) ;
253
- }
253
+ if ( ! parsed . instance && need_script ) {
254
+ str . appendRight ( insertion_point , '\n</script>\n\n' ) ;
254
255
}
255
-
256
256
return { code : str . toString ( ) } ;
257
257
} catch ( e ) {
258
258
// eslint-disable-next-line no-console
@@ -843,22 +843,6 @@ function get_node_range(source, node) {
843
843
return { start, end } ;
844
844
}
845
845
846
- /**
847
- * @param {AST.OnDirective } last
848
- * @param {State } state
849
- */
850
- function generate_event_name ( last , state ) {
851
- const scope =
852
- ( last . expression && state . analysis . template . scopes . get ( last . expression ) ) || state . scope ;
853
-
854
- let name = 'event' ;
855
- if ( ! scope . get ( name ) ) return name ;
856
-
857
- let i = 1 ;
858
- while ( scope . get ( `${ name } ${ i } ` ) ) i += 1 ;
859
- return `${ name } ${ i } ` ;
860
- }
861
-
862
846
/**
863
847
* @param {Identifier } node
864
848
* @param {State } state
0 commit comments