@ -102,13 +102,40 @@ export function migrate(source) {
state = { ... state , scope : analysis . template . scope } ;
walk ( parsed . fragment , state , template ) ;
let insertion _point = parsed . instance
? /** @type {number} */ ( parsed . instance . content . start )
: 0 ;
const need _script =
state . legacy _imports . size > 0 ||
state . script _insertions . size > 0 ||
state . props . length > 0 ||
analysis . uses _rest _props ||
analysis . uses _props ;
if ( ! parsed . instance && need _script ) {
str . appendRight ( 0 , '<script>' ) ;
}
const specifiers = [ ... state . legacy _imports ] . map ( ( imported ) => {
const local = state . names [ imported ] ;
return imported === local ? imported : ` ${ imported } as ${ local } ` ;
} ) ;
const legacy _import = ` import { ${ specifiers . join ( ', ' ) } } from 'svelte/legacy'; ` ;
let added _legacy _import = false ;
const legacy _import = ` import { ${ specifiers . join ( ', ' ) } } from 'svelte/legacy'; \n ` ;
if ( state . legacy _imports . size > 0 ) {
str . appendRight ( insertion _point , ` \n ${ indent } ${ legacy _import } ` ) ;
}
if ( state . script _insertions . size > 0 ) {
str . appendRight (
insertion _point ,
` \n ${ indent } ${ [ ... state . script _insertions ] . join ( ` \n ${ indent } ` ) } `
) ;
}
insertion _point = state . props _insertion _point ;
if ( state . props . length > 0 || analysis . uses _rest _props || analysis . uses _props ) {
const has _many _props = state . props . length > 3 ;
@ -138,7 +165,7 @@ export function migrate(source) {
if ( state . has _props _rune ) {
// some render tags or forwarded event attributes to add
str . appendRight ( state. props _ insertion_point , ` ${ props } , ` ) ;
str . appendRight ( insertion_point , ` ${ props } , ` ) ;
} else {
const uses _ts = parsed . instance ? . attributes . some (
( attr ) => attr . name === 'lang' && /** @type {any} */ ( attr ) . value [ 0 ] . data === 'ts'
@ -177,20 +204,8 @@ export function migrate(source) {
props _declaration = ` ${ props _declaration } = $ props(); ` ;
}
if ( parsed . instance ) {
props _declaration = ` \n ${ indent } ${ props _declaration } ` ;
str . appendRight ( state . props _insertion _point , props _declaration ) ;
} else {
const imports = state . legacy _imports . size > 0 ? ` ${ indent } ${ legacy _import } \n ` : '' ;
const script _insertions =
state . script _insertions . size > 0
? ` \n ${ indent } ${ [ ... state . script _insertions ] . join ( ` \n ${ indent } ` ) } `
: '' ;
str . prepend (
` <script> \n ${ imports } ${ indent } ${ props _declaration } ${ script _insertions } \n </script> \n \n `
) ;
added _legacy _import = true ;
}
props _declaration = ` \n ${ indent } ${ props _declaration } ` ;
str . appendRight ( insertion _point , props _declaration ) ;
}
}
@ -235,24 +250,9 @@ export function migrate(source) {
}
}
if ( state . legacy _imports . size > 0 && ! added _legacy _import ) {
const script _insertions =
state . script _insertions . size > 0
? ` \n ${ indent } ${ [ ... state . script _insertions ] . join ( ` \n ${ indent } ` ) } `
: '' ;
if ( parsed . instance ) {
str . appendRight (
/** @type {number} */ ( parsed . instance . content . start ) ,
` \n ${ indent } ${ legacy _import } ${ script _insertions } \n `
) ;
} else {
str . prepend (
` <script> \n ${ indent } ${ legacy _import } \n ${ indent } ${ script _insertions } \n </script> \n \n `
) ;
}
if ( ! parsed . instance && need _script ) {
str . appendRight ( insertion _point , '\n</script>\n\n' ) ;
}
return { code : str . toString ( ) } ;
} catch ( e ) {
// eslint-disable-next-line no-console
@ -843,22 +843,6 @@ function get_node_range(source, node) {
return { start , end } ;
}
/ * *
* @ param { AST . OnDirective } last
* @ param { State } state
* /
function generate _event _name ( last , state ) {
const scope =
( last . expression && state . analysis . template . scopes . get ( last . expression ) ) || state . scope ;
let name = 'event' ;
if ( ! scope . get ( name ) ) return name ;
let i = 1 ;
while ( scope . get ( ` ${ name } ${ i } ` ) ) i += 1 ;
return ` ${ name } ${ i } ` ;
}
/ * *
* @ param { Identifier } node
* @ param { State } state