@@ -58,6 +58,7 @@ type ScriptLetRestoreCallbackOption = {
58
58
registerNodeToScope : ( node : any , scope : Scope ) => void ;
59
59
scopeManager : ScopeManager ;
60
60
visitorKeys ?: { [ type : string ] : string [ ] } ;
61
+ addPostProcess : ( callback : ( ) => void ) => void ;
61
62
} ;
62
63
63
64
/**
@@ -130,6 +131,8 @@ export class ScriptLetContext {
130
131
131
132
private readonly restoreCallbacks : RestoreCallback [ ] = [ ] ;
132
133
134
+ private readonly programRestoreCallbacks : ScriptLetRestoreCallback [ ] = [ ] ;
135
+
133
136
private readonly closeScopeCallbacks : ( ( ) => void ) [ ] = [ ] ;
134
137
135
138
private readonly unique = new UniqueIdGenerator ( ) ;
@@ -574,6 +577,10 @@ export class ScriptLetContext {
574
577
this . closeScopeCallbacks . pop ( ) ! ( ) ;
575
578
}
576
579
580
+ public addProgramRestore ( callback : ScriptLetRestoreCallback ) : void {
581
+ this . programRestoreCallbacks . push ( callback ) ;
582
+ }
583
+
577
584
private appendScript (
578
585
text : string ,
579
586
offset : number ,
@@ -631,6 +638,57 @@ export class ScriptLetContext {
631
638
* Restore AST nodes
632
639
*/
633
640
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 {
634
692
let orderedRestoreCallback = this . restoreCallbacks . shift ( ) ;
635
693
if ( ! orderedRestoreCallback ) {
636
694
return ;
@@ -640,8 +698,6 @@ export class ScriptLetContext {
640
698
const processedTokens = [ ] ;
641
699
const comments = result . ast . comments ;
642
700
const processedComments = [ ] ;
643
- const nodeToScope = getNodeToScope ( result . scopeManager ! ) ;
644
- const postprocessList : ( ( ) => void ) [ ] = [ ] ;
645
701
646
702
let tok ;
647
703
while ( ( tok = tokens . shift ( ) ) ) {
@@ -731,13 +787,12 @@ export class ScriptLetContext {
731
787
startIndex . comment ,
732
788
endIndex . comment - startIndex . comment
733
789
) ;
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
+ ) ;
741
796
742
797
processedTokens . push ( ...targetTokens ) ;
743
798
processedComments . push ( ...targetComments ) ;
@@ -750,33 +805,22 @@ export class ScriptLetContext {
750
805
751
806
result . ast . tokens = processedTokens ;
752
807
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
+ }
773
809
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
+ ) ;
780
824
}
781
825
}
782
826
0 commit comments