Home / Class/ ProgramContext Class — react Architecture

ProgramContext Class — react Architecture

Architecture documentation for the ProgramContext class in Imports.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16["ProgramContext"]
  2a7e50cd_6171_085d_277c_6ced6ddd7148["Imports.ts"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|defined in| 2a7e50cd_6171_085d_277c_6ced6ddd7148
  daf7f40d_4b1a_938b_ef51_4bee7b9c635c["constructor()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| daf7f40d_4b1a_938b_ef51_4bee7b9c635c
  d94198b4_0d10_05c9_4f74_b75616595f53["isHookName()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| d94198b4_0d10_05c9_4f74_b75616595f53
  b5c63c35_0468_f7d9_414d_79eceec19099["hasReference()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| b5c63c35_0468_f7d9_414d_79eceec19099
  be5dd024_ebb7_729f_f653_2158dc13ee4c["newUid()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| be5dd024_ebb7_729f_f653_2158dc13ee4c
  d8a73ffa_b16d_aa4e_3ae4_e3bcc29476e8["addMemoCacheImport()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| d8a73ffa_b16d_aa4e_3ae4_e3bcc29476e8
  dd08b3af_83bb_70f8_8837_798eff915e39["addImportSpecifier()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| dd08b3af_83bb_70f8_8837_798eff915e39
  809b04ec_ddb4_8b92_41e3_4e5ac7fa21e5["addNewReference()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| 809b04ec_ddb4_8b92_41e3_4e5ac7fa21e5
  5aa19ce4_3e31_5200_0219_de4147a588a1["assertGlobalBinding()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| 5aa19ce4_3e31_5200_0219_de4147a588a1
  57b4d928_ecd2_92c9_b55b_ce5e00d0fba4["logEvent()"]
  1bba49a9_dc32_ad8e_e249_ee6b8eb56d16 -->|method| 57b4d928_ecd2_92c9_b55b_ce5e00d0fba4

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts lines 64–222

export class ProgramContext {
  /**
   * Program and environment context
   */
  scope: BabelScope;
  opts: ParsedPluginOptions;
  filename: string | null;
  code: string | null;
  reactRuntimeModule: string;
  suppressions: Array<SuppressionRange>;
  hasModuleScopeOptOut: boolean;

  /*
   * This is a hack to work around what seems to be a Babel bug. Babel doesn't
   * consistently respect the `skip()` function to avoid revisiting a node within
   * a pass, so we use this set to track nodes that we have compiled.
   */
  alreadyCompiled: WeakSet<object> | Set<object> = new (WeakSet ?? Set)();
  // known generated or referenced identifiers in the program
  knownReferencedNames: Set<string> = new Set();
  // generated imports
  imports: Map<string, Map<string, NonLocalImportSpecifier>> = new Map();

  /**
   * Metadata from compilation
   */
  retryErrors: Array<{fn: BabelFn; error: CompilerError}> = [];
  inferredEffectLocations: Set<t.SourceLocation> = new Set();

  constructor({
    program,
    suppressions,
    opts,
    filename,
    code,
    hasModuleScopeOptOut,
  }: ProgramContextOptions) {
    this.scope = program.scope;
    this.opts = opts;
    this.filename = filename;
    this.code = code;
    this.reactRuntimeModule = getReactCompilerRuntimeModule(opts.target);
    this.suppressions = suppressions;
    this.hasModuleScopeOptOut = hasModuleScopeOptOut;
  }

  isHookName(name: string): boolean {
    if (this.opts.environment.hookPattern == null) {
      return isHookName(name);
    } else {
      const match = new RegExp(this.opts.environment.hookPattern).exec(name);
      return (
        match != null && typeof match[1] === 'string' && isHookName(match[1])
      );
    }
  }

  hasReference(name: string): boolean {
    return (
      this.knownReferencedNames.has(name) ||
      this.scope.hasBinding(name) ||
      this.scope.hasGlobal(name) ||
      this.scope.hasReference(name)
    );
  }

  newUid(name: string): string {
    /**
     * Don't call babel's generateUid for known hook imports, as
     * InferTypes might eventually type `HookKind` based on callee naming
     * convention and `_useFoo` is not named as a hook.
     *
     * Local uid generation is susceptible to check-before-use bugs since we're
     * checking for naming conflicts / references long before we actually insert
     * the import. (see similar logic in HIRBuilder:resolveBinding)
     */
    let uid;
    if (this.isHookName(name)) {
      uid = name;
      let i = 0;
      while (this.hasReference(uid)) {

Domain

Frequently Asked Questions

What is the ProgramContext class?
ProgramContext is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts.
Where is ProgramContext defined?
ProgramContext is defined in compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts at line 64.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free