@@ -274,41 +274,7 @@ export default class Expression {
274
274
) ;
275
275
276
276
const declaration = b `const ${ id } = ${ node } ` ;
277
-
278
- if ( owner . type === 'ConstTag' ) {
279
- let child_scope = scope ;
280
- walk ( node , {
281
- enter ( node : Node , parent : any ) {
282
- if ( map . has ( node ) ) child_scope = map . get ( node ) ;
283
- if ( node . type === 'Identifier' && is_reference ( node , parent ) ) {
284
- if ( child_scope . has ( node . name ) ) return ;
285
- this . replace ( block . renderer . reference ( node , ctx ) ) ;
286
- }
287
- } ,
288
- leave ( node : Node ) {
289
- if ( map . has ( node ) ) child_scope = child_scope . parent ;
290
- }
291
- } ) ;
292
- } else if ( dependencies . size === 0 && contextual_dependencies . size === 0 ) {
293
- // we can hoist this out of the component completely
294
- component . fully_hoisted . push ( declaration ) ;
295
-
296
- this . replace ( id as any ) ;
297
-
298
- component . add_var ( node , {
299
- name : id . name ,
300
- internal : true ,
301
- hoistable : true ,
302
- referenced : true
303
- } ) ;
304
- } else if ( contextual_dependencies . size === 0 ) {
305
- // function can be hoisted inside the component init
306
- component . partly_hoisted . push ( declaration ) ;
307
-
308
- block . renderer . add_to_context ( id . name ) ;
309
- this . replace ( block . renderer . reference ( id ) ) ;
310
- } else {
311
- // we need a combo block/init recipe
277
+ const extract_functions = ( ) => {
312
278
const deps = Array . from ( contextual_dependencies ) ;
313
279
const function_expression = node as FunctionExpression ;
314
280
@@ -318,7 +284,7 @@ export default class Expression {
318
284
...function_expression . params
319
285
] ;
320
286
321
- const context_args = deps . map ( name => block . renderer . reference ( name ) ) ;
287
+ const context_args = deps . map ( name => block . renderer . reference ( name , ctx ) ) ;
322
288
323
289
component . partly_hoisted . push ( declaration ) ;
324
290
@@ -334,6 +300,50 @@ export default class Expression {
334
300
: b `function ${ id } () {
335
301
return ${ callee } (${ context_args } );
336
302
}` ;
303
+ return { deps, func_declaration } ;
304
+ } ;
305
+
306
+ if ( owner . type === 'ConstTag' ) {
307
+ // we need a combo block/init recipe
308
+ if ( contextual_dependencies . size === 0 ) {
309
+ let child_scope = scope ;
310
+ walk ( node , {
311
+ enter ( node : Node , parent : any ) {
312
+ if ( map . has ( node ) ) child_scope = map . get ( node ) ;
313
+ if ( node . type === 'Identifier' && is_reference ( node , parent ) ) {
314
+ if ( child_scope . has ( node . name ) ) return ;
315
+ this . replace ( block . renderer . reference ( node , ctx ) ) ;
316
+ }
317
+ } ,
318
+ leave ( node : Node ) {
319
+ if ( map . has ( node ) ) child_scope = child_scope . parent ;
320
+ }
321
+ } ) ;
322
+ } else {
323
+ const { func_declaration } = extract_functions ( ) ;
324
+ this . replace ( func_declaration [ 0 ] ) ;
325
+ }
326
+ } else if ( dependencies . size === 0 && contextual_dependencies . size === 0 ) {
327
+ // we can hoist this out of the component completely
328
+ component . fully_hoisted . push ( declaration ) ;
329
+
330
+ this . replace ( id as any ) ;
331
+
332
+ component . add_var ( node , {
333
+ name : id . name ,
334
+ internal : true ,
335
+ hoistable : true ,
336
+ referenced : true
337
+ } ) ;
338
+ } else if ( contextual_dependencies . size === 0 ) {
339
+ // function can be hoisted inside the component init
340
+ component . partly_hoisted . push ( declaration ) ;
341
+
342
+ block . renderer . add_to_context ( id . name ) ;
343
+ this . replace ( block . renderer . reference ( id ) ) ;
344
+ } else {
345
+ // we need a combo block/init recipe
346
+ const { deps, func_declaration } = extract_functions ( ) ;
337
347
338
348
if ( owner . type === 'Attribute' && owner . parent . name === 'slot' ) {
339
349
const dep_scopes = new Set < INode > ( deps . map ( name => template_scope . get_owner ( name ) ) ) ;
0 commit comments