diff --git a/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js b/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js index 0dee5b777b..c72549369e 100644 --- a/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js +++ b/packages/svelte/src/compiler/phases/3-transform/server/transform-server.js @@ -47,30 +47,28 @@ export const block_anchor = t_string(``); /** * @param {string} value - * @returns {import('./types').TemplateExpression} */ function t_string(value) { - return { type: 'expression', value: b.literal(sanitize_template_string(value)) }; + return t_expression(b.literal(sanitize_template_string(value))); } /** * @param {import('estree').Expression} value - * @returns {import('./types').TemplateExpression} */ function t_expression(value) { - return { type: 'expression', value }; + return value; } /** - * @param {import('estree').Statement} value - * @returns {import('./types').TemplateStatement} + * @param {import('estree').Node} node + * @returns {node is import('estree').Statement} */ -function t_statement(value) { - return { type: 'statement', value }; +function is_statement(node) { + return node.type.endsWith('Statement') || node.type.endsWith('Declaration'); } /** - * @param {import('./types').Template[]} template + * @param {Array} template * @param {import('estree').Identifier} out * @returns {import('estree').Statement[]} */ @@ -94,19 +92,20 @@ function serialize_template(template, out = b.id('out')) { for (let i = 0; i < template.length; i++) { const template_item = template[i]; - if (template_item.type === 'statement') { + + if (is_statement(template_item)) { if (quasis.length !== 0) { flush_payload(); } - statements.push(template_item.value); + statements.push(template_item); } else { let last = quasis.at(-1); if (!last) quasis.push((last = b.quasi('', false))); - if (template_item.value.type === 'Literal') { - last.value.raw += template_item.value.value; - } else if (template_item.type === 'expression') { - const value = template_item.value; + if (template_item.type === 'Literal') { + last.value.raw += template_item.value; + } else { + const value = template_item; if (value.type === 'TemplateLiteral') { last.value.raw += value.quasis[0].value.raw; quasis.push(...value.quasis.slice(1)); @@ -114,7 +113,7 @@ function serialize_template(template, out = b.id('out')) { continue; } expressions.push(value); - quasis.push(b.quasi('', i + 1 === template.length || template[i + 1].type === 'statement')); + quasis.push(b.quasi('', i + 1 === template.length || is_statement(template[i + 1]))); } } } @@ -1125,10 +1124,10 @@ function serialize_inline_component(node, component_name, context) { ) ); - context.state.template.push(t_statement(statement)); + context.state.template.push(statement); } else { context.state.template.push(block_open); - context.state.template.push(t_statement(statement)); + context.state.template.push(statement); context.state.template.push(block_close); } } @@ -1302,23 +1301,21 @@ const template_visitors = { }, DebugTag(node, { state, visit }) { state.template.push( - t_statement( - b.stmt( - b.call( - 'console.log', - b.object( - node.identifiers.map((identifier) => - b.prop( - 'init', - identifier, - /** @type {import('estree').Expression} */ (visit(identifier)) - ) + b.stmt( + b.call( + 'console.log', + b.object( + node.identifiers.map((identifier) => + b.prop( + 'init', + identifier, + /** @type {import('estree').Expression} */ (visit(identifier)) ) ) ) ) ), - t_statement(b.debugger) + b.debugger ); }, RenderTag(node, context) { @@ -1339,13 +1336,11 @@ const template_visitors = { }); state.template.push( - t_statement( - b.stmt( - (node.expression.type === 'CallExpression' ? b.call : b.maybe_call)( - snippet_function, - b.id('$$payload'), - ...snippet_args - ) + b.stmt( + (node.expression.type === 'CallExpression' ? b.call : b.maybe_call)( + snippet_function, + b.id('$$payload'), + ...snippet_args ) ) ); @@ -1394,15 +1389,13 @@ const template_visitors = { if (state.options.dev) { const location = /** @type {import('locate-character').Location} */ (locator(node.start)); state.template.push( - t_statement( - b.stmt( - b.call( - '$.push_element', - b.id('$$payload'), - b.literal(node.name), - b.literal(location.line), - b.literal(location.column) - ) + b.stmt( + b.call( + '$.push_element', + b.id('$$payload'), + b.literal(node.name), + b.literal(location.line), + b.literal(location.column) ) ) ); @@ -1415,7 +1408,7 @@ const template_visitors = { if (body.type !== 'Identifier') { id = b.id(state.scope.generate('$$body')); - state.template.push(t_statement(b.const(id, body))); + state.template.push(b.const(id, body)); } // if this is a `