mirror of https://github.com/sveltejs/svelte
commit
4b7130ea18
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: use function declaration for snippets in server output to avoid TDZ violation
|
@ -1,35 +1,29 @@
|
|||||||
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression } from 'estree' */
|
/** @import { BlockStatement } 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 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) {
|
// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
|
||||||
body.body.unshift(b.stmt(b.call('$.validate_snippet_args', b.id('$$payload'))));
|
fn.___snippet = true;
|
||||||
}
|
|
||||||
|
|
||||||
/** @type {ArrowFunctionExpression | CallExpression} */
|
const statements = node.metadata.can_hoist ? context.state.hoisted : context.state.init;
|
||||||
let fn = b.arrow([b.id('$$payload'), ...node.parameters], body);
|
|
||||||
|
|
||||||
if (dev) {
|
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)]);
|
statements.push(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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
export * from './types/index';
|
||||||
|
export * from './index';
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue