@@ -429,36 +429,40 @@ class Parser extends Tapable {
429429 this . walkExpression ( methodDefinition . value ) ;
430430 }
431431
432- walkStatements ( statements ) {
433- for ( let indexA = 0 , lenA = statements . length ; indexA < lenA ; indexA ++ ) {
434- const statementA = statements [ indexA ] ;
435- if ( this . isHoistedStatement ( statementA ) )
436- this . walkStatement ( statementA ) ;
437- }
438- for ( let indexB = 0 , lenB = statements . length ; indexB < lenB ; indexB ++ ) {
439- const statementB = statements [ indexB ] ;
440- if ( ! this . isHoistedStatement ( statementB ) )
441- this . walkStatement ( statementB ) ;
432+ // Prewalking iterates the scope for variable declarations
433+ prewalkStatements ( statements ) {
434+ for ( let index = 0 , len = statements . length ; index < len ; index ++ ) {
435+ const statement = statements [ index ] ;
436+ this . prewalkStatement ( statement ) ;
442437 }
443438 }
444439
445- isHoistedStatement ( statement ) {
446- switch ( statement . type ) {
447- case "ImportDeclaration" :
448- case "ExportAllDeclaration" :
449- case "ExportNamedDeclaration" :
450- return true ;
440+ // Walking iterates the statements and expressions and processes them
441+ walkStatements ( statements ) {
442+ for ( let index = 0 , len = statements . length ; index < len ; index ++ ) {
443+ const statement = statements [ index ] ;
444+ this . walkStatement ( statement ) ;
451445 }
452- return false ;
446+ }
447+
448+ prewalkStatement ( statement ) {
449+ const handler = this [ "prewalk" + statement . type ] ;
450+ if ( handler )
451+ handler . call ( this , statement ) ;
453452 }
454453
455454 walkStatement ( statement ) {
456455 if ( this . applyPluginsBailResult1 ( "statement" , statement ) !== undefined ) return ;
457- if ( this [ "walk" + statement . type ] )
458- this [ "walk" + statement . type ] ( statement ) ;
456+ const handler = this [ "walk" + statement . type ] ;
457+ if ( handler )
458+ handler . call ( this , statement ) ;
459459 }
460460
461461 // Real Statements
462+ prewalkBlockStatement ( statement ) {
463+ this . prewalkStatements ( statement . body ) ;
464+ }
465+
462466 walkBlockStatement ( statement ) {
463467 this . walkStatements ( statement . body ) ;
464468 }
@@ -467,6 +471,12 @@ class Parser extends Tapable {
467471 this . walkExpression ( statement . expression ) ;
468472 }
469473
474+ prewalkIfStatement ( statement ) {
475+ this . prewalkStatement ( statement . consequent ) ;
476+ if ( statement . alternate )
477+ this . prewalkStatement ( statement . alternate ) ;
478+ }
479+
470480 walkIfStatement ( statement ) {
471481 const result = this . applyPluginsBailResult1 ( "statement if" , statement ) ;
472482 if ( result === undefined ) {
@@ -482,17 +492,29 @@ class Parser extends Tapable {
482492 }
483493 }
484494
495+ prewalkLabeledStatement ( statement ) {
496+ this . prewalkStatement ( statement . body ) ;
497+ }
498+
485499 walkLabeledStatement ( statement ) {
486500 const result = this . applyPluginsBailResult1 ( "label " + statement . label . name , statement ) ;
487501 if ( result !== true )
488502 this . walkStatement ( statement . body ) ;
489503 }
490504
505+ prewalkWithStatement ( statement ) {
506+ this . prewalkStatement ( statement . body ) ;
507+ }
508+
491509 walkWithStatement ( statement ) {
492510 this . walkExpression ( statement . object ) ;
493511 this . walkStatement ( statement . body ) ;
494512 }
495513
514+ prewalkSwitchStatement ( statement ) {
515+ this . prewalkSwitchCases ( statement . cases ) ;
516+ }
517+
496518 walkSwitchStatement ( statement ) {
497519 this . walkExpression ( statement . discriminant ) ;
498520 this . walkSwitchCases ( statement . cases ) ;
@@ -511,6 +533,10 @@ class Parser extends Tapable {
511533 this . walkTerminatingStatement ( statement ) ;
512534 }
513535
536+ prewalkTryStatement ( statement ) {
537+ this . prewalkStatement ( statement . block ) ;
538+ }
539+
514540 walkTryStatement ( statement ) {
515541 if ( this . scope . inTry ) {
516542 this . walkStatement ( statement . block ) ;
@@ -525,16 +551,32 @@ class Parser extends Tapable {
525551 this . walkStatement ( statement . finalizer ) ;
526552 }
527553
554+ prewalkWhileStatement ( statement ) {
555+ this . prewalkStatement ( statement . body ) ;
556+ }
557+
528558 walkWhileStatement ( statement ) {
529559 this . walkExpression ( statement . test ) ;
530560 this . walkStatement ( statement . body ) ;
531561 }
532562
563+ prewalkDoWhileStatement ( statement ) {
564+ this . prewalkStatement ( statement . body ) ;
565+ }
566+
533567 walkDoWhileStatement ( statement ) {
534568 this . walkStatement ( statement . body ) ;
535569 this . walkExpression ( statement . test ) ;
536570 }
537571
572+ prewalkForStatement ( statement ) {
573+ if ( statement . init ) {
574+ if ( statement . init . type === "VariableDeclaration" )
575+ this . prewalkStatement ( statement . init ) ;
576+ }
577+ this . prewalkStatement ( statement . body ) ;
578+ }
579+
538580 walkForStatement ( statement ) {
539581 if ( statement . init ) {
540582 if ( statement . init . type === "VariableDeclaration" )
@@ -549,6 +591,12 @@ class Parser extends Tapable {
549591 this . walkStatement ( statement . body ) ;
550592 }
551593
594+ prewalkForInStatement ( statement ) {
595+ if ( statement . left . type === "VariableDeclaration" )
596+ this . prewalkStatement ( statement . left ) ;
597+ this . prewalkStatement ( statement . body ) ;
598+ }
599+
552600 walkForInStatement ( statement ) {
553601 if ( statement . left . type === "VariableDeclaration" )
554602 this . walkStatement ( statement . left ) ;
@@ -558,6 +606,12 @@ class Parser extends Tapable {
558606 this . walkStatement ( statement . body ) ;
559607 }
560608
609+ prewalkForOfStatement ( statement ) {
610+ if ( statement . left . type === "VariableDeclaration" )
611+ this . prewalkStatement ( statement . left ) ;
612+ this . prewalkStatement ( statement . body ) ;
613+ }
614+
561615 walkForOfStatement ( statement ) {
562616 if ( statement . left . type === "VariableDeclaration" )
563617 this . walkStatement ( statement . left ) ;
@@ -568,11 +622,14 @@ class Parser extends Tapable {
568622 }
569623
570624 // Declarations
571- walkFunctionDeclaration ( statement ) {
625+ prewalkFunctionDeclaration ( statement ) {
572626 if ( statement . id ) {
573627 this . scope . renames [ "$" + statement . id . name ] = undefined ;
574628 this . scope . definitions . push ( statement . id . name ) ;
575629 }
630+ }
631+
632+ walkFunctionDeclaration ( statement ) {
576633 this . inScope ( statement . params , function ( ) {
577634 if ( statement . body . type === "BlockStatement" )
578635 this . walkStatement ( statement . body ) ;
@@ -581,7 +638,7 @@ class Parser extends Tapable {
581638 } . bind ( this ) ) ;
582639 }
583640
584- walkImportDeclaration ( statement ) {
641+ prewalkImportDeclaration ( statement ) {
585642 const source = statement . source . value ;
586643 this . applyPluginsBailResult ( "import" , statement , source ) ;
587644 statement . specifiers . forEach ( function ( specifier ) {
@@ -602,7 +659,7 @@ class Parser extends Tapable {
602659 } , this ) ;
603660 }
604661
605- walkExportNamedDeclaration ( statement ) {
662+ prewalkExportNamedDeclaration ( statement ) {
606663 let source ;
607664 if ( statement . source ) {
608665 source = statement . source . value ;
@@ -616,7 +673,7 @@ class Parser extends Tapable {
616673 } else {
617674 if ( ! this . applyPluginsBailResult ( "export declaration" , statement , statement . declaration ) ) {
618675 const pos = this . scope . definitions . length ;
619- this . walkStatement ( statement . declaration ) ;
676+ this . prewalkStatement ( statement . declaration ) ;
620677 const newDefs = this . scope . definitions . slice ( pos ) ;
621678 for ( let index = newDefs . length - 1 ; index >= 0 ; index -- ) {
622679 const def = newDefs [ index ] ;
@@ -643,17 +700,29 @@ class Parser extends Tapable {
643700 }
644701 }
645702
703+ walkExportNamedDeclaration ( statement ) {
704+ if ( statement . declaration ) {
705+ this . walkStatement ( statement . declaration ) ;
706+ }
707+ }
708+
709+ prewalkExportDefaultDeclaration ( statement ) {
710+ if ( / D e c l a r a t i o n $ / . test ( statement . declaration . type ) ) {
711+ const pos = this . scope . definitions . length ;
712+ this . prewalkStatement ( statement . declaration ) ;
713+ const newDefs = this . scope . definitions . slice ( pos ) ;
714+ for ( let index = 0 , len = newDefs . length ; index < len ; index ++ ) {
715+ const def = newDefs [ index ] ;
716+ this . applyPluginsBailResult ( "export specifier" , statement , def , "default" ) ;
717+ }
718+ }
719+ }
720+
646721 walkExportDefaultDeclaration ( statement ) {
647722 this . applyPluginsBailResult1 ( "export" , statement ) ;
648723 if ( / D e c l a r a t i o n $ / . test ( statement . declaration . type ) ) {
649724 if ( ! this . applyPluginsBailResult ( "export declaration" , statement , statement . declaration ) ) {
650- const pos = this . scope . definitions . length ;
651725 this . walkStatement ( statement . declaration ) ;
652- const newDefs = this . scope . definitions . slice ( pos ) ;
653- for ( let index = 0 , len = newDefs . length ; index < len ; index ++ ) {
654- const def = newDefs [ index ] ;
655- this . applyPluginsBailResult ( "export specifier" , statement , def , "default" ) ;
656- }
657726 }
658727 } else {
659728 this . walkExpression ( statement . declaration ) ;
@@ -663,25 +732,40 @@ class Parser extends Tapable {
663732 }
664733 }
665734
666- walkExportAllDeclaration ( statement ) {
735+ prewalkExportAllDeclaration ( statement ) {
667736 const source = statement . source . value ;
668737 this . applyPluginsBailResult ( "export import" , statement , source ) ;
669738 this . applyPluginsBailResult ( "export import specifier" , statement , source , null , null , 0 ) ;
670739 }
671740
741+ prewalkVariableDeclaration ( statement ) {
742+ if ( statement . declarations )
743+ this . prewalkVariableDeclarators ( statement . declarations ) ;
744+ }
745+
672746 walkVariableDeclaration ( statement ) {
673747 if ( statement . declarations )
674748 this . walkVariableDeclarators ( statement . declarations ) ;
675749 }
676750
677- walkClassDeclaration ( statement ) {
751+ prewalkClassDeclaration ( statement ) {
678752 if ( statement . id ) {
679753 this . scope . renames [ "$" + statement . id . name ] = undefined ;
680754 this . scope . definitions . push ( statement . id . name ) ;
681755 }
756+ }
757+
758+ walkClassDeclaration ( statement ) {
682759 this . walkClass ( statement ) ;
683760 }
684761
762+ prewalkSwitchCases ( switchCases ) {
763+ for ( let index = 0 , len = switchCases . length ; index < len ; index ++ ) {
764+ const switchCase = switchCases [ index ] ;
765+ this . prewalkStatements ( switchCase . consequent ) ;
766+ }
767+ }
768+
685769 walkSwitchCases ( switchCases ) {
686770 for ( let index = 0 , len = switchCases . length ; index < len ; index ++ ) {
687771 const switchCase = switchCases [ index ] ;
@@ -697,11 +781,12 @@ class Parser extends Tapable {
697781 if ( catchClause . guard )
698782 this . walkExpression ( catchClause . guard ) ;
699783 this . inScope ( [ catchClause . param ] , function ( ) {
784+ this . prewalkStatement ( catchClause . body ) ;
700785 this . walkStatement ( catchClause . body ) ;
701786 } . bind ( this ) ) ;
702787 }
703788
704- walkVariableDeclarators ( declarators ) {
789+ prewalkVariableDeclarators ( declarators ) {
705790 declarators . forEach ( declarator => {
706791 switch ( declarator . type ) {
707792 case "VariableDeclarator" :
@@ -715,7 +800,6 @@ class Parser extends Tapable {
715800 if ( idx >= 0 ) this . scope . definitions . splice ( idx , 1 ) ;
716801 }
717802 } else {
718- this . walkPattern ( declarator . id ) ;
719803 this . enterPattern ( declarator . id , ( name , decl ) => {
720804 if ( ! this . applyPluginsBailResult1 ( "var-" + declarator . kind + " " + name , decl ) ) {
721805 if ( ! this . applyPluginsBailResult1 ( "var " + name , decl ) ) {
@@ -724,6 +808,21 @@ class Parser extends Tapable {
724808 }
725809 }
726810 } ) ;
811+ }
812+ break ;
813+ }
814+ }
815+ } ) ;
816+ }
817+
818+ walkVariableDeclarators ( declarators ) {
819+ declarators . forEach ( declarator => {
820+ switch ( declarator . type ) {
821+ case "VariableDeclarator" :
822+ {
823+ const renameIdentifier = declarator . init && this . getRenameIdentifier ( declarator . init ) ;
824+ if ( ! renameIdentifier || declarator . id . type !== "Identifier" || ! this . applyPluginsBailResult1 ( "can-rename " + renameIdentifier , declarator . init ) ) {
825+ this . walkPattern ( declarator . id ) ;
727826 if ( declarator . init )
728827 this . walkExpression ( declarator . init ) ;
729828 }
@@ -808,19 +907,23 @@ class Parser extends Tapable {
808907
809908 walkFunctionExpression ( expression ) {
810909 this . inScope ( expression . params , function ( ) {
811- if ( expression . body . type === "BlockStatement" )
910+ if ( expression . body . type === "BlockStatement" ) {
911+ this . prewalkStatement ( expression . body ) ;
812912 this . walkStatement ( expression . body ) ;
813- else
913+ } else {
814914 this . walkExpression ( expression . body ) ;
915+ }
815916 } . bind ( this ) ) ;
816917 }
817918
818919 walkArrowFunctionExpression ( expression ) {
819920 this . inScope ( expression . params , function ( ) {
820- if ( expression . body . type === "BlockStatement" )
921+ if ( expression . body . type === "BlockStatement" ) {
922+ this . prewalkStatement ( expression . body ) ;
821923 this . walkStatement ( expression . body ) ;
822- else
924+ } else {
823925 this . walkExpression ( expression . body ) ;
926+ }
824927 } . bind ( this ) ) ;
825928 }
826929
@@ -953,9 +1056,10 @@ class Parser extends Tapable {
9531056 if ( ! params [ i ] || params [ i ] . type !== "Identifier" ) continue ;
9541057 this . scope . renames [ "$" + params [ i ] . name ] = param ;
9551058 }
956- if ( functionExpression . body . type === "BlockStatement" )
1059+ if ( functionExpression . body . type === "BlockStatement" ) {
1060+ this . prewalkStatement ( functionExpression . body ) ;
9571061 this . walkStatement ( functionExpression . body ) ;
958- else
1062+ } else
9591063 this . walkExpression ( functionExpression . body ) ;
9601064 } . bind ( this ) ) ;
9611065 }
@@ -1232,8 +1336,10 @@ class Parser extends Tapable {
12321336 } ;
12331337 const state = this . state = initialState || { } ;
12341338 this . comments = comments ;
1235- if ( this . applyPluginsBailResult ( "program" , ast , comments ) === undefined )
1339+ if ( this . applyPluginsBailResult ( "program" , ast , comments ) === undefined ) {
1340+ this . prewalkStatements ( ast . body ) ;
12361341 this . walkStatements ( ast . body ) ;
1342+ }
12371343 this . scope = oldScope ;
12381344 this . state = oldState ;
12391345 this . comments = oldComments ;
0 commit comments