@@ -18,8 +18,9 @@ import {
1818import { getOrInsertWith } from '../Utils/utils' ;
1919import { ExternalFunction , isHookName } from '../HIR/Environment' ;
2020import { 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' ;
2324
2425export function validateRestrictedImports (
2526 path : NodePath < t . Program > ,
@@ -52,32 +53,61 @@ export function validateRestrictedImports(
5253 }
5354}
5455
56+ type ProgramContextOptions = {
57+ program : NodePath < t . Program > ;
58+ suppressions : Array < SuppressionRange > ;
59+ opts : PluginOptions ;
60+ filename : string | null ;
61+ code : string | null ;
62+ } ;
5563export class ProgramContext {
56- /* Program and environment context */
64+ /**
65+ * Program and environment context
66+ */
5767 scope : BabelScope ;
68+ opts : PluginOptions ;
69+ filename : string | null ;
70+ code : string | null ;
5871 reactRuntimeModule : string ;
59- hookPattern : string | null ;
72+ suppressions : Array < SuppressionRange > ;
6073
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 ) ( ) ;
6180 // known generated or referenced identifiers in the program
6281 knownReferencedNames : Set < string > = new Set ( ) ;
6382 // generated imports
6483 imports : Map < string , Map < string , NonLocalImportSpecifier > > = new Map ( ) ;
6584
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 ) {
7298 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 ;
74104 }
75105
76106 isHookName ( name : string ) : boolean {
77- if ( this . hookPattern == null ) {
107+ if ( this . opts . environment . hookPattern == null ) {
78108 return isHookName ( name ) ;
79109 } else {
80- const match = new RegExp ( this . hookPattern ) . exec ( name ) ;
110+ const match = new RegExp ( this . opts . environment . hookPattern ) . exec ( name ) ;
81111 return (
82112 match != null && typeof match [ 1 ] === 'string' && isHookName ( match [ 1 ] )
83113 ) ;
@@ -179,6 +209,12 @@ export class ProgramContext {
179209 } ) ;
180210 return Err ( error ) ;
181211 }
212+
213+ logEvent ( event : LoggerEvent ) : void {
214+ if ( this . opts . logger != null ) {
215+ this . opts . logger . logEvent ( this . filename , event ) ;
216+ }
217+ }
182218}
183219
184220function getExistingImports (
0 commit comments