remove create_block

pull/11908/head
Rich Harris 2 years ago
parent 903b8dd766
commit e97fc9f296

@ -238,64 +238,6 @@ function process_children(nodes, parent, { visit, state }) {
}
}
/**
* @param {import('#compiler').SvelteNode} parent
* @param {import('#compiler').Fragment} fragment
* @param {import('#compiler').SvelteNode[]} nodes
* @param {import('./types').ComponentContext} context
* @param {import('./types').Anchor} [anchor]
* @returns {import('estree').Statement[]}
*/
function create_block(parent, fragment, nodes, context, anchor) {
const namespace = infer_namespace(context.state.metadata.namespace, parent, nodes);
const { hoisted, trimmed } = clean_nodes(
parent,
nodes,
context.path,
namespace,
context.state,
context.state.preserve_whitespace,
context.state.options.preserveComments
);
if (hoisted.length === 0 && trimmed.length === 0 && !anchor) {
return [];
}
/** @type {import('./types').ComponentServerTransformState} */
const state = {
...context.state,
scope: context.state.scopes.get(fragment) ?? context.state.scope,
init: [],
template: [],
metadata: {
namespace
}
};
for (const node of hoisted) {
context.visit(node, state);
}
process_children(anchor ? [anchor, ...trimmed, anchor] : trimmed, parent, {
...context,
state
});
/** @type {import('estree').Statement[]} */
const body = [];
if (state.template.length > 0) {
body.push(...state.init);
body.push(...serialize_template(state.template));
} else {
body.push(...state.init);
}
return body;
}
/**
* @param {import('estree').VariableDeclarator} declarator
* @param {import('../../scope').Scope} scope
@ -1290,7 +1232,46 @@ const javascript_visitors_legacy = {
/** @type {import('./types').ComponentVisitors} */
const template_visitors = {
Fragment(node, context) {
const body = create_block(context.path.at(-1) ?? node, node, node.nodes, context);
const parent = context.path.at(-1) ?? node;
const namespace = infer_namespace(context.state.metadata.namespace, parent, node.nodes);
const { hoisted, trimmed } = clean_nodes(
parent,
node.nodes,
context.path,
namespace,
context.state,
context.state.preserve_whitespace,
context.state.options.preserveComments
);
if (hoisted.length === 0 && trimmed.length === 0) {
return b.block([]);
}
/** @type {import('./types').ComponentServerTransformState} */
const state = {
...context.state,
init: [],
template: [],
metadata: {
namespace
}
};
for (const node of hoisted) {
context.visit(node, state);
}
process_children(trimmed, parent, { ...context, state });
/** @type {import('estree').Statement[]} */
const body = [...state.init];
if (state.template.length > 0) {
body.push(...serialize_template(state.template));
}
return b.block(body);
},
HtmlTag(node, context) {
@ -2190,23 +2171,9 @@ export function server_component(analysis, options) {
scopes: analysis.template.scopes,
hoisted: [b.import_all('$', 'svelte/internal/server')],
legacy_reactive_statements: new Map(),
// these should be set by create_block - if they're called outside, it's a bug
get init() {
/** @type {any[]} */
const a = [];
a.push = () => {
throw new Error('init.push should not be called outside create_block');
};
return a;
},
get template() {
/** @type {any[]} */
const a = [];
a.push = () => {
throw new Error('template.push should not be called outside create_block');
};
return a;
},
// these are set inside the `Fragment` visitor, and cannot be used until then
init: /** @type {any} */ (null),
template: /** @type {any} */ (null),
metadata: {
namespace: options.namespace
},

Loading…
Cancel
Save