Skip to content

Commit e9ecbcf

Browse files
committed
hoisted snippets belong in transform state, not analysis
1 parent 9a9226c commit e9ecbcf

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,6 @@ export function analyze_component(root, source, options) {
424424
reactive_statements: new Map(),
425425
binding_groups: new Map(),
426426
slot_names: new Map(),
427-
top_level_snippets: [],
428-
module_level_snippets: [],
429427
css: {
430428
ast: root.css,
431429
hash: root.css

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ export function client_component(analysis, options) {
163163
private_state: new Map(),
164164
transform: {},
165165
in_constructor: false,
166+
instance_level_snippets: [],
167+
module_level_snippets: [],
166168

167169
// these are set inside the `Fragment` visitor, and cannot be used until then
168170
before_init: /** @type {any} */ (null),
@@ -368,7 +370,7 @@ export function client_component(analysis, options) {
368370
...store_setup,
369371
...legacy_reactive_declarations,
370372
...group_binding_declarations,
371-
...analysis.top_level_snippets,
373+
...state.instance_level_snippets,
372374
.../** @type {ESTree.Statement[]} */ (instance.body),
373375
analysis.runes || !analysis.needs_context
374376
? b.empty
@@ -483,7 +485,7 @@ export function client_component(analysis, options) {
483485
}
484486
}
485487

486-
body = [...imports, ...analysis.module_level_snippets, ...body];
488+
body = [...imports, ...state.module_level_snippets, ...body];
487489

488490
const component = b.function_declaration(
489491
b.id(analysis.name),

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import type {
66
PrivateIdentifier,
77
Expression,
88
AssignmentExpression,
9-
UpdateExpression
9+
UpdateExpression,
10+
VariableDeclaration
1011
} from 'estree';
1112
import type { Namespace, SvelteNode, ValidatedCompileOptions } from '#compiler';
1213
import type { TransformState } from '../types.js';
@@ -85,6 +86,11 @@ export interface ComponentClientTransformState extends ClientTransformState {
8586

8687
/** The $: calls, which will be ordered in the end */
8788
readonly legacy_reactive_statements: Map<LabeledStatement, Statement>;
89+
90+
/** Snippets hoisted to the instance */
91+
readonly instance_level_snippets: VariableDeclaration[];
92+
/** Snippets hoisted to the module */
93+
readonly module_level_snippets: VariableDeclaration[];
8894
}
8995

9096
export interface StateField {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ export function SnippetBlock(node, context) {
8484
// Top-level snippets are hoisted so they can be referenced in the `<script>`
8585
if (context.path.length === 1 && context.path[0].type === 'Fragment') {
8686
if (node.metadata.can_hoist) {
87-
context.state.analysis.module_level_snippets.push(declaration);
87+
context.state.module_level_snippets.push(declaration);
8888
} else {
89-
context.state.analysis.top_level_snippets.push(declaration);
89+
context.state.instance_level_snippets.push(declaration);
9090
}
9191
} else {
9292
context.state.init.push(declaration);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ export interface ComponentAnalysis extends Analysis {
6262
/** If `true`, should append styles through JavaScript */
6363
inject_styles: boolean;
6464
reactive_statements: Map<LabeledStatement, ReactiveStatement>;
65-
top_level_snippets: VariableDeclaration[];
66-
module_level_snippets: VariableDeclaration[];
6765
/** Identifiers that make up the `bind:group` expression -> internal group binding name */
6866
binding_groups: Map<[key: string, bindings: Array<Binding | null>], Identifier>;
6967
slot_names: Map<string, AST.SlotElement>;

0 commit comments

Comments
 (0)