Skip to content

Commit 6cd7ef9

Browse files
committed
partial merge
1 parent 0530c2c commit 6cd7ef9

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed
Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { BlockStatement, Statement, Expression } from 'estree' */
1+
/** @import { BlockStatement, Statement, Expression, FunctionDeclaration, VariableDeclaration, ArrowFunctionExpression } from 'estree' */
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
import { dev } from '../../../../state.js';
@@ -34,65 +34,61 @@ export function SvelteBoundary(node, context) {
3434
const nodes = [];
3535

3636
/** @type {Statement[]} */
37-
const external_statements = [];
37+
const const_tags = [];
3838

3939
/** @type {Statement[]} */
40-
const internal_statements = [];
40+
const hoisted = [];
4141

42-
const snippets_visits = [];
42+
// const tags need to live inside the boundary, but might also be referenced in hoisted snippets.
43+
// to resolve this we cheat: we duplicate const tags inside snippets
44+
for (const child of node.fragment.nodes) {
45+
if (child.type === 'ConstTag') {
46+
context.visit(child, { ...context.state, init: const_tags });
47+
}
48+
}
4349

44-
// Capture the `failed` implicit snippet prop
4550
for (const child of node.fragment.nodes) {
46-
if (
47-
child.type === 'SnippetBlock' &&
48-
(child.expression.name === 'failed' || child.expression.name === 'pending')
49-
) {
50-
// we need to delay the visit of the snippets in case they access a ConstTag that is declared
51-
// after the snippets so that the visitor for the const tag can be updated
52-
snippets_visits.push(() => {
53-
/** @type {Statement[]} */
54-
const init = [];
55-
context.visit(child, { ...context.state, init });
56-
props.properties.push(b.prop('init', child.expression, child.expression));
57-
external_statements.push(...init);
58-
});
59-
} else if (child.type === 'ConstTag') {
51+
if (child.type === 'ConstTag') {
52+
continue;
53+
}
54+
55+
if (child.type === 'SnippetBlock') {
6056
/** @type {Statement[]} */
61-
const init = [];
62-
context.visit(child, { ...context.state, init });
63-
64-
if (dev) {
65-
// In dev we must separate the declarations from the code
66-
// that eagerly evaluate the expression...
67-
for (const statement of init) {
68-
if (statement.type === 'VariableDeclaration') {
69-
external_statements.push(statement);
70-
} else {
71-
internal_statements.push(statement);
72-
}
73-
}
74-
} else {
75-
external_statements.push(...init);
57+
const statements = [];
58+
59+
context.visit(child, { ...context.state, init: statements });
60+
61+
const snippet = /** @type {VariableDeclaration} */ (statements[0]);
62+
63+
const snippet_fn = dev
64+
? // @ts-expect-error we know this shape is correct
65+
snippet.declarations[0].init.arguments[1]
66+
: snippet.declarations[0].init;
67+
68+
snippet_fn.body.body.unshift(
69+
...const_tags.filter((node) => node.type === 'VariableDeclaration')
70+
);
71+
72+
hoisted.push(snippet);
73+
74+
if (['failed', 'pending'].includes(child.expression.name)) {
75+
props.properties.push(b.prop('init', child.expression, child.expression));
7676
}
77-
} else {
78-
nodes.push(child);
77+
78+
continue;
7979
}
80-
}
8180

82-
snippets_visits.forEach((visit) => visit());
81+
nodes.push(child);
82+
}
8383

8484
const block = /** @type {BlockStatement} */ (context.visit({ ...node.fragment, nodes }));
8585

86-
if (dev && internal_statements.length) {
87-
block.body.unshift(...internal_statements);
88-
}
86+
block.body.unshift(...const_tags);
8987

9088
const boundary = b.stmt(
9189
b.call('$.boundary', context.state.node, props, b.arrow([b.id('$$anchor')], block))
9290
);
9391

9492
context.state.template.push_comment();
95-
context.state.init.push(
96-
external_statements.length > 0 ? b.block([...external_statements, boundary]) : boundary
97-
);
93+
context.state.init.push(hoisted.length > 0 ? b.block([...hoisted, boundary]) : boundary);
9894
}

0 commit comments

Comments
 (0)