@@ -102,13 +102,40 @@ export function migrate(source) {
102102 state = { ...state , scope : analysis . template . scope } ;
103103 walk ( parsed . fragment , state , template ) ;
104104
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+
105120 const specifiers = [ ...state . legacy_imports ] . map ( ( imported ) => {
106121 const local = state . names [ imported ] ;
107122 return imported === local ? imported : `${ imported } as ${ local } ` ;
108123 } ) ;
109124
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 ;
112139
113140 if ( state . props . length > 0 || analysis . uses_rest_props || analysis . uses_props ) {
114141 const has_many_props = state . props . length > 3 ;
@@ -138,7 +165,7 @@ export function migrate(source) {
138165
139166 if ( state . has_props_rune ) {
140167 // some render tags or forwarded event attributes to add
141- str . appendRight ( state . props_insertion_point , ` ${ props } ,` ) ;
168+ str . appendRight ( insertion_point , ` ${ props } ,` ) ;
142169 } else {
143170 const uses_ts = parsed . instance ?. attributes . some (
144171 ( attr ) => attr . name === 'lang' && /** @type {any } */ ( attr ) . value [ 0 ] . data === 'ts'
@@ -177,20 +204,8 @@ export function migrate(source) {
177204 props_declaration = `${ props_declaration } = $props();` ;
178205 }
179206
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 ) ;
194209 }
195210 }
196211
@@ -235,24 +250,9 @@ export function migrate(source) {
235250 }
236251 }
237252
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' ) ;
254255 }
255-
256256 return { code : str . toString ( ) } ;
257257 } catch ( e ) {
258258 // eslint-disable-next-line no-console
@@ -843,22 +843,6 @@ function get_node_range(source, node) {
843843 return { start, end } ;
844844}
845845
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-
862846/**
863847 * @param {Identifier } node
864848 * @param {State } state
0 commit comments