1
1
/** @import { BlockStatement, Identifier, Statement, Expression, MaybeNamedFunctionDeclaration, Pattern } from 'estree' */
2
2
/** @import { AST } from '#compiler' */
3
3
/** @import { ComponentContext } from '../types' */
4
- import { attr } from 'svelte/internal/client' ;
5
- import { BLOCK_CLOSE , BLOCK_OPEN } from '../../../../../internal/server/hydration.js' ;
6
4
import * as b from '../../../../utils/builders.js' ;
7
5
import { extract_identifiers } from '../../../../utils/ast.js' ;
8
6
@@ -20,8 +18,6 @@ export function SvelteBoundary(node, context) {
20
18
/** @type {AST.ConstTag[] } */
21
19
let const_tags = [ ] ;
22
20
23
- /** @type {Statement[] } */
24
- const init_body = [ ] ;
25
21
const nodes = [ ] ;
26
22
27
23
const payload = b . id ( '$$payload' ) ; // correct ?
@@ -61,60 +57,34 @@ export function SvelteBoundary(node, context) {
61
57
}
62
58
}
63
59
64
- if ( snippet && has_const_referenced ( context , snippet , const_tags ) ) {
65
- const const_values = b . id ( context . state . scope . generate ( 'const_values' ) ) ;
66
- statements . push ( b . declaration ( 'const' , [ b . declarator ( const_values , b . object ( [ ] ) ) ] ) ) ;
67
- for ( const tag of const_tags ) {
68
- const identifiers = extract_identifiers ( tag . declaration . declarations [ 0 ] . id ) ;
69
-
70
- statements . push (
71
- b . declaration (
72
- 'let' ,
73
- identifiers . map ( ( id ) => b . declarator ( id ) )
74
- )
75
- ) ;
76
- for ( const id of identifiers ) {
77
- statements . push ( b . stmt ( b . call ( 'console.log' , id ) ) ) ;
78
- }
79
-
80
- context . visit ( tag , { ...context . state , init : init_body } ) ;
81
- for ( const id of identifiers ) {
82
- init_body . push ( b . stmt ( b . assignment ( '=' , const_values , id ) ) ) ;
83
- init_body . push ( b . stmt ( b . call ( 'console.log' , const_values ) ) ) ;
84
- }
85
-
86
- // statements.push(b.declaration('const', [b.declarator(tmp, b.arrow([], b.block(init)))]));
87
-
88
- // nodes.unshift(b.declaration('let', [b.declarator(b.array_pattern(identifiers), tmp)]));
89
- // // b.declaration('let', [b.declarator(b.array_pattern(identifiers), tmp)]);
60
+ let max_referenced_const_tag = - 1 ;
90
61
91
- // const c = server_const(() => {
92
- // const { t, x } = { x: name, y: 'x' };
93
- // if (true) {
94
- // throw new Error('badaboum');
95
- // }
96
- // return { t, x };
97
- // });
62
+ if ( snippet ) {
63
+ const references = context . state . scopes . get ( snippet ) ?. references ;
64
+ if ( references != null && references . size ) {
65
+ const keys = new Set ( references . keys ( ) ) ;
98
66
99
- ///** @type {Statement[] } */
100
- //const init = [];
101
- //context.visit(tag, { ...context.state, init });
102
- //statements.push(...init);
67
+ const_tags . forEach ( ( tag , index ) => {
68
+ if ( has_reference ( keys , tag ) ) {
69
+ max_referenced_const_tag = index + 1 ;
70
+ }
71
+ } ) ;
103
72
}
104
- } else if ( const_tags . length ) {
105
- nodes . unshift ( ...const_tags ) ;
106
73
}
107
74
108
- if ( init_body . length ) {
109
- //nodes.unshift(...init_body);
75
+ if ( max_referenced_const_tag < 0 ) {
76
+ nodes . unshift ( ...const_tags ) ;
77
+ } else if ( max_referenced_const_tag === const_tags . length ) {
78
+ const_tags . forEach ( ( tag ) => context . visit ( tag , { ...context . state , init : statements } ) ) ;
79
+ } else {
80
+ const_tags
81
+ . slice ( 0 , max_referenced_const_tag )
82
+ . forEach ( ( tag ) => context . visit ( tag , { ...context . state , init : statements } ) ) ;
83
+ nodes . unshift ( ...const_tags . slice ( max_referenced_const_tag ) ) ;
110
84
}
111
85
112
86
const body_block = /** @type {BlockStatement } */ ( context . visit ( { ...node . fragment , nodes } ) ) ;
113
87
114
- if ( init_body . length ) {
115
- body_block . body . unshift ( ...init_body ) ;
116
- }
117
-
118
88
const body = b . arrow ( [ b . id ( '$$payload' ) ] , body_block ) ;
119
89
120
90
statements . push ( b . stmt ( b . call ( '$.boundary' , payload , body , failed ) ) ) ;
@@ -127,38 +97,16 @@ export function SvelteBoundary(node, context) {
127
97
}
128
98
129
99
/**
130
- *
131
- * @param {ComponentContext } context
132
- * @param {AST.SnippetBlock } snippet
133
- * @param {AST.ConstTag[] } const_tags
100
+ * @param {Set<string> } keys
101
+ * @param {AST.ConstTag } tag
134
102
*/
135
- function has_const_referenced ( context , snippet , const_tags ) {
136
- if ( const_tags . length === 0 ) {
137
- return false ;
138
- }
139
-
140
- const references = context . state . scopes . get ( snippet ) ?. references ;
141
- if ( references == null || references . size === 0 ) {
142
- return false ;
143
- }
144
-
145
- const identifiers = new Set ( ) ;
146
- for ( const tag of const_tags ) {
147
- for ( const declaration of tag . declaration . declarations ) {
148
- for ( const id of extract_identifiers ( declaration . id ) ) {
149
- identifiers . add ( id . name ) ;
103
+ function has_reference ( keys , tag ) {
104
+ for ( const declaration of tag . declaration . declarations ) {
105
+ for ( const id of extract_identifiers ( declaration . id ) ) {
106
+ if ( keys . has ( id . name ) ) {
107
+ return true ;
150
108
}
151
109
}
152
110
}
153
-
154
- if ( identifiers . size === 0 ) {
155
- return false ;
156
- }
157
-
158
- for ( const reference of references . keys ( ) ) {
159
- if ( identifiers . has ( reference ) ) {
160
- return true ;
161
- }
162
- }
163
111
return false ;
164
112
}
0 commit comments