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} */
let body;
if (node.context) {
const id = node.context.type === 'Identifier' ? node.context : b.id('$$context');
args.push(id);
/** @type {import('estree').Statement[]} */
const declarations = [];
/** @type {import('estree').Statement[]} */
const declarations = [];
node.context.elements.forEach((element, i) => {
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 (node.context.type === 'Identifier') {
if (element.type === 'Identifier') {
const binding = /** @type {import('#compiler').Binding} */ (
context.state.scope.get(id.name)
);
binding.expression = b.call(id);
} else {
const paths = extract_paths(node.context);
const paths = extract_paths(element);
for (const path of paths) {
const name = /** @type {import('estree').Identifier} */ (path.node).name;
@ -2471,7 +2471,7 @@ export const template_visitors = {
path.node,
b.thunk(
/** @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);
}
}
});
body = b.block([
...declarations,
.../** @type {import('estree').BlockStatement} */ (context.visit(node.body)).body
]);
} else {
body = /** @type {import('estree').BlockStatement} */ (context.visit(node.body));
}
body = b.block([
...declarations,
.../** @type {import('estree').BlockStatement} */ (context.visit(node.body)).body
]);
const path = context.path;
// 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 {
ArrayExpression,
ArrowFunctionExpression,
ArrayPattern,
VariableDeclaration,
VariableDeclarator,
Expression,
@ -413,7 +414,7 @@ export interface KeyBlock extends BaseNode {
export interface SnippetBlock extends BaseNode {
type: 'SnippetBlock';
expression: Identifier;
context: null | Pattern;
context: ArrayPattern;
body: Fragment;
}

Loading…
Cancel
Save