Skip to content

Commit 4bfe67f

Browse files
committed
refactor
1 parent bc6f857 commit 4bfe67f

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

packages/svelte/src/compiler/phases/1-parse/read/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function read_script(parser, start, attributes) {
3333
/** @type {Program} */
3434
let ast;
3535

36+
debugger;
3637
try {
3738
ast = acorn.parse(source, parser.ts, true);
3839
} catch (err) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/** @import { Context } from '../types' */
33
import { validate_block_not_empty, validate_opening_tag } from './shared/utils.js';
44
import * as e from '../../../errors.js';
5+
import { can_hoist_snippet } from '../../3-transform/utils.js';
56

67
/**
78
* @param {AST.SnippetBlock} node
@@ -22,6 +23,16 @@ export function SnippetBlock(node, context) {
2223

2324
context.next({ ...context.state, parent_element: null });
2425

26+
const local_scope = context.state.scope;
27+
const can_hoist =
28+
context.path.length === 1 &&
29+
context.path[0].type === 'Fragment' &&
30+
can_hoist_snippet(node, local_scope, context.state.scopes);
31+
32+
node.metadata = {
33+
can_hoist
34+
};
35+
2536
const { path } = context;
2637
const parent = path.at(-2);
2738
if (!parent) return;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import { dev } from '../../../../state.js';
55
import { extract_paths } from '../../../../utils/ast.js';
66
import * as b from '../../../../utils/builders.js';
7-
import { can_hoist_snippet } from '../../utils.js';
87
import { get_value } from './shared/declarations.js';
98

109
/**
@@ -81,12 +80,10 @@ export function SnippetBlock(node, context) {
8180
}
8281

8382
const declaration = b.const(node.expression, snippet);
84-
const local_scope = context.state.scope;
85-
const can_hoist = can_hoist_snippet(node, local_scope, context.state.scopes);
8683

8784
// Top-level snippets are hoisted so they can be referenced in the `<script>`
8885
if (context.path.length === 1 && context.path[0].type === 'Fragment') {
89-
if (can_hoist) {
86+
if (node.metadata.can_hoist) {
9087
context.state.analysis.module_level_snippets.push(declaration);
9188
} else {
9289
context.state.analysis.top_level_snippets.push(declaration);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext, } from '../types.js' */
44
import * as b from '../../../../utils/builders.js';
5-
import { can_hoist_snippet } from '../../utils.js';
65

76
/**
87
* @param {AST.SnippetBlock} node
@@ -18,9 +17,7 @@ export function SnippetBlock(node, context) {
1817
// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
1918
fn.___snippet = true;
2019

21-
const can_hoist = can_hoist_snippet(node, context.state.scope, context.state.scopes);
22-
23-
if (context.path.length === 1 && context.path[0].type === 'Fragment' && can_hoist) {
20+
if (node.metadata.can_hoist) {
2421
context.state.hoisted.push(fn);
2522
} else {
2623
context.state.init.push(fn);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ export namespace AST {
438438
expression: Identifier;
439439
parameters: Pattern[];
440440
body: Fragment;
441+
metadata: {
442+
can_hoist: boolean;
443+
};
441444
}
442445

443446
export interface Attribute extends BaseNode {

0 commit comments

Comments
 (0)