Skip to content

Commit 2e62d0b

Browse files
committed
more
1 parent 2bee4cb commit 2e62d0b

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

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

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/** @import { Expression, ExpressionStatement, MethodDefinition, Pattern, Program, Property, PropertyDefinition, Statement, VariableDeclarator } from 'estree' */
2-
/** @import { Binding, Namespace, SvelteNode, ValidatedCompileOptions, ValidatedModuleCompileOptions } from '#compiler' */
1+
/** @import { Program, Property, Statement, VariableDeclarator } from 'estree' */
2+
/** @import { SvelteNode, ValidatedCompileOptions, ValidatedModuleCompileOptions } from '#compiler' */
33
/** @import { ComponentServerTransformState, ComponentVisitors, ServerTransformState, Visitors } from './types.js' */
44
/** @import { Analysis, ComponentAnalysis } from '../../types.js' */
5-
/** @import { Scope } from '../../scope.js' */
6-
/** @import { StateField } from '../../3-transform/client/types.js' */ // TODO move this type
75
import { walk } from 'zimmerframe';
86
import { set_scope } from '../../scope.js';
9-
import { extract_identifiers, extract_paths, is_expression_async } from '../../../utils/ast.js';
7+
import { extract_identifiers } from '../../../utils/ast.js';
108
import * as b from '../../../utils/builders.js';
119
import { filename } from '../../../state.js';
1210
import { render_stylesheet } from '../css/index.js';
@@ -15,6 +13,7 @@ import { CallExpression } from './visitors/javascript/CallExpression.js';
1513
import { ClassBodyRunes } from './visitors/javascript/ClassBody.js';
1614
import { ExpressionStatementRunes } from './visitors/javascript/ExpressionStatement.js';
1715
import { Identifier } from './visitors/javascript/Identifier.js';
16+
import { LabeledStatementLegacy } from './visitors/javascript/LabeledStatement.js';
1817
import { MemberExpressionRunes } from './visitors/javascript/MemberExpression.js';
1918
import { UpdateExpression } from './visitors/javascript/UpdateExpression.js';
2019
import { PropertyDefinitionRunes } from './visitors/javascript/PropertyDefinition.js';
@@ -54,6 +53,7 @@ const global_visitors = {
5453

5554
/** @type {Visitors} */
5655
const javascript_visitors_runes = {
56+
...global_visitors,
5757
ClassBody: ClassBodyRunes,
5858
PropertyDefinition: PropertyDefinitionRunes,
5959
VariableDeclaration: VariableDeclarationRunes,
@@ -63,22 +63,9 @@ const javascript_visitors_runes = {
6363

6464
/** @type {Visitors} */
6565
const javascript_visitors_legacy = {
66+
...global_visitors,
6667
VariableDeclaration: VariableDeclarationLegacy,
67-
LabeledStatement(node, context) {
68-
if (context.path.length > 1) return;
69-
if (node.label.name !== '$') return;
70-
71-
// TODO bail out if we're in module context
72-
73-
// these statements will be topologically ordered later
74-
context.state.legacy_reactive_statements.set(
75-
node,
76-
// people could do "break $" inside, so we need to keep the label
77-
b.labeled('$', /** @type {ExpressionStatement} */ (context.visit(node.body)))
78-
);
79-
80-
return b.empty;
81-
}
68+
LabeledStatement: LabeledStatementLegacy
8269
};
8370

8471
/** @type {ComponentVisitors} */
@@ -137,7 +124,6 @@ export function server_component(analysis, options) {
137124
// @ts-expect-error TODO: zimmerframe types
138125
{
139126
...set_scope(analysis.module.scopes),
140-
...global_visitors,
141127
...(analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy)
142128
}
143129
)
@@ -146,11 +132,10 @@ export function server_component(analysis, options) {
146132
const instance = /** @type {Program} */ (
147133
walk(
148134
/** @type {SvelteNode} */ (analysis.instance.ast),
149-
{ ...state, scope: analysis.instance.scope },
135+
state,
150136
// @ts-expect-error TODO: zimmerframe types
151137
{
152138
...set_scope(analysis.instance.scopes),
153-
...global_visitors,
154139
...(analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy),
155140
ImportDeclaration(node) {
156141
state.hoisted.push(node);
@@ -170,7 +155,7 @@ export function server_component(analysis, options) {
170155
const template = /** @type {Program} */ (
171156
walk(
172157
/** @type {SvelteNode} */ (analysis.template.ast),
173-
{ ...state, scope: analysis.template.scope },
158+
state,
174159
// @ts-expect-error TODO: zimmerframe types
175160
{
176161
...set_scope(analysis.template.scopes),
@@ -216,17 +201,15 @@ export function server_component(analysis, options) {
216201
// We can remove this once the legacy syntax is gone.
217202
if (analysis.uses_component_bindings) {
218203
const snippets = template.body.filter(
219-
(node) =>
220-
node.type === 'FunctionDeclaration' &&
221-
// @ts-expect-error
222-
node.___snippet
204+
// @ts-expect-error
205+
(node) => node.type === 'FunctionDeclaration' && node.___snippet
223206
);
207+
224208
const rest = template.body.filter(
225-
(node) =>
226-
node.type !== 'FunctionDeclaration' ||
227-
// @ts-expect-error
228-
!node.___snippet
209+
// @ts-expect-error
210+
(node) => node.type !== 'FunctionDeclaration' || !node.___snippet
229211
);
212+
230213
template.body = [
231214
...snippets,
232215
b.let('$$settled', b.true),
@@ -262,26 +245,27 @@ export function server_component(analysis, options) {
262245
b.if(b.id('$$store_subs'), b.stmt(b.call('$.unsubscribe_stores', b.id('$$store_subs'))))
263246
);
264247
}
248+
265249
// Propagate values of bound props upwards if they're undefined in the parent and have a value.
266250
// Don't do this as part of the props retrieval because people could eagerly mutate the prop in the instance script.
267251
/** @type {Property[]} */
268252
const props = [];
253+
269254
for (const [name, binding] of analysis.instance.scope.declarations) {
270255
if (binding.kind === 'bindable_prop' && !name.startsWith('$$')) {
271256
props.push(b.init(binding.prop_alias ?? name, b.id(name)));
272257
}
273258
}
259+
274260
for (const { name, alias } of analysis.exports) {
275261
props.push(b.init(alias ?? name, b.id(name)));
276262
}
263+
277264
if (props.length > 0) {
278265
// This has no effect in runes mode other than throwing an error when someone passes
279266
// undefined to a binding that has a default value.
280267
template.body.push(b.stmt(b.call('$.bind_props', b.id('$$props'), b.object(props))));
281268
}
282-
/** @type {Expression[]} */
283-
const push_args = [];
284-
if (options.dev) push_args.push(b.id(analysis.name));
285269

286270
const component_block = b.block([
287271
.../** @type {Statement[]} */ (instance.body),
@@ -291,7 +275,7 @@ export function server_component(analysis, options) {
291275
let should_inject_context = analysis.needs_context || options.dev;
292276

293277
if (should_inject_context) {
294-
component_block.body.unshift(b.stmt(b.call('$.push', ...push_args)));
278+
component_block.body.unshift(b.stmt(b.call('$.push', options.dev && b.id(analysis.name))));
295279
component_block.body.push(b.stmt(b.call('$.pop')));
296280
}
297281

@@ -348,6 +332,7 @@ export function server_component(analysis, options) {
348332
should_inject_props ? [b.id('$$payload'), b.id('$$props')] : [b.id('$$payload')],
349333
component_block
350334
);
335+
351336
if (options.compatibility.componentApi === 4) {
352337
body.unshift(b.imports([['render', '$$_render']], 'svelte/server'));
353338
body.push(
@@ -444,7 +429,6 @@ export function server_module(analysis, options) {
444429
const module = /** @type {Program} */ (
445430
walk(/** @type {SvelteNode} */ (analysis.module.ast), state, {
446431
...set_scope(analysis.module.scopes),
447-
...global_visitors,
448432
...javascript_visitors_runes
449433
})
450434
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** @import { ExpressionStatement, LabeledStatement } from 'estree' */
2+
/** @import { Context } from '../../types' */
3+
import * as b from '../../../../../utils/builders.js';
4+
5+
/**
6+
* @param {LabeledStatement} node
7+
* @param {Context} context
8+
*/
9+
export function LabeledStatementLegacy(node, context) {
10+
if (context.path.length > 1) return;
11+
if (node.label.name !== '$') return;
12+
13+
// TODO bail out if we're in module context
14+
15+
// these statements will be topologically ordered later
16+
context.state.legacy_reactive_statements.set(
17+
node,
18+
// people could do "break $" inside, so we need to keep the label
19+
b.labeled('$', /** @type {ExpressionStatement} */ (context.visit(node.body)))
20+
);
21+
22+
return b.empty;
23+
}

0 commit comments

Comments
 (0)