@@ -135,25 +135,7 @@ function parseAsSvelte(
135
135
analyzeStoreScope ( resultScript . scopeManager ! ) ; // for reactive vars
136
136
137
137
// Add $$xxx variable
138
- const globalScope = resultScript . scopeManager ! . globalScope ;
139
- for ( const $$name of globals ) {
140
- if ( globalScope . set . has ( $$name ) ) continue ;
141
- const variable = new Variable ( ) ;
142
- variable . name = $$name ;
143
- ( variable as any ) . scope = globalScope ;
144
- globalScope . variables . push ( variable ) ;
145
- globalScope . set . set ( $$name , variable ) ;
146
- globalScope . through = globalScope . through . filter ( ( reference ) => {
147
- if ( reference . identifier . name === $$name ) {
148
- // Links the variable and the reference.
149
- // And this reference is removed from `Scope#through`.
150
- reference . resolved = variable ;
151
- addReference ( variable . references , reference ) ;
152
- return false ;
153
- }
154
- return true ;
155
- } ) ;
156
- }
138
+ addGlobalVariables ( resultScript . scopeManager ! , globals ) ;
157
139
158
140
const ast = resultTemplate . ast ;
159
141
@@ -239,16 +221,30 @@ function parseAsScript(
239
221
: parseScript ( code , { lang } , parserOptions ) ;
240
222
241
223
// Add $$xxx variable
242
- const globalScope = resultScript . scopeManager ! . globalScope ;
243
- for ( const $$name of globalsForSvelteScript ) {
244
- if ( globalScope . set . has ( $$name ) ) continue ;
224
+ addGlobalVariables ( resultScript . scopeManager ! , globalsForSvelteScript ) ;
225
+
226
+ resultScript . services = Object . assign ( resultScript . services || { } , {
227
+ isSvelte : false ,
228
+ isSvelteScript : true ,
229
+ } ) ;
230
+ resultScript . visitorKeys = Object . assign ( { } , KEYS , resultScript . visitorKeys ) ;
231
+ return resultScript as any ;
232
+ }
233
+
234
+ function addGlobalVariables (
235
+ scopeManager : ScopeManager ,
236
+ globals : readonly string [ ] ,
237
+ ) {
238
+ const globalScope = scopeManager . globalScope ;
239
+ for ( const globalName of globals ) {
240
+ if ( globalScope . set . has ( globalName ) ) continue ;
245
241
const variable = new Variable ( ) ;
246
- variable . name = $$name ;
242
+ variable . name = globalName ;
247
243
( variable as any ) . scope = globalScope ;
248
244
globalScope . variables . push ( variable ) ;
249
- globalScope . set . set ( $$name , variable ) ;
245
+ globalScope . set . set ( globalName , variable ) ;
250
246
globalScope . through = globalScope . through . filter ( ( reference ) => {
251
- if ( reference . identifier . name === $$name ) {
247
+ if ( reference . identifier . name === globalName ) {
252
248
// Links the variable and the reference.
253
249
// And this reference is removed from `Scope#through`.
254
250
reference . resolved = variable ;
@@ -258,13 +254,6 @@ function parseAsScript(
258
254
return true ;
259
255
} ) ;
260
256
}
261
-
262
- resultScript . services = Object . assign ( resultScript . services || { } , {
263
- isSvelte : false ,
264
- isSvelteScript : true ,
265
- } ) ;
266
- resultScript . visitorKeys = Object . assign ( { } , KEYS , resultScript . visitorKeys ) ;
267
- return resultScript as any ;
268
257
}
269
258
270
259
/** Extract tokens */
0 commit comments