From f10c6ffd9a06a70dd00bc7435da35628c641385f Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Fri, 22 Dec 2023 21:30:48 -0500 Subject: [PATCH] maybe --- .../3-transform/client/visitors/template.js | 30 +++++++++---------- .../svelte/src/compiler/types/template.d.ts | 3 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js index 6ad1026cc7..4693ba9630 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js @@ -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 diff --git a/packages/svelte/src/compiler/types/template.d.ts b/packages/svelte/src/compiler/types/template.d.ts index 4646dc6d32..502f9c7d5f 100644 --- a/packages/svelte/src/compiler/types/template.d.ts +++ b/packages/svelte/src/compiler/types/template.d.ts @@ -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; }