|
| 1 | +/** @import { BlockStatement, Statement, Expression } from 'estree' */ |
| 2 | +/** @import { AST } from '#compiler' */ |
| 3 | +/** @import { ComponentContext } from '../types' */ |
| 4 | +import * as b from '../../../../utils/builders.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @param {AST.SvelteBoundary} node |
| 8 | + * @param {ComponentContext} context |
| 9 | + */ |
| 10 | +export function SvelteBoundary(node, context) { |
| 11 | + const props = b.object([]); |
| 12 | + |
| 13 | + for (const attribute of node.attributes) { |
| 14 | + if (attribute.type !== 'Attribute' || attribute.value === true) { |
| 15 | + // these can't exist, because they would have caused validation |
| 16 | + // to fail, but typescript doesn't know that |
| 17 | + continue; |
| 18 | + } |
| 19 | + |
| 20 | + const chunk = Array.isArray(attribute.value) |
| 21 | + ? /** @type {AST.ExpressionTag} */ (attribute.value[0]) |
| 22 | + : attribute.value; |
| 23 | + |
| 24 | + const expression = /** @type {Expression} */ (context.visit(chunk.expression, context.state)); |
| 25 | + |
| 26 | + if (attribute.metadata.expression.has_state) { |
| 27 | + props.properties.push(b.get(attribute.name, [b.return(expression)])); |
| 28 | + } else { |
| 29 | + props.properties.push(b.init(attribute.name, expression)); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const nodes = []; |
| 34 | + |
| 35 | + /** @type {Statement[]} */ |
| 36 | + const snippet_statements = []; |
| 37 | + |
| 38 | + // Capture the `failed` implicit snippet prop |
| 39 | + for (const child of node.fragment.nodes) { |
| 40 | + if (child.type === 'SnippetBlock' && child.expression.name === 'failed') { |
| 41 | + /** @type {Statement[]} */ |
| 42 | + const init = []; |
| 43 | + context.visit(child, { ...context.state, init }); |
| 44 | + props.properties.push(b.prop('init', child.expression, child.expression)); |
| 45 | + snippet_statements.push(...init); |
| 46 | + } else { |
| 47 | + nodes.push(child); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const block = /** @type {BlockStatement} */ (context.visit({ ...node.fragment, nodes })); |
| 52 | + |
| 53 | + const boundary = b.stmt( |
| 54 | + b.call('$.boundary', context.state.node, props, b.arrow([b.id('$$anchor')], block)) |
| 55 | + ); |
| 56 | + |
| 57 | + context.state.template.push('<!>'); |
| 58 | + context.state.init.push( |
| 59 | + snippet_statements.length > 0 ? b.block([...snippet_statements, boundary]) : boundary |
| 60 | + ); |
| 61 | +} |
0 commit comments