@ -117,7 +117,7 @@ export function migrate(source) {
. join ( ` , ${ props _separator } ` ) ;
if ( analysis . uses _rest _props ) {
props += ` , ${ props _separator } ... ${ state . rest _props _name } ` ;
props += ` ${ state . props . length > 0 ? ` , ${ props _separator } ` : '' } ... ${ state . rest _props _name } ` ;
}
}
@ -172,7 +172,7 @@ export function migrate(source) {
? ` \n ${ indent } ${ [ ... state . script _insertions ] . join ( indent ) } \n `
: '' ;
str . prepend (
` <script> \n ${ imports } ${ indent} ${ props _declaration } ${ script _insertions } \n </script> \n \n `
` <script> \n ${ imports } ${ script_insertions } ${ indent} ${ props _declaration } \n </script> \n \n `
) ;
added _legacy _import = true ;
}
@ -739,23 +739,19 @@ function handle_events(element, state) {
for ( let i = 0 ; i < nodes . length ; i += 1 ) {
const node = nodes [ i ] ;
const payload _name =
( node . expression ? . type === 'ArrowFunctionExpression' ||
node . expression ? . type === 'FunctionExpression' ) &&
node . expression . params [ 0 ] ? . type === 'Identifier'
? node . expression . params [ 0 ] . name
: generate _event _name ( node , state ) ;
const indent = get _indent ( state , node , element ) ;
const new _line _index = state . str . original . lastIndexOf ( '\n' , node . start ) ;
const needs _line _delete =
state . str . original . substring ( new _line _index , node . start ) . trim ( ) === '' && i !== 0 ;
// Check if prop already set, could happen when on:click on different elements
let local = state . props . find ( ( prop ) => prop . exported === exported ) ? . local ;
// always move once as the first modifier
const sorted _modifier = [ ... node . modifiers ] . sort ( ( a , b ) =>
a === 'once' ? 1 : b === 'once' ? - 1 : 0
) ;
let body = ` ${ state . legacy _imports _names . bubble } (' ${ node . name } ') ` ;
let body = local ? ? name ;
if ( node . expression ) {
body = state . str . original . substring (
@ -763,6 +759,19 @@ function handle_events(element, state) {
/** @type {number} */ ( node . expression . end )
) ;
} else {
const init = ` ${ state . legacy _imports _names . bubble } (' ${ node . name } ') ` ;
if ( ! local ) {
local = state . scope . generate ( ` on ${ node . name } ` ) ;
state . props . push ( {
local ,
exported ,
init ,
bindable : false ,
optional : true ,
type : '(event: any) => void'
} ) ;
}
body = local ;
state . legacy _imports . add ( 'createBubbler' ) ;
state . script _insertions . add (
` const ${ state . legacy _imports _names . bubble } = ${ state . legacy _imports _names . createBubbler } (); \n `
@ -791,11 +800,15 @@ function handle_events(element, state) {
handlers _body += ` ${ handler . needs _line _delete || nodes . length > 1 ? ` \n ${ handler . indent } ` : '' } ${ handler . handler } , ` ;
}
handlers _body = handlers _body . substring ( 0 , handlers _body . length - 1 ) ;
state . str . overwrite (
first _node . start ,
first _node . end ,
` ${ name } ={ ${ nodes . length > 1 ? ` ${ state . legacy _imports _names . handlers } ( ` : '' } ${ handlers _body } ${ nodes . length > 1 ? ')' : '' } } `
) ;
if ( handlers _body === name && local === name ) {
state . str . overwrite ( first _node . start , first _node . end , ` { ${ name } } ` ) ;
} else {
state . str . overwrite (
first _node . start ,
first _node . end ,
` ${ name } ={ ${ nodes . length > 1 ? ` ${ state . legacy _imports _names . handlers } ( ` : '' } ${ handlers _body } ${ nodes . length > 1 ? ')' : '' } } `
) ;
}
}
}
}