all tests passing

pull/16091/head
Rich Harris 4 months ago
parent 9b8439a92d
commit c4f528a7bf

@ -1,4 +1,4 @@
/** @import { BlockStatement, Statement, Expression } from 'estree' */ /** @import { BlockStatement, Statement, Expression, FunctionDeclaration, VariableDeclaration, ArrowFunctionExpression } from 'estree' */
/** @import { AST } from '#compiler' */ /** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types' */ /** @import { ComponentContext } from '../types' */
import { dev } from '../../../../state.js'; import { dev } from '../../../../state.js';
@ -34,62 +34,61 @@ export function SvelteBoundary(node, context) {
const nodes = []; const nodes = [];
/** @type {Statement[]} */ /** @type {Statement[]} */
const external_statements = []; const const_tags = [];
/** @type {Statement[]} */ /** @type {Statement[]} */
const internal_statements = []; const hoisted = [];
const snippets_visits = []; // const tags need to live inside the boundary, but might also be referenced in hoisted snippets.
// to resolve this we cheat: we duplicate const tags inside snippets
for (const child of node.fragment.nodes) {
if (child.type === 'ConstTag') {
context.visit(child, { ...context.state, init: const_tags });
}
}
// Capture the `failed` implicit snippet prop
for (const child of node.fragment.nodes) { for (const child of node.fragment.nodes) {
if (child.type === 'SnippetBlock' && child.expression.name === 'failed') { if (child.type === 'ConstTag') {
// we need to delay the visit of the snippets in case they access a ConstTag that is declared continue;
// after the snippets so that the visitor for the const tag can be updated }
snippets_visits.push(() => {
/** @type {Statement[]} */ if (child.type === 'SnippetBlock') {
const init = [];
context.visit(child, { ...context.state, init });
props.properties.push(b.prop('init', child.expression, child.expression));
external_statements.push(...init);
});
} else if (child.type === 'ConstTag') {
/** @type {Statement[]} */ /** @type {Statement[]} */
const init = []; const statements = [];
context.visit(child, { ...context.state, init });
context.visit(child, { ...context.state, init: statements });
if (dev) {
// In dev we must separate the declarations from the code const snippet = /** @type {VariableDeclaration} */ (statements[0]);
// that eagerly evaluate the expression...
for (const statement of init) { const snippet_fn = dev
if (statement.type === 'VariableDeclaration') { ? // @ts-expect-error we know this shape is correct
external_statements.push(statement); snippet.declarations[0].init.arguments[1]
} else { : snippet.declarations[0].init;
internal_statements.push(statement);
} snippet_fn.body.body.unshift(
} ...const_tags.filter((node) => node.type === 'VariableDeclaration')
} else { );
external_statements.push(...init);
hoisted.push(snippet);
if (child.expression.name === 'failed') {
props.properties.push(b.prop('init', child.expression, child.expression));
} }
} else {
nodes.push(child); continue;
} }
}
snippets_visits.forEach((visit) => visit()); nodes.push(child);
}
const block = /** @type {BlockStatement} */ (context.visit({ ...node.fragment, nodes })); const block = /** @type {BlockStatement} */ (context.visit({ ...node.fragment, nodes }));
if (dev && internal_statements.length) { block.body.unshift(...const_tags);
block.body.unshift(...internal_statements);
}
const boundary = b.stmt( const boundary = b.stmt(
b.call('$.boundary', context.state.node, props, b.arrow([b.id('$$anchor')], block)) b.call('$.boundary', context.state.node, props, b.arrow([b.id('$$anchor')], block))
); );
context.state.template.push_comment(); context.state.template.push_comment();
context.state.init.push( context.state.init.push(hoisted.length > 0 ? b.block([...hoisted, boundary]) : boundary);
external_statements.length > 0 ? b.block([...external_statements, boundary]) : boundary
);
} }

@ -81,7 +81,7 @@ export function adjust_error(error, effect) {
if (adjusted_errors.has(error)) return; if (adjusted_errors.has(error)) return;
adjusted_errors.add(error); adjusted_errors.add(error);
const component_stack = [effect.fn?.name ?? '<unknown>']; const component_stack = [effect.fn?.name || '<unknown>'];
const indent = is_firefox ? ' ' : '\t'; const indent = is_firefox ? ' ' : '\t';
var context = effect.ctx; var context = effect.ctx;

Loading…
Cancel
Save