@@ -58,6 +58,7 @@ type ScriptLetRestoreCallbackOption = {
5858 registerNodeToScope : ( node : any , scope : Scope ) => void ;
5959 scopeManager : ScopeManager ;
6060 visitorKeys ?: { [ type : string ] : string [ ] } ;
61+ addPostProcess : ( callback : ( ) => void ) => void ;
6162} ;
6263
6364/**
@@ -130,6 +131,8 @@ export class ScriptLetContext {
130131
131132 private readonly restoreCallbacks : RestoreCallback [ ] = [ ] ;
132133
134+ private readonly programRestoreCallbacks : ScriptLetRestoreCallback [ ] = [ ] ;
135+
133136 private readonly closeScopeCallbacks : ( ( ) => void ) [ ] = [ ] ;
134137
135138 private readonly unique = new UniqueIdGenerator ( ) ;
@@ -574,6 +577,10 @@ export class ScriptLetContext {
574577 this . closeScopeCallbacks . pop ( ) ! ( ) ;
575578 }
576579
580+ public addProgramRestore ( callback : ScriptLetRestoreCallback ) : void {
581+ this . programRestoreCallbacks . push ( callback ) ;
582+ }
583+
577584 private appendScript (
578585 text : string ,
579586 offset : number ,
@@ -631,6 +638,57 @@ export class ScriptLetContext {
631638 * Restore AST nodes
632639 */
633640 public restore ( result : ESLintExtendedProgram ) : void {
641+ const nodeToScope = getNodeToScope ( result . scopeManager ! ) ;
642+ const postprocessList : ( ( ) => void ) [ ] = [ ] ;
643+
644+ const callbackOption : ScriptLetRestoreCallbackOption = {
645+ getScope,
646+ getInnermostScope,
647+ registerNodeToScope,
648+ scopeManager : result . scopeManager ! ,
649+ visitorKeys : result . visitorKeys ,
650+ addPostProcess : ( cb ) => postprocessList . push ( cb ) ,
651+ } ;
652+
653+ this . restoreNodes ( result , callbackOption ) ;
654+ this . restoreProgram ( result , callbackOption ) ;
655+ postprocessList . forEach ( ( p ) => p ( ) ) ;
656+
657+ // Helpers
658+ /** Get scope */
659+ function getScope ( node : ESTree . Node ) {
660+ return getScopeFromNode ( result . scopeManager ! , node ) ;
661+ }
662+
663+ /** Get innermost scope */
664+ function getInnermostScope ( node : ESTree . Node ) {
665+ return getInnermostScopeFromNode ( result . scopeManager ! , node ) ;
666+ }
667+
668+ /** Register node to scope */
669+ function registerNodeToScope ( node : any , scope : Scope ) : void {
670+ // If we replace the `scope.block` at this time,
671+ // the scope restore calculation will not work, so we will replace the `scope.block` later.
672+ postprocessList . push ( ( ) => {
673+ scope . block = node ;
674+ } ) ;
675+
676+ const scopes = nodeToScope . get ( node ) ;
677+ if ( scopes ) {
678+ scopes . push ( scope ) ;
679+ } else {
680+ nodeToScope . set ( node , [ scope ] ) ;
681+ }
682+ }
683+ }
684+
685+ /**
686+ * Restore AST nodes
687+ */
688+ private restoreNodes (
689+ result : ESLintExtendedProgram ,
690+ callbackOption : ScriptLetRestoreCallbackOption
691+ ) : void {
634692 let orderedRestoreCallback = this . restoreCallbacks . shift ( ) ;
635693 if ( ! orderedRestoreCallback ) {
636694 return ;
@@ -640,8 +698,6 @@ export class ScriptLetContext {
640698 const processedTokens = [ ] ;
641699 const comments = result . ast . comments ;
642700 const processedComments = [ ] ;
643- const nodeToScope = getNodeToScope ( result . scopeManager ! ) ;
644- const postprocessList : ( ( ) => void ) [ ] = [ ] ;
645701
646702 let tok ;
647703 while ( ( tok = tokens . shift ( ) ) ) {
@@ -731,13 +787,12 @@ export class ScriptLetContext {
731787 startIndex . comment ,
732788 endIndex . comment - startIndex . comment
733789 ) ;
734- restoreCallback . callback ( node , targetTokens , targetComments , {
735- getScope,
736- getInnermostScope,
737- registerNodeToScope,
738- scopeManager : result . scopeManager ! ,
739- visitorKeys : result . visitorKeys ,
740- } ) ;
790+ restoreCallback . callback (
791+ node ,
792+ targetTokens ,
793+ targetComments ,
794+ callbackOption
795+ ) ;
741796
742797 processedTokens . push ( ...targetTokens ) ;
743798 processedComments . push ( ...targetComments ) ;
@@ -750,33 +805,22 @@ export class ScriptLetContext {
750805
751806 result . ast . tokens = processedTokens ;
752807 result . ast . comments = processedComments ;
753- postprocessList . forEach ( ( p ) => p ( ) ) ;
754-
755- // Helpers
756- /** Get scope */
757- function getScope ( node : ESTree . Node ) {
758- return getScopeFromNode ( result . scopeManager ! , node ) ;
759- }
760-
761- /** Get innermost scope */
762- function getInnermostScope ( node : ESTree . Node ) {
763- return getInnermostScopeFromNode ( result . scopeManager ! , node ) ;
764- }
765-
766- /** Register node to scope */
767- function registerNodeToScope ( node : any , scope : Scope ) : void {
768- // If we replace the `scope.block` at this time,
769- // the scope restore calculation will not work, so we will replace the `scope.block` later.
770- postprocessList . push ( ( ) => {
771- scope . block = node ;
772- } ) ;
808+ }
773809
774- const scopes = nodeToScope . get ( node ) ;
775- if ( scopes ) {
776- scopes . push ( scope ) ;
777- } else {
778- nodeToScope . set ( node , [ scope ] ) ;
779- }
810+ /**
811+ * Restore program node
812+ */
813+ private restoreProgram (
814+ result : ESLintExtendedProgram ,
815+ callbackOption : ScriptLetRestoreCallbackOption
816+ ) : void {
817+ for ( const callback of this . programRestoreCallbacks ) {
818+ callback (
819+ result . ast ,
820+ result . ast . tokens ,
821+ result . ast . comments ,
822+ callbackOption
823+ ) ;
780824 }
781825 }
782826
0 commit comments