Skip to content

Commit 58052bf

Browse files
committed
simplify
1 parent f1bbe72 commit 58052bf

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

packages/svelte/src/compiler/phases/2-analyze/visitors/SnippetBlock.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function SnippetBlock(node, context) {
2727
const can_hoist =
2828
context.path.length === 1 &&
2929
context.path[0].type === 'Fragment' &&
30-
can_hoist_snippet(node, local_scope, context.state.scopes);
30+
can_hoist_snippet(local_scope, context.state.scopes);
3131
const undefined_exports = context.state.analysis.undefined_exports;
3232
const name = node.expression.name;
3333

@@ -78,15 +78,14 @@ export function SnippetBlock(node, context) {
7878
}
7979

8080
/**
81-
* @param {AST.SnippetBlock} node
8281
* @param {Map<SvelteNode, Scope>} scopes
8382
* @param {Scope} scope
8483
*/
85-
function can_hoist_snippet(node, scope, scopes, visited = new Set()) {
86-
ref_loop: for (const [reference] of scope.references) {
84+
function can_hoist_snippet(scope, scopes, visited = new Set()) {
85+
for (const [reference] of scope.references) {
8786
const binding = scope.get(reference);
8887

89-
if (!binding || binding.node === node.expression || binding.scope.function_depth === 0) {
88+
if (!binding || binding.scope.function_depth === 0) {
9089
continue;
9190
}
9291

@@ -95,21 +94,12 @@ function can_hoist_snippet(node, scope, scopes, visited = new Set()) {
9594
continue;
9695
}
9796

98-
// Recursively check if another snippet can be hoisted
99-
if (binding.kind === 'normal') {
100-
for (const ref of binding.references) {
101-
const parent = ref.path.at(-1);
102-
if (ref.node === binding.node && parent?.type === 'SnippetBlock') {
103-
const ref_scope = scopes.get(parent);
104-
if (visited.has(ref)) {
105-
break;
106-
}
107-
visited.add(ref);
108-
if (ref_scope && can_hoist_snippet(parent, ref_scope, scopes, visited)) {
109-
continue ref_loop;
110-
}
111-
break;
112-
}
97+
if (binding.initial?.type === 'SnippetBlock') {
98+
if (visited.has(binding)) continue;
99+
visited.add(binding);
100+
101+
if (can_hoist_snippet(binding.scope, scopes, visited)) {
102+
continue;
113103
}
114104
}
115105

packages/svelte/src/compiler/phases/scope.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class Scope {
7575
* @param {Identifier} node
7676
* @param {Binding['kind']} kind
7777
* @param {DeclarationKind} declaration_kind
78-
* @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | AST.EachBlock} initial
78+
* @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | AST.EachBlock | AST.SnippetBlock} initial
7979
* @returns {Binding}
8080
*/
8181
declare(node, kind, declaration_kind, initial = null) {
@@ -632,7 +632,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
632632
if (is_top_level) {
633633
scope = /** @type {Scope} */ (parent);
634634
}
635-
scope.declare(node.expression, 'normal', 'function', node.expression);
635+
scope.declare(node.expression, 'normal', 'function', node);
636636

637637
const child_scope = state.scope.child();
638638
scopes.set(node, child_scope);

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ export interface Binding {
292292
| FunctionDeclaration
293293
| ClassDeclaration
294294
| ImportDeclaration
295-
| AST.EachBlock;
295+
| AST.EachBlock
296+
| AST.SnippetBlock;
296297
is_called: boolean;
297298
references: { node: Identifier; path: SvelteNode[] }[];
298299
mutated: boolean;

0 commit comments

Comments
 (0)