@@ -18,8 +18,9 @@ import {
18
18
import { getOrInsertWith } from '../Utils/utils' ;
19
19
import { ExternalFunction , isHookName } from '../HIR/Environment' ;
20
20
import { Err , Ok , Result } from '../Utils/Result' ;
21
- import { CompilerReactTarget } from './Options' ;
22
- import { getReactCompilerRuntimeModule } from './Program' ;
21
+ import { LoggerEvent , PluginOptions } from './Options' ;
22
+ import { BabelFn , getReactCompilerRuntimeModule } from './Program' ;
23
+ import { SuppressionRange } from './Suppression' ;
23
24
24
25
export function validateRestrictedImports (
25
26
path : NodePath < t . Program > ,
@@ -52,32 +53,61 @@ export function validateRestrictedImports(
52
53
}
53
54
}
54
55
56
+ type ProgramContextOptions = {
57
+ program : NodePath < t . Program > ;
58
+ suppressions : Array < SuppressionRange > ;
59
+ opts : PluginOptions ;
60
+ filename : string | null ;
61
+ code : string | null ;
62
+ } ;
55
63
export class ProgramContext {
56
- /* Program and environment context */
64
+ /**
65
+ * Program and environment context
66
+ */
57
67
scope : BabelScope ;
68
+ opts : PluginOptions ;
69
+ filename : string | null ;
70
+ code : string | null ;
58
71
reactRuntimeModule : string ;
59
- hookPattern : string | null ;
72
+ suppressions : Array < SuppressionRange > ;
60
73
74
+ /*
75
+ * This is a hack to work around what seems to be a Babel bug. Babel doesn't
76
+ * consistently respect the `skip()` function to avoid revisiting a node within
77
+ * a pass, so we use this set to track nodes that we have compiled.
78
+ */
79
+ alreadyCompiled : WeakSet < object > | Set < object > = new ( WeakSet ?? Set ) ( ) ;
61
80
// known generated or referenced identifiers in the program
62
81
knownReferencedNames : Set < string > = new Set ( ) ;
63
82
// generated imports
64
83
imports : Map < string , Map < string , NonLocalImportSpecifier > > = new Map ( ) ;
65
84
66
- constructor (
67
- program : NodePath < t . Program > ,
68
- reactRuntimeModule : CompilerReactTarget ,
69
- hookPattern : string | null ,
70
- ) {
71
- this . hookPattern = hookPattern ;
85
+ /**
86
+ * Metadata from compilation
87
+ */
88
+ retryErrors : Array < { fn : BabelFn ; error : CompilerError } > = [ ] ;
89
+ inferredEffectLocations : Set < t . SourceLocation > = new Set ( ) ;
90
+
91
+ constructor ( {
92
+ program,
93
+ suppressions,
94
+ opts,
95
+ filename,
96
+ code,
97
+ } : ProgramContextOptions ) {
72
98
this . scope = program . scope ;
73
- this . reactRuntimeModule = getReactCompilerRuntimeModule ( reactRuntimeModule ) ;
99
+ this . opts = opts ;
100
+ this . filename = filename ;
101
+ this . code = code ;
102
+ this . reactRuntimeModule = getReactCompilerRuntimeModule ( opts . target ) ;
103
+ this . suppressions = suppressions ;
74
104
}
75
105
76
106
isHookName ( name : string ) : boolean {
77
- if ( this . hookPattern == null ) {
107
+ if ( this . opts . environment . hookPattern == null ) {
78
108
return isHookName ( name ) ;
79
109
} else {
80
- const match = new RegExp ( this . hookPattern ) . exec ( name ) ;
110
+ const match = new RegExp ( this . opts . environment . hookPattern ) . exec ( name ) ;
81
111
return (
82
112
match != null && typeof match [ 1 ] === 'string' && isHookName ( match [ 1 ] )
83
113
) ;
@@ -179,6 +209,12 @@ export class ProgramContext {
179
209
} ) ;
180
210
return Err ( error ) ;
181
211
}
212
+
213
+ logEvent ( event : LoggerEvent ) : void {
214
+ if ( this . opts . logger != null ) {
215
+ this . opts . logger . logEvent ( this . filename , event ) ;
216
+ }
217
+ }
182
218
}
183
219
184
220
function getExistingImports (
0 commit comments