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' */
3
3
/** @import { ComponentServerTransformState, ComponentVisitors, ServerTransformState, Visitors } from './types.js' */
4
4
/** @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
7
5
import { walk } from 'zimmerframe' ;
8
6
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' ;
10
8
import * as b from '../../../utils/builders.js' ;
11
9
import { filename } from '../../../state.js' ;
12
10
import { render_stylesheet } from '../css/index.js' ;
@@ -15,6 +13,7 @@ import { CallExpression } from './visitors/javascript/CallExpression.js';
15
13
import { ClassBodyRunes } from './visitors/javascript/ClassBody.js' ;
16
14
import { ExpressionStatementRunes } from './visitors/javascript/ExpressionStatement.js' ;
17
15
import { Identifier } from './visitors/javascript/Identifier.js' ;
16
+ import { LabeledStatementLegacy } from './visitors/javascript/LabeledStatement.js' ;
18
17
import { MemberExpressionRunes } from './visitors/javascript/MemberExpression.js' ;
19
18
import { UpdateExpression } from './visitors/javascript/UpdateExpression.js' ;
20
19
import { PropertyDefinitionRunes } from './visitors/javascript/PropertyDefinition.js' ;
@@ -54,6 +53,7 @@ const global_visitors = {
54
53
55
54
/** @type {Visitors } */
56
55
const javascript_visitors_runes = {
56
+ ...global_visitors ,
57
57
ClassBody : ClassBodyRunes ,
58
58
PropertyDefinition : PropertyDefinitionRunes ,
59
59
VariableDeclaration : VariableDeclarationRunes ,
@@ -63,22 +63,9 @@ const javascript_visitors_runes = {
63
63
64
64
/** @type {Visitors } */
65
65
const javascript_visitors_legacy = {
66
+ ...global_visitors ,
66
67
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
82
69
} ;
83
70
84
71
/** @type {ComponentVisitors } */
@@ -137,7 +124,6 @@ export function server_component(analysis, options) {
137
124
// @ts -expect-error TODO: zimmerframe types
138
125
{
139
126
...set_scope ( analysis . module . scopes ) ,
140
- ...global_visitors ,
141
127
...( analysis . runes ? javascript_visitors_runes : javascript_visitors_legacy )
142
128
}
143
129
)
@@ -146,11 +132,10 @@ export function server_component(analysis, options) {
146
132
const instance = /** @type {Program } */ (
147
133
walk (
148
134
/** @type {SvelteNode } */ ( analysis . instance . ast ) ,
149
- { ... state , scope : analysis . instance . scope } ,
135
+ state ,
150
136
// @ts -expect-error TODO: zimmerframe types
151
137
{
152
138
...set_scope ( analysis . instance . scopes ) ,
153
- ...global_visitors ,
154
139
...( analysis . runes ? javascript_visitors_runes : javascript_visitors_legacy ) ,
155
140
ImportDeclaration ( node ) {
156
141
state . hoisted . push ( node ) ;
@@ -170,7 +155,7 @@ export function server_component(analysis, options) {
170
155
const template = /** @type {Program } */ (
171
156
walk (
172
157
/** @type {SvelteNode } */ ( analysis . template . ast ) ,
173
- { ... state , scope : analysis . template . scope } ,
158
+ state ,
174
159
// @ts -expect-error TODO: zimmerframe types
175
160
{
176
161
...set_scope ( analysis . template . scopes ) ,
@@ -216,17 +201,15 @@ export function server_component(analysis, options) {
216
201
// We can remove this once the legacy syntax is gone.
217
202
if ( analysis . uses_component_bindings ) {
218
203
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
223
206
) ;
207
+
224
208
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
229
211
) ;
212
+
230
213
template . body = [
231
214
...snippets ,
232
215
b . let ( '$$settled' , b . true ) ,
@@ -262,26 +245,27 @@ export function server_component(analysis, options) {
262
245
b . if ( b . id ( '$$store_subs' ) , b . stmt ( b . call ( '$.unsubscribe_stores' , b . id ( '$$store_subs' ) ) ) )
263
246
) ;
264
247
}
248
+
265
249
// Propagate values of bound props upwards if they're undefined in the parent and have a value.
266
250
// Don't do this as part of the props retrieval because people could eagerly mutate the prop in the instance script.
267
251
/** @type {Property[] } */
268
252
const props = [ ] ;
253
+
269
254
for ( const [ name , binding ] of analysis . instance . scope . declarations ) {
270
255
if ( binding . kind === 'bindable_prop' && ! name . startsWith ( '$$' ) ) {
271
256
props . push ( b . init ( binding . prop_alias ?? name , b . id ( name ) ) ) ;
272
257
}
273
258
}
259
+
274
260
for ( const { name, alias } of analysis . exports ) {
275
261
props . push ( b . init ( alias ?? name , b . id ( name ) ) ) ;
276
262
}
263
+
277
264
if ( props . length > 0 ) {
278
265
// This has no effect in runes mode other than throwing an error when someone passes
279
266
// undefined to a binding that has a default value.
280
267
template . body . push ( b . stmt ( b . call ( '$.bind_props' , b . id ( '$$props' ) , b . object ( props ) ) ) ) ;
281
268
}
282
- /** @type {Expression[] } */
283
- const push_args = [ ] ;
284
- if ( options . dev ) push_args . push ( b . id ( analysis . name ) ) ;
285
269
286
270
const component_block = b . block ( [
287
271
.../** @type {Statement[] } */ ( instance . body ) ,
@@ -291,7 +275,7 @@ export function server_component(analysis, options) {
291
275
let should_inject_context = analysis . needs_context || options . dev ;
292
276
293
277
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 ) ) ) ) ;
295
279
component_block . body . push ( b . stmt ( b . call ( '$.pop' ) ) ) ;
296
280
}
297
281
@@ -348,6 +332,7 @@ export function server_component(analysis, options) {
348
332
should_inject_props ? [ b . id ( '$$payload' ) , b . id ( '$$props' ) ] : [ b . id ( '$$payload' ) ] ,
349
333
component_block
350
334
) ;
335
+
351
336
if ( options . compatibility . componentApi === 4 ) {
352
337
body . unshift ( b . imports ( [ [ 'render' , '$$_render' ] ] , 'svelte/server' ) ) ;
353
338
body . push (
@@ -444,7 +429,6 @@ export function server_module(analysis, options) {
444
429
const module = /** @type {Program } */ (
445
430
walk ( /** @type {SvelteNode } */ ( analysis . module . ast ) , state , {
446
431
...set_scope ( analysis . module . scopes ) ,
447
- ...global_visitors ,
448
432
...javascript_visitors_runes
449
433
} )
450
434
) ;
0 commit comments