@@ -435,36 +435,40 @@ class Parser extends Tapable {
435435 this . walkExpression ( methodDefinition . value ) ;
436436 }
437437
438- walkStatements ( statements ) {
439- for ( let indexA = 0 , lenA = statements . length ; indexA < lenA ; indexA ++ ) {
440- const statementA = statements [ indexA ] ;
441- if ( this . isHoistedStatement ( statementA ) )
442- this . walkStatement ( statementA ) ;
443- }
444- for ( let indexB = 0 , lenB = statements . length ; indexB < lenB ; indexB ++ ) {
445- const statementB = statements [ indexB ] ;
446- if ( ! this . isHoistedStatement ( statementB ) )
447- this . walkStatement ( statementB ) ;
438+ // Prewalking iterates the scope for variable declarations
439+ prewalkStatements ( statements ) {
440+ for ( let index = 0 , len = statements . length ; index < len ; index ++ ) {
441+ const statement = statements [ index ] ;
442+ this . prewalkStatement ( statement ) ;
448443 }
449444 }
450445
451- isHoistedStatement ( statement ) {
452- switch ( statement . type ) {
453- case "ImportDeclaration" :
454- case "ExportAllDeclaration" :
455- case "ExportNamedDeclaration" :
456- return true ;
446+ // Walking iterates the statements and expressions and processes them
447+ walkStatements ( statements ) {
448+ for ( let index = 0 , len = statements . length ; index < len ; index ++ ) {
449+ const statement = statements [ index ] ;
450+ this . walkStatement ( statement ) ;
457451 }
458- return false ;
452+ }
453+
454+ prewalkStatement ( statement ) {
455+ const handler = this [ "prewalk" + statement . type ] ;
456+ if ( handler )
457+ handler . call ( this , statement ) ;
459458 }
460459
461460 walkStatement ( statement ) {
462461 if ( this . applyPluginsBailResult1 ( "statement" , statement ) !== undefined ) return ;
463- if ( this [ "walk" + statement . type ] )
464- this [ "walk" + statement . type ] ( statement ) ;
462+ const handler = this [ "walk" + statement . type ] ;
463+ if ( handler )
464+ handler . call ( this , statement ) ;
465465 }
466466
467467 // Real Statements
468+ prewalkBlockStatement ( statement ) {
469+ this . prewalkStatements ( statement . body ) ;
470+ }
471+
468472 walkBlockStatement ( statement ) {
469473 this . walkStatements ( statement . body ) ;
470474 }
@@ -473,6 +477,12 @@ class Parser extends Tapable {
473477 this . walkExpression ( statement . expression ) ;
474478 }
475479
480+ prewalkIfStatement ( statement ) {
481+ this . prewalkStatement ( statement . consequent ) ;
482+ if ( statement . alternate )
483+ this . prewalkStatement ( statement . alternate ) ;
484+ }
485+
476486 walkIfStatement ( statement ) {
477487 const result = this . applyPluginsBailResult1 ( "statement if" , statement ) ;
478488 if ( result === undefined ) {
@@ -488,17 +498,29 @@ class Parser extends Tapable {
488498 }
489499 }
490500
501+ prewalkLabeledStatement ( statement ) {
502+ this . prewalkStatement ( statement . body ) ;
503+ }
504+
491505 walkLabeledStatement ( statement ) {
492506 const result = this . applyPluginsBailResult1 ( "label " + statement . label . name , statement ) ;
493507 if ( result !== true )
494508 this . walkStatement ( statement . body ) ;
495509 }
496510
511+ prewalkWithStatement ( statement ) {
512+ this . prewalkStatement ( statement . body ) ;
513+ }
514+
497515 walkWithStatement ( statement ) {
498516 this . walkExpression ( statement . object ) ;
499517 this . walkStatement ( statement . body ) ;
500518 }
501519
520+ prewalkSwitchStatement ( statement ) {
521+ this . prewalkSwitchCases ( statement . cases ) ;
522+ }
523+
502524 walkSwitchStatement ( statement ) {
503525 this . walkExpression ( statement . discriminant ) ;
504526 this . walkSwitchCases ( statement . cases ) ;
@@ -517,6 +539,10 @@ class Parser extends Tapable {
517539 this . walkTerminatingStatement ( statement ) ;
518540 }
519541
542+ prewalkTryStatement ( statement ) {
543+ this . prewalkStatement ( statement . block ) ;
544+ }
545+
520546 walkTryStatement ( statement ) {
521547 if ( this . scope . inTry ) {
522548 this . walkStatement ( statement . block ) ;
@@ -531,16 +557,32 @@ class Parser extends Tapable {
531557 this . walkStatement ( statement . finalizer ) ;
532558 }
533559
560+ prewalkWhileStatement ( statement ) {
561+ this . prewalkStatement ( statement . body ) ;
562+ }
563+
534564 walkWhileStatement ( statement ) {
535565 this . walkExpression ( statement . test ) ;
536566 this . walkStatement ( statement . body ) ;
537567 }
538568
569+ prewalkDoWhileStatement ( statement ) {
570+ this . prewalkStatement ( statement . body ) ;
571+ }
572+
539573 walkDoWhileStatement ( statement ) {
540574 this . walkStatement ( statement . body ) ;
541575 this . walkExpression ( statement . test ) ;
542576 }
543577
578+ prewalkForStatement ( statement ) {
579+ if ( statement . init ) {
580+ if ( statement . init . type === "VariableDeclaration" )
581+ this . prewalkStatement ( statement . init ) ;
582+ }
583+ this . prewalkStatement ( statement . body ) ;
584+ }
585+
544586 walkForStatement ( statement ) {
545587 if ( statement . init ) {
546588 if ( statement . init . type === "VariableDeclaration" )
@@ -555,6 +597,12 @@ class Parser extends Tapable {
555597 this . walkStatement ( statement . body ) ;
556598 }
557599
600+ prewalkForInStatement ( statement ) {
601+ if ( statement . left . type === "VariableDeclaration" )
602+ this . prewalkStatement ( statement . left ) ;
603+ this . prewalkStatement ( statement . body ) ;
604+ }
605+
558606 walkForInStatement ( statement ) {
559607 if ( statement . left . type === "VariableDeclaration" )
560608 this . walkStatement ( statement . left ) ;
@@ -564,6 +612,12 @@ class Parser extends Tapable {
564612 this . walkStatement ( statement . body ) ;
565613 }
566614
615+ prewalkForOfStatement ( statement ) {
616+ if ( statement . left . type === "VariableDeclaration" )
617+ this . prewalkStatement ( statement . left ) ;
618+ this . prewalkStatement ( statement . body ) ;
619+ }
620+
567621 walkForOfStatement ( statement ) {
568622 if ( statement . left . type === "VariableDeclaration" )
569623 this . walkStatement ( statement . left ) ;
@@ -574,11 +628,14 @@ class Parser extends Tapable {
574628 }
575629
576630 // Declarations
577- walkFunctionDeclaration ( statement ) {
631+ prewalkFunctionDeclaration ( statement ) {
578632 if ( statement . id ) {
579633 this . scope . renames [ "$" + statement . id . name ] = undefined ;
580634 this . scope . definitions . push ( statement . id . name ) ;
581635 }
636+ }
637+
638+ walkFunctionDeclaration ( statement ) {
582639 this . inScope ( statement . params , function ( ) {
583640 if ( statement . body . type === "BlockStatement" )
584641 this . walkStatement ( statement . body ) ;
@@ -587,7 +644,7 @@ class Parser extends Tapable {
587644 } . bind ( this ) ) ;
588645 }
589646
590- walkImportDeclaration ( statement ) {
647+ prewalkImportDeclaration ( statement ) {
591648 const source = statement . source . value ;
592649 this . applyPluginsBailResult ( "import" , statement , source ) ;
593650 statement . specifiers . forEach ( function ( specifier ) {
@@ -608,7 +665,7 @@ class Parser extends Tapable {
608665 } , this ) ;
609666 }
610667
611- walkExportNamedDeclaration ( statement ) {
668+ prewalkExportNamedDeclaration ( statement ) {
612669 let source ;
613670 if ( statement . source ) {
614671 source = statement . source . value ;
@@ -622,7 +679,7 @@ class Parser extends Tapable {
622679 } else {
623680 if ( ! this . applyPluginsBailResult ( "export declaration" , statement , statement . declaration ) ) {
624681 const pos = this . scope . definitions . length ;
625- this . walkStatement ( statement . declaration ) ;
682+ this . prewalkStatement ( statement . declaration ) ;
626683 const newDefs = this . scope . definitions . slice ( pos ) ;
627684 for ( let index = newDefs . length - 1 ; index >= 0 ; index -- ) {
628685 const def = newDefs [ index ] ;
@@ -649,17 +706,29 @@ class Parser extends Tapable {
649706 }
650707 }
651708
709+ walkExportNamedDeclaration ( statement ) {
710+ if ( statement . declaration ) {
711+ this . walkStatement ( statement . declaration ) ;
712+ }
713+ }
714+
715+ prewalkExportDefaultDeclaration ( statement ) {
716+ if ( / D e c l a r a t i o n $ / . test ( statement . declaration . type ) ) {
717+ const pos = this . scope . definitions . length ;
718+ this . prewalkStatement ( statement . declaration ) ;
719+ const newDefs = this . scope . definitions . slice ( pos ) ;
720+ for ( let index = 0 , len = newDefs . length ; index < len ; index ++ ) {
721+ const def = newDefs [ index ] ;
722+ this . applyPluginsBailResult ( "export specifier" , statement , def , "default" ) ;
723+ }
724+ }
725+ }
726+
652727 walkExportDefaultDeclaration ( statement ) {
653728 this . applyPluginsBailResult1 ( "export" , statement ) ;
654729 if ( / D e c l a r a t i o n $ / . test ( statement . declaration . type ) ) {
655730 if ( ! this . applyPluginsBailResult ( "export declaration" , statement , statement . declaration ) ) {
656- const pos = this . scope . definitions . length ;
657731 this . walkStatement ( statement . declaration ) ;
658- const newDefs = this . scope . definitions . slice ( pos ) ;
659- for ( let index = 0 , len = newDefs . length ; index < len ; index ++ ) {
660- const def = newDefs [ index ] ;
661- this . applyPluginsBailResult ( "export specifier" , statement , def , "default" ) ;
662- }
663732 }
664733 } else {
665734 this . walkExpression ( statement . declaration ) ;
@@ -669,25 +738,40 @@ class Parser extends Tapable {
669738 }
670739 }
671740
672- walkExportAllDeclaration ( statement ) {
741+ prewalkExportAllDeclaration ( statement ) {
673742 const source = statement . source . value ;
674743 this . applyPluginsBailResult ( "export import" , statement , source ) ;
675744 this . applyPluginsBailResult ( "export import specifier" , statement , source , null , null , 0 ) ;
676745 }
677746
747+ prewalkVariableDeclaration ( statement ) {
748+ if ( statement . declarations )
749+ this . prewalkVariableDeclarators ( statement . declarations ) ;
750+ }
751+
678752 walkVariableDeclaration ( statement ) {
679753 if ( statement . declarations )
680754 this . walkVariableDeclarators ( statement . declarations ) ;
681755 }
682756
683- walkClassDeclaration ( statement ) {
757+ prewalkClassDeclaration ( statement ) {
684758 if ( statement . id ) {
685759 this . scope . renames [ "$" + statement . id . name ] = undefined ;
686760 this . scope . definitions . push ( statement . id . name ) ;
687761 }
762+ }
763+
764+ walkClassDeclaration ( statement ) {
688765 this . walkClass ( statement ) ;
689766 }
690767
768+ prewalkSwitchCases ( switchCases ) {
769+ for ( let index = 0 , len = switchCases . length ; index < len ; index ++ ) {
770+ const switchCase = switchCases [ index ] ;
771+ this . prewalkStatements ( switchCase . consequent ) ;
772+ }
773+ }
774+
691775 walkSwitchCases ( switchCases ) {
692776 for ( let index = 0 , len = switchCases . length ; index < len ; index ++ ) {
693777 const switchCase = switchCases [ index ] ;
@@ -701,11 +785,12 @@ class Parser extends Tapable {
701785
702786 walkCatchClause ( catchClause ) {
703787 this . inScope ( [ catchClause . param ] , function ( ) {
788+ this . prewalkStatement ( catchClause . body ) ;
704789 this . walkStatement ( catchClause . body ) ;
705790 } . bind ( this ) ) ;
706791 }
707792
708- walkVariableDeclarators ( declarators ) {
793+ prewalkVariableDeclarators ( declarators ) {
709794 declarators . forEach ( declarator => {
710795 switch ( declarator . type ) {
711796 case "VariableDeclarator" :
@@ -719,7 +804,6 @@ class Parser extends Tapable {
719804 if ( idx >= 0 ) this . scope . definitions . splice ( idx , 1 ) ;
720805 }
721806 } else {
722- this . walkPattern ( declarator . id ) ;
723807 this . enterPattern ( declarator . id , ( name , decl ) => {
724808 if ( ! this . applyPluginsBailResult1 ( "var-" + declarator . kind + " " + name , decl ) ) {
725809 if ( ! this . applyPluginsBailResult1 ( "var " + name , decl ) ) {
@@ -728,6 +812,21 @@ class Parser extends Tapable {
728812 }
729813 }
730814 } ) ;
815+ }
816+ break ;
817+ }
818+ }
819+ } ) ;
820+ }
821+
822+ walkVariableDeclarators ( declarators ) {
823+ declarators . forEach ( declarator => {
824+ switch ( declarator . type ) {
825+ case "VariableDeclarator" :
826+ {
827+ const renameIdentifier = declarator . init && this . getRenameIdentifier ( declarator . init ) ;
828+ if ( ! renameIdentifier || declarator . id . type !== "Identifier" || ! this . applyPluginsBailResult1 ( "can-rename " + renameIdentifier , declarator . init ) ) {
829+ this . walkPattern ( declarator . id ) ;
731830 if ( declarator . init )
732831 this . walkExpression ( declarator . init ) ;
733832 }
@@ -812,19 +911,23 @@ class Parser extends Tapable {
812911
813912 walkFunctionExpression ( expression ) {
814913 this . inScope ( expression . params , function ( ) {
815- if ( expression . body . type === "BlockStatement" )
914+ if ( expression . body . type === "BlockStatement" ) {
915+ this . prewalkStatement ( expression . body ) ;
816916 this . walkStatement ( expression . body ) ;
817- else
917+ } else {
818918 this . walkExpression ( expression . body ) ;
919+ }
819920 } . bind ( this ) ) ;
820921 }
821922
822923 walkArrowFunctionExpression ( expression ) {
823924 this . inScope ( expression . params , function ( ) {
824- if ( expression . body . type === "BlockStatement" )
925+ if ( expression . body . type === "BlockStatement" ) {
926+ this . prewalkStatement ( expression . body ) ;
825927 this . walkStatement ( expression . body ) ;
826- else
928+ } else {
827929 this . walkExpression ( expression . body ) ;
930+ }
828931 } . bind ( this ) ) ;
829932 }
830933
@@ -957,9 +1060,10 @@ class Parser extends Tapable {
9571060 if ( ! params [ i ] || params [ i ] . type !== "Identifier" ) continue ;
9581061 this . scope . renames [ "$" + params [ i ] . name ] = param ;
9591062 }
960- if ( functionExpression . body . type === "BlockStatement" )
1063+ if ( functionExpression . body . type === "BlockStatement" ) {
1064+ this . prewalkStatement ( functionExpression . body ) ;
9611065 this . walkStatement ( functionExpression . body ) ;
962- else
1066+ } else
9631067 this . walkExpression ( functionExpression . body ) ;
9641068 } . bind ( this ) ) ;
9651069 }
@@ -1236,8 +1340,10 @@ class Parser extends Tapable {
12361340 } ;
12371341 const state = this . state = initialState || { } ;
12381342 this . comments = comments ;
1239- if ( this . applyPluginsBailResult ( "program" , ast , comments ) === undefined )
1343+ if ( this . applyPluginsBailResult ( "program" , ast , comments ) === undefined ) {
1344+ this . prewalkStatements ( ast . body ) ;
12401345 this . walkStatements ( ast . body ) ;
1346+ }
12411347 this . scope = oldScope ;
12421348 this . state = oldState ;
12431349 this . comments = oldComments ;
0 commit comments