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
75import { walk } from 'zimmerframe' ;
86import { 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' ;
108import * as b from '../../../utils/builders.js' ;
119import { filename } from '../../../state.js' ;
1210import { render_stylesheet } from '../css/index.js' ;
@@ -15,6 +13,7 @@ import { CallExpression } from './visitors/javascript/CallExpression.js';
1513import { ClassBodyRunes } from './visitors/javascript/ClassBody.js' ;
1614import { ExpressionStatementRunes } from './visitors/javascript/ExpressionStatement.js' ;
1715import { Identifier } from './visitors/javascript/Identifier.js' ;
16+ import { LabeledStatementLegacy } from './visitors/javascript/LabeledStatement.js' ;
1817import { MemberExpressionRunes } from './visitors/javascript/MemberExpression.js' ;
1918import { UpdateExpression } from './visitors/javascript/UpdateExpression.js' ;
2019import { PropertyDefinitionRunes } from './visitors/javascript/PropertyDefinition.js' ;
@@ -54,6 +53,7 @@ const global_visitors = {
5453
5554/** @type {Visitors } */
5655const 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 } */
6565const 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 ) ;
0 commit comments