Skip to content

Commit 1c99218

Browse files
committed
only create BlockStatement when necessary
1 parent 46cbdd4 commit 1c99218

File tree

1 file changed

+16
-21
lines changed
  • packages/svelte/src/compiler/phases/3-transform/client/visitors

1 file changed

+16
-21
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,21 +2178,15 @@ export const template_visitors = {
21782178
state.options.preserveComments
21792179
);
21802180

2181-
/**
2182-
* @type {import("estree").Statement[]}
2183-
*/
2184-
const init = [];
2185-
/**
2186-
* @type {never[]}
2187-
*/
2188-
const update = [];
2189-
/**
2190-
* @type {never[]}
2191-
*/
2192-
const after_update = [];
2181+
/** Whether or not we need to wrap the children in `{...}` to avoid declaration conflicts */
2182+
const has_declaration = node.fragment.nodes.some((node) => node.type === 'SnippetBlock');
2183+
2184+
const child_state = has_declaration
2185+
? { ...state, init: [], update: [], after_update: [] }
2186+
: state;
21932187

21942188
for (const node of hoisted) {
2195-
context.visit(node, { ...state, init, update, after_update });
2189+
context.visit(node, child_state);
21962190
}
21972191

21982192
process_children(
@@ -2205,16 +2199,17 @@ export const template_visitors = {
22052199
: context.state.node
22062200
),
22072201
true,
2208-
{ ...context, state: { ...state, init, update, after_update } }
2202+
{ ...context, state: child_state }
22092203
);
22102204

2211-
if (init.length !== 0 || update.length !== 0 || after_update.length !== 0) {
2212-
const block = b.block([...init]);
2213-
if (update.length > 0) {
2214-
block.body.push(serialize_render_stmt(update));
2215-
}
2216-
block.body.push(...after_update);
2217-
context.state.init.push(block);
2205+
if (has_declaration) {
2206+
context.state.init.push(
2207+
b.block([
2208+
...child_state.init,
2209+
child_state.update.length > 0 ? serialize_render_stmt(child_state.update) : b.empty,
2210+
...child_state.after_update
2211+
])
2212+
);
22182213
}
22192214

22202215
if (has_direction_attribute) {

0 commit comments

Comments
 (0)