elliott/add-variadic-snippets
S. Elliott Johnson 2 years ago
parent d0c61a8596
commit f10c6ffd9a

@ -2445,21 +2445,21 @@ export const template_visitors = {
/** @type {import('estree').BlockStatement} */ /** @type {import('estree').BlockStatement} */
let body; let body;
if (node.context) { /** @type {import('estree').Statement[]} */
const id = node.context.type === 'Identifier' ? node.context : b.id('$$context'); const declarations = [];
args.push(id);
/** @type {import('estree').Statement[]} */ node.context.elements.forEach((element, i) => {
const declarations = []; if (!element) return;
const id = element.type === 'Identifier' ? element : b.id(`$$context${i}`);
args.push(id);
// some of this is duplicated with EachBlock — TODO dedupe? if (element.type === 'Identifier') {
if (node.context.type === 'Identifier') {
const binding = /** @type {import('#compiler').Binding} */ ( const binding = /** @type {import('#compiler').Binding} */ (
context.state.scope.get(id.name) context.state.scope.get(id.name)
); );
binding.expression = b.call(id); binding.expression = b.call(id);
} else { } else {
const paths = extract_paths(node.context); const paths = extract_paths(element);
for (const path of paths) { for (const path of paths) {
const name = /** @type {import('estree').Identifier} */ (path.node).name; const name = /** @type {import('estree').Identifier} */ (path.node).name;
@ -2471,7 +2471,7 @@ export const template_visitors = {
path.node, path.node,
b.thunk( b.thunk(
/** @type {import('estree').Expression} */ ( /** @type {import('estree').Expression} */ (
context.visit(path.expression?.(b.call('$$context'))) context.visit(path.expression?.(b.call(`$$context${i}`)))
) )
) )
) )
@ -2486,14 +2486,12 @@ export const template_visitors = {
binding.expression = b.call(name); binding.expression = b.call(name);
} }
} }
});
body = b.block([ body = b.block([
...declarations, ...declarations,
.../** @type {import('estree').BlockStatement} */ (context.visit(node.body)).body .../** @type {import('estree').BlockStatement} */ (context.visit(node.body)).body
]); ]);
} else {
body = /** @type {import('estree').BlockStatement} */ (context.visit(node.body));
}
const path = context.path; const path = context.path;
// If we're top-level, then we can create a function for the snippet so that it can be referenced // If we're top-level, then we can create a function for the snippet so that it can be referenced

@ -2,6 +2,7 @@ import type { Binding } from '#compiler';
import type { import type {
ArrayExpression, ArrayExpression,
ArrowFunctionExpression, ArrowFunctionExpression,
ArrayPattern,
VariableDeclaration, VariableDeclaration,
VariableDeclarator, VariableDeclarator,
Expression, Expression,
@ -413,7 +414,7 @@ export interface KeyBlock extends BaseNode {
export interface SnippetBlock extends BaseNode { export interface SnippetBlock extends BaseNode {
type: 'SnippetBlock'; type: 'SnippetBlock';
expression: Identifier; expression: Identifier;
context: null | Pattern; context: ArrayPattern;
body: Fragment; body: Fragment;
} }

Loading…
Cancel
Save