From c9d2b255804ede8bda0a865b40d0021f28e05702 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 12 May 2023 12:07:26 +0200 Subject: [PATCH] Convert src/compiler/compile/render_ssr/handlers/SlotTemplate.ts to JavaScript --- .../compile/render_ssr/handlers/Slot.js | 2 +- .../render_ssr/handlers/SlotTemplate.ts | 86 +++++++++---------- 2 files changed, 41 insertions(+), 47 deletions(-) diff --git a/src/compiler/compile/render_ssr/handlers/Slot.js b/src/compiler/compile/render_ssr/handlers/Slot.js index d09bc34f55..9a645eb58b 100644 --- a/src/compiler/compile/render_ssr/handlers/Slot.js +++ b/src/compiler/compile/render_ssr/handlers/Slot.js @@ -5,7 +5,7 @@ import { get_slot_scope } from './shared/get_slot_scope'; /** * @param {import('../../nodes/Slot.js').default} node * @param {import('../Renderer.js').default} renderer - * @param {RenderOptions & { + * @param {import('../Renderer.js').RenderOptions & { * slot_scopes: Map; * }} options */ diff --git a/src/compiler/compile/render_ssr/handlers/SlotTemplate.ts b/src/compiler/compile/render_ssr/handlers/SlotTemplate.ts index 333ac4c6bc..c61bb995a7 100644 --- a/src/compiler/compile/render_ssr/handlers/SlotTemplate.ts +++ b/src/compiler/compile/render_ssr/handlers/SlotTemplate.ts @@ -1,55 +1,49 @@ -import Renderer, { RenderOptions } from '../Renderer'; -import SlotTemplate from '../../nodes/SlotTemplate'; +import SlotTemplate from '../../nodes/SlotTemplate.js'; import remove_whitespace_children from './utils/remove_whitespace_children'; import { get_slot_scope } from './shared/get_slot_scope'; -import InlineComponent from '../../nodes/InlineComponent'; import { get_const_tags } from './shared/get_const_tags'; -export default function ( - node: SlotTemplate, - renderer: Renderer, - options: RenderOptions & { - slot_scopes: Map; - } -) { - const parent_inline_component = node.parent as InlineComponent; - const children = remove_whitespace_children( - node instanceof SlotTemplate ? node.children : [node], - node.next - ); +/** + * @param {import('../../nodes/SlotTemplate.js').default} node + * @param {import('../Renderer.js').default} renderer + * @param {RenderOptions & { + * slot_scopes: Map; + * }} options + */ +export default function (node, renderer, options) { + const parent_inline_component = /** @type {import('../../nodes/InlineComponent.js').default} */ (node.parent); + const children = remove_whitespace_children(node instanceof SlotTemplate ? node.children : [node], node.next); + renderer.push(); + renderer.render(children, options); + const lets = node.lets; + const seen = new Set(lets.map((l) => l.name.name)); + parent_inline_component.lets.forEach((l) => { + if (!seen.has(l.name.name)) + lets.push(l); + }); + const slot_fragment_content = renderer.pop(); + if (!is_empty_template_literal(slot_fragment_content)) { + if (options.slot_scopes.has(node.slot_template_name)) { + if (node.slot_template_name === 'default') { + throw new Error('Found elements without slot attribute when using slot="default"'); + } + throw new Error(`Duplicate slot name "${node.slot_template_name}" in <${parent_inline_component.name}>`); + } + options.slot_scopes.set(node.slot_template_name, { + input: get_slot_scope(node.lets), + output: slot_fragment_content, + statements: get_const_tags(node.const_tags) + }); + } +} - renderer.push(); - renderer.render(children, options); +/** @param {any} template_literal */ +function is_empty_template_literal(template_literal) { + return (template_literal.expressions.length === 0 && + template_literal.quasis.length === 1 && + template_literal.quasis[0].value.raw === ''); +} - const lets = node.lets; - const seen = new Set(lets.map((l) => l.name.name)); - parent_inline_component.lets.forEach((l) => { - if (!seen.has(l.name.name)) lets.push(l); - }); - const slot_fragment_content = renderer.pop(); - if (!is_empty_template_literal(slot_fragment_content)) { - if (options.slot_scopes.has(node.slot_template_name)) { - if (node.slot_template_name === 'default') { - throw new Error('Found elements without slot attribute when using slot="default"'); - } - throw new Error( - `Duplicate slot name "${node.slot_template_name}" in <${parent_inline_component.name}>` - ); - } - options.slot_scopes.set(node.slot_template_name, { - input: get_slot_scope(node.lets), - output: slot_fragment_content, - statements: get_const_tags(node.const_tags) - }); - } -} -function is_empty_template_literal(template_literal) { - return ( - template_literal.expressions.length === 0 && - template_literal.quasis.length === 1 && - template_literal.quasis[0].value.raw === '' - ); -}