@ -1,7 +1,8 @@
/** @import { Expression } from 'estree' */
/** @import { Attribute, ExpressionMetadata, ExpressionTag, OnDirective, SvelteNode } from '#compiler' */
/** @import { Attribute, ExpressionMetadata, ExpressionTag, SvelteNode } from '#compiler' */
/** @import { ComponentContext } from '../../types' */
import { is _capture _event , is _passive _event } from '../../../../../../utils.js' ;
import { is _capture _event } from '../../../../../../utils.js' ;
import { dev , locator } from '../../../../../state.js' ;
import * as b from '../../../../../utils/builders.js' ;
/ * *
@ -136,9 +137,47 @@ export function build_event_handler(node, metadata, context) {
}
// wrap the handler in a function, so the expression is re-evaluated for each event
return b . function (
null ,
[ b . rest ( b . id ( '$$args' ) ) ] ,
b . block ( [ b . stmt ( b . call ( b . member ( handler , b . id ( 'apply' ) , false , true ) , b . this , b . id ( '$$args' ) ) ) ] )
let call = b . call ( b . member ( handler , b . id ( 'apply' ) , false , true ) , b . this , b . id ( '$$args' ) ) ;
if ( dev ) {
const loc = locator ( /** @type {number} */ ( node . start ) ) ;
const remove _parens =
node . type === 'CallExpression' &&
node . arguments . length === 0 &&
node . callee . type === 'Identifier' ;
call = b . call (
'$.apply' ,
b . thunk ( handler ) ,
b . this ,
b . id ( '$$args' ) ,
b . id ( context . state . analysis . name ) ,
loc && b . array ( [ b . literal ( loc . line ) , b . literal ( loc . column ) ] ) ,
has _side _effects ( node ) && b . true ,
remove _parens && b . true
) ;
}
return b . function ( null , [ b . rest ( b . id ( '$$args' ) ) ] , b . block ( [ b . stmt ( call ) ] ) ) ;
}
/ * *
* @ param { Expression } node
* /
function has _side _effects ( node ) {
if (
node . type === 'CallExpression' ||
node . type === 'NewExpression' ||
node . type === 'AssignmentExpression' ||
node . type === 'UpdateExpression'
) {
return true ;
}
if ( node . type === 'SequenceExpression' ) {
return node . expressions . some ( has _side _effects ) ;
}
return false ;
}