|
1 | 1 | /** @import { BlockStatement, Expression, Pattern, Statement } from 'estree' */
|
2 | 2 | /** @import { AST } from '#compiler' */
|
3 |
| -/** @import { ComponentContext } from '../types' */ |
| 3 | +/** @import { ComponentClientTransformState, ComponentContext } from '../types' */ |
| 4 | +import { extract_identifiers } from '../../../../utils/ast.js'; |
4 | 5 | import * as b from '../../../../utils/builders.js';
|
5 |
| -import { create_derived_block_argument } from '../utils.js'; |
| 6 | +import { create_derived } from '../utils.js'; |
| 7 | +import { get_value } from './shared/declarations.js'; |
6 | 8 |
|
7 | 9 | /**
|
8 | 10 | * @param {AST.AwaitBlock} node
|
@@ -65,3 +67,38 @@ export function AwaitBlock(node, context) {
|
65 | 67 | )
|
66 | 68 | );
|
67 | 69 | }
|
| 70 | + |
| 71 | +/** |
| 72 | + * @param {Pattern} node |
| 73 | + * @param {import('zimmerframe').Context<AST.SvelteNode, ComponentClientTransformState>} context |
| 74 | + * @returns {{ id: Pattern, declarations: null | Statement[] }} |
| 75 | + */ |
| 76 | +function create_derived_block_argument(node, context) { |
| 77 | + if (node.type === 'Identifier') { |
| 78 | + context.state.transform[node.name] = { read: get_value }; |
| 79 | + return { id: node, declarations: null }; |
| 80 | + } |
| 81 | + |
| 82 | + const pattern = /** @type {Pattern} */ (context.visit(node)); |
| 83 | + const identifiers = extract_identifiers(node); |
| 84 | + |
| 85 | + const id = b.id('$$source'); |
| 86 | + const value = b.id('$$value'); |
| 87 | + |
| 88 | + const block = b.block([ |
| 89 | + b.var(pattern, b.call('$.get', id)), |
| 90 | + b.return(b.object(identifiers.map((identifier) => b.prop('init', identifier, identifier)))) |
| 91 | + ]); |
| 92 | + |
| 93 | + const declarations = [b.var(value, create_derived(context.state, b.thunk(block)))]; |
| 94 | + |
| 95 | + for (const id of identifiers) { |
| 96 | + context.state.transform[id.name] = { read: get_value }; |
| 97 | + |
| 98 | + declarations.push( |
| 99 | + b.var(id, create_derived(context.state, b.thunk(b.member(b.call('$.get', value), id)))) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + return { id, declarations }; |
| 104 | +} |
0 commit comments