@@ -102,13 +102,42 @@ 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 . derived_components . size > 0 ||
112
+ state . script_insertions . size > 0 ||
113
+ state . props . length > 0 ||
114
+ analysis . uses_rest_props ||
115
+ analysis . uses_props ;
116
+
117
+ if ( ! parsed . instance && need_script ) {
118
+ const to_prepend = '<script>' ;
119
+ str . appendRight ( 0 , to_prepend ) ;
120
+ }
121
+
105
122
const specifiers = [ ...state . legacy_imports ] . map ( ( imported ) => {
106
123
const local = state . names [ imported ] ;
107
124
return imported === local ? imported : `${ imported } as ${ local } ` ;
108
125
} ) ;
109
126
110
- const legacy_import = `import { ${ specifiers . join ( ', ' ) } } from 'svelte/legacy';` ;
111
- let added_legacy_import = false ;
127
+ const legacy_import = `import { ${ specifiers . join ( ', ' ) } } from 'svelte/legacy';\n` ;
128
+
129
+ if ( state . legacy_imports . size > 0 ) {
130
+ str . appendRight ( insertion_point , `\n${ indent } ${ legacy_import } ` ) ;
131
+ }
132
+
133
+ if ( state . script_insertions . size > 0 ) {
134
+ str . appendRight (
135
+ insertion_point ,
136
+ `\n${ indent } ${ [ ...state . script_insertions ] . join ( `\n${ indent } ` ) } `
137
+ ) ;
138
+ }
139
+
140
+ insertion_point = state . props_insertion_point ;
112
141
113
142
if ( state . props . length > 0 || analysis . uses_rest_props || analysis . uses_props ) {
114
143
const has_many_props = state . props . length > 3 ;
@@ -138,7 +167,7 @@ export function migrate(source) {
138
167
139
168
if ( state . has_props_rune ) {
140
169
// some render tags or forwarded event attributes to add
141
- str . appendRight ( state . props_insertion_point , ` ${ props } ,` ) ;
170
+ str . appendRight ( insertion_point , ` ${ props } ,` ) ;
142
171
} else {
143
172
const uses_ts = parsed . instance ?. attributes . some (
144
173
( attr ) => attr . name === 'lang' && /** @type {any } */ ( attr ) . value [ 0 ] . data === 'ts'
@@ -177,20 +206,8 @@ export function migrate(source) {
177
206
props_declaration = `${ props_declaration } = $props();` ;
178
207
}
179
208
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
- }
209
+ props_declaration = `\n${ indent } ${ props_declaration } ` ;
210
+ str . appendRight ( insertion_point , props_declaration ) ;
194
211
}
195
212
}
196
213
@@ -235,24 +252,10 @@ export function migrate(source) {
235
252
}
236
253
}
237
254
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
- }
255
+
256
+ if ( ! parsed . instance && need_script ) {
257
+ str . appendRight ( insertion_point , '\n</script>\n\n' ) ;
254
258
}
255
-
256
259
return { code : str . toString ( ) } ;
257
260
} catch ( e ) {
258
261
// eslint-disable-next-line no-console
@@ -843,22 +846,6 @@ function get_node_range(source, node) {
843
846
return { start, end } ;
844
847
}
845
848
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
849
/**
863
850
* @param {Identifier } node
864
851
* @param {State } state
0 commit comments