@ -1,4 +1,4 @@
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression } from 'estree' */
/** @import { BlockStatement } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types.js' */
import { dev } from '../../../../state.js' ;
@ -9,27 +9,21 @@ import * as b from '#compiler/builders';
* @ param { ComponentContext } context
* /
export function SnippetBlock ( node , context ) {
const body = /** @type {BlockStatement} */ ( context . visit ( node . body ) ) ;
let fn = b . function _declaration (
node . expression ,
[ b . id ( '$$payload' ) , ... node . parameters ] ,
/** @type {BlockStatement} */ ( context . visit ( node . body ) )
) ;
if ( dev ) {
body . body . unshift ( b . stmt ( b . call ( '$.validate_snippet_args' , b . id ( '$$payload' ) ) ) ) ;
}
// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
fn . _ _ _snippet = true ;
/** @type {ArrowFunctionExpression | CallExpression} */
let fn = b . arrow ( [ b . id ( '$$payload' ) , ... node . parameters ] , body ) ;
const statements = node . metadata . can _hoist ? context . state . hoisted : context . state . init ;
if ( dev ) {
fn = b . call ( '$.prevent_snippet_stringification' , fn ) ;
fn . body . body . unshift ( b . stmt ( b . call ( '$.validate_snippet_args' , b . id ( '$$payload' ) ) ) ) ;
statements . push ( b . stmt ( b . call ( '$.prevent_snippet_stringification' , fn . id ) ) ) ;
}
const declaration = b . declaration ( 'const' , [ b . declarator ( node . expression , fn ) ] ) ;
// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
fn . _ _ _snippet = true ;
if ( node . metadata . can _hoist ) {
context . state . hoisted . push ( declaration ) ;
} else {
context . state . init . push ( declaration ) ;
}
statements . push ( fn ) ;
}