mirror of https://github.com/sveltejs/svelte
commit
cfd8ef2768
@ -1,28 +1,35 @@
|
|||||||
/** @import { BlockStatement } from 'estree' */
|
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression } from 'estree' */
|
||||||
/** @import { AST } from '#compiler' */
|
/** @import { AST } from '#compiler' */
|
||||||
/** @import { ComponentContext } from '../types.js' */
|
/** @import { ComponentContext } from '../types.js' */
|
||||||
import { dev } from '../../../../state.js';
|
import { dev } from '../../../../state.js';
|
||||||
import * as b from '../../../../utils/builders.js';
|
import * as b from '#compiler/builders';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {AST.SnippetBlock} node
|
* @param {AST.SnippetBlock} node
|
||||||
* @param {ComponentContext} context
|
* @param {ComponentContext} context
|
||||||
*/
|
*/
|
||||||
export function SnippetBlock(node, context) {
|
export function SnippetBlock(node, context) {
|
||||||
const fn = b.function_declaration(
|
const body = /** @type {BlockStatement} */ (context.visit(node.body));
|
||||||
node.expression,
|
|
||||||
[b.id('$$payload'), ...node.parameters],
|
|
||||||
/** @type {BlockStatement} */ (context.visit(node.body))
|
|
||||||
);
|
|
||||||
if (dev) {
|
if (dev) {
|
||||||
fn.body.body.unshift(b.stmt(b.call('$.validate_snippet_args', b.id('$$payload'))));
|
body.body.unshift(b.stmt(b.call('$.validate_snippet_args', b.id('$$payload'))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {ArrowFunctionExpression | CallExpression} */
|
||||||
|
let fn = b.arrow([b.id('$$payload'), ...node.parameters], body);
|
||||||
|
|
||||||
|
if (dev) {
|
||||||
|
fn = b.call('$.prevent_snippet_stringification', fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
|
||||||
fn.___snippet = true;
|
fn.___snippet = true;
|
||||||
|
|
||||||
if (node.metadata.can_hoist) {
|
if (node.metadata.can_hoist) {
|
||||||
context.state.hoisted.push(fn);
|
context.state.hoisted.push(declaration);
|
||||||
} else {
|
} else {
|
||||||
context.state.init.push(fn);
|
context.state.init.push(declaration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue