@@ -193,7 +193,7 @@ export default function(babel) {
193
193
}
194
194
}
195
195
196
- function getHookCallsSignature ( functionNode ) {
196
+ function getHookCallsSignature ( functionNode , scope ) {
197
197
const fnHookCalls = hookCalls . get ( functionNode ) ;
198
198
if ( fnHookCalls === undefined ) {
199
199
return null ;
@@ -202,6 +202,7 @@ export default function(babel) {
202
202
key : fnHookCalls . map ( call => call . name + '{' + call . key + '}' ) . join ( '\n' ) ,
203
203
customHooks : fnHookCalls
204
204
. filter ( call => ! isBuiltinHook ( call . name ) )
205
+ . filter ( call => scope . parent . hasBinding ( call . name ) )
205
206
. map ( call => t . cloneDeep ( call . callee ) ) ,
206
207
} ;
207
208
}
@@ -240,6 +241,7 @@ export default function(babel) {
240
241
return ;
241
242
}
242
243
const fnScope = path . scope . getFunctionParent ( ) ;
244
+
243
245
if ( fnScope === null ) {
244
246
return ;
245
247
}
@@ -249,6 +251,7 @@ export default function(babel) {
249
251
if ( ! hookCalls . has ( fnNode ) ) {
250
252
hookCalls . set ( fnNode , [ ] ) ;
251
253
}
254
+
252
255
let hookCallsForFn = hookCalls . get ( fnNode ) ;
253
256
let key = '' ;
254
257
if ( path . parent . type === 'VariableDeclarator' ) {
@@ -384,7 +387,7 @@ export default function(babel) {
384
387
if ( id === null ) {
385
388
return ;
386
389
}
387
- const signature = getHookCallsSignature ( node ) ;
390
+ const signature = getHookCallsSignature ( node , path . scope ) ;
388
391
if ( signature === null ) {
389
392
return ;
390
393
}
@@ -424,7 +427,7 @@ export default function(babel) {
424
427
'ArrowFunctionExpression|FunctionExpression' : {
425
428
exit ( path ) {
426
429
const node = path . node ;
427
- const signature = getHookCallsSignature ( node ) ;
430
+ const signature = getHookCallsSignature ( node , path . scope ) ;
428
431
if ( signature === null ) {
429
432
return ;
430
433
}
@@ -474,80 +477,6 @@ export default function(babel) {
474
477
}
475
478
} ,
476
479
} ,
477
- VariableDeclaration ( path ) {
478
- return ;
479
- const node = path . node ;
480
- let programPath ;
481
- let insertAfterPath ;
482
- switch ( path . parent . type ) {
483
- case 'Program' :
484
- insertAfterPath = path ;
485
- programPath = path . parentPath ;
486
- break ;
487
- case 'ExportNamedDeclaration' :
488
- insertAfterPath = path . parentPath ;
489
- programPath = insertAfterPath . parentPath ;
490
- break ;
491
- case 'ExportDefaultDeclaration' :
492
- insertAfterPath = path . parentPath ;
493
- programPath = insertAfterPath . parentPath ;
494
- break ;
495
- default :
496
- return ;
497
- }
498
-
499
- // Make sure we're not mutating the same tree twice.
500
- // This can happen if another Babel plugin replaces parents.
501
- if ( seenForRegistration . has ( node ) ) {
502
- return ;
503
- }
504
- seenForRegistration . add ( node ) ;
505
- // Don't mutate the tree above this point.
506
-
507
- const declPaths = path . get ( 'declarations' ) ;
508
- if ( declPaths . length !== 1 ) {
509
- return ;
510
- }
511
- const declPath = declPaths [ 0 ] ;
512
- const inferredName = declPath . node . id . name ;
513
- findInnerComponents (
514
- inferredName ,
515
- declPath ,
516
- ( persistentID , targetExpr , targetPath ) => {
517
- if ( targetPath === null ) {
518
- // For case like:
519
- // export const Something = hoc(Foo)
520
- // we don't want to wrap Foo inside the call.
521
- // Instead we assume it's registered at definition.
522
- return ;
523
- }
524
- const handle = createRegistration ( programPath , persistentID ) ;
525
- if (
526
- ( targetExpr . type === 'ArrowFunctionExpression' ||
527
- targetExpr . type === 'FunctionExpression' ) &&
528
- targetPath . parent . type === 'VariableDeclarator'
529
- ) {
530
- // Special case when a function would get an inferred name:
531
- // let Foo = () => {}
532
- // let Foo = function() {}
533
- // We'll register it on next line so that
534
- // we don't mess up the inferred 'Foo' function name.
535
- insertAfterPath . insertAfter (
536
- t . expressionStatement (
537
- t . assignmentExpression ( '=' , handle , declPath . node . id ) ,
538
- ) ,
539
- ) ;
540
- // Result: let Foo = () => {}; _c1 = Foo;
541
- } else {
542
- // let Foo = hoc(() => {})
543
- targetPath . replaceWith (
544
- t . assignmentExpression ( '=' , handle , targetExpr ) ,
545
- ) ;
546
- // Result: let Foo = _c1 = hoc(() => {})
547
- }
548
- } ,
549
- ) ;
550
- } ,
551
480
Program : {
552
481
enter ( path ) {
553
482
// This is a separate early visitor because we need to collect Hook calls
0 commit comments