@ -113,6 +113,37 @@ function find_closing_parenthesis(start, code) {
return end ;
}
/ * *
* @ param { MagicString } str
* @ param { unknown } start
* @ param { unknown } end
* /
function is _valid _range ( str , start , end ) {
return (
typeof start === 'number' &&
typeof end === 'number' &&
start >= 0 &&
start < end &&
end <= str . original . length
) ;
}
/ * *
* @ param { MagicString } str
* @ param { unknown } start
* @ param { unknown } end
* /
function remove _if _valid ( str , start , end ) {
if ( ! is _valid _range ( str , start , end ) ) return false ;
try {
str . remove ( /** @type {number} */ ( start ) , /** @type {number} */ ( end ) ) ;
return true ;
} catch {
return false ;
}
}
/ * *
* Does a best - effort migration of Svelte code towards using runes , event attributes and render tags .
* May throw an error if the code is too complex to migrate automatically .
@ -547,21 +578,22 @@ const instance_script = {
return ;
}
let count _removed = 0 ;
const specifiers _to _remove = /** @type {typeof node.specifiers} */ ( [ ] ) ;
for ( const specifier of node . specifiers ) {
if ( specifier . local . type !== 'Identifier' ) continue ;
const binding = state . scope . get ( specifier . local . name ) ;
if ( binding ? . kind === 'bindable_prop' ) {
state . str . remove (
/** @type {number} */ ( specifier . start ) ,
/** @type {number} */ ( specifier . end )
) ;
count _removed ++ ;
specifiers _to _remove . push ( specifier ) ;
}
}
if ( count _removed === node . specifiers . length ) {
state . str . remove ( /** @type {number} */ ( node . start ) , /** @type {number} */ ( node . end ) ) ;
if ( specifiers _to _remove . length === node . specifiers . length ) {
remove _if _valid ( state . str , node . start , node . end ) ;
} else {
for ( const specifier of specifiers _to _remove ) {
remove _if _valid ( state . str , specifier . start , specifier . end ) ;
}
}
} ,
VariableDeclaration ( node , { state , path , visit , next } ) {
@ -1597,8 +1629,14 @@ function extract_type_and_comment(declarator, state, path) {
const comment _start = /** @type {any} */ ( comment _node ) ? . start ;
const comment _end = /** @type {any} */ ( comment _node ) ? . end ;
let comment = comment _node && str . original . substring ( comment _start , comment _end ) ;
if ( comment _node ) {
const has _comment _range = is _valid _range ( str , comment _start , comment _end ) ;
const is _svelte _comment =
has _comment _range && str . original . startsWith ( '<!--' , /** @type {number} */ ( comment _start ) ) ;
let comment =
comment _node && has _comment _range && ! is _svelte _comment
? str . original . substring ( comment _start , comment _end )
: undefined ;
if ( has _comment _range && ! is _svelte _comment ) {
str . update ( comment _start , comment _end , '' ) ;
}
@ -1607,9 +1645,11 @@ function extract_type_and_comment(declarator, state, path) {
const trailing _comment _start = /** @type {any} */ ( trailing _comment _node ) ? . start ;
const trailing _comment _end = /** @type {any} */ ( trailing _comment _node ) ? . end ;
let trailing _comment =
trailing _comment _node && str . original . substring ( trailing _comment _start , trailing _comment _end ) ;
trailing _comment _node && is _valid _range ( str , trailing _comment _start , trailing _comment _end )
? str . original . substring ( trailing _comment _start , trailing _comment _end )
: undefined ;
if ( trailing _comment _node ) {
if ( is_valid _range ( str , trailing _comment _start , trailing _comment _end ) ) {
str . update ( trailing _comment _start , trailing _comment _end , '' ) ;
}