Home / Function/ gatherCapturedContext() — react Function Reference

gatherCapturedContext() — react Function Reference

Architecture documentation for the gatherCapturedContext() function in BuildHIR.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  de018292_621c_1061_1e34_80ea74ce2c88["gatherCapturedContext()"]
  e04c04d6_37a7_1dc3_7fae_7d07660d0af9["BuildHIR.ts"]
  de018292_621c_1061_1e34_80ea74ce2c88 -->|defined in| e04c04d6_37a7_1dc3_7fae_7d07660d0af9
  535369af_370f_d1a4_e72e_56482eceef72["lowerFunction()"]
  535369af_370f_d1a4_e72e_56482eceef72 -->|calls| de018292_621c_1061_1e34_80ea74ce2c88
  b762aa76_48ec_9fd1_85ca_e15408824cf5["captureScopes()"]
  de018292_621c_1061_1e34_80ea74ce2c88 -->|calls| b762aa76_48ec_9fd1_85ca_e15408824cf5
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  de018292_621c_1061_1e34_80ea74ce2c88 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  style de018292_621c_1061_1e34_80ea74ce2c88 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts lines 4241–4339

function gatherCapturedContext(
  fn: NodePath<
    | t.FunctionExpression
    | t.ArrowFunctionExpression
    | t.FunctionDeclaration
    | t.ObjectMethod
  >,
  componentScope: Scope,
): Map<t.Identifier, SourceLocation> {
  const capturedIds = new Map<t.Identifier, SourceLocation>();

  /*
   * Capture all the scopes from the parent of this function up to and including
   * the component scope.
   */
  const pureScopes: Set<Scope> = captureScopes({
    from: fn.scope.parent,
    to: componentScope,
  });

  function handleMaybeDependency(
    path: NodePath<t.Identifier> | NodePath<t.JSXOpeningElement>,
  ): void {
    // Base context variable to depend on
    let baseIdentifier: NodePath<t.Identifier> | NodePath<t.JSXIdentifier>;
    if (path.isJSXOpeningElement()) {
      const name = path.get('name');
      if (!(name.isJSXMemberExpression() || name.isJSXIdentifier())) {
        // TODO: should JSX namespaced names be handled here as well?
        return;
      }
      let current: NodePath<t.JSXMemberExpression | t.JSXIdentifier> = name;
      while (current.isJSXMemberExpression()) {
        current = current.get('object');
      }
      invariant(
        current.isJSXIdentifier(),
        'Invalid logic in gatherCapturedDeps',
      );
      baseIdentifier = current;
    } else {
      baseIdentifier = path;
    }

    /*
     * Skip dependency path, as we already tried to recursively add it (+ all subexpressions)
     * as a dependency.
     */
    path.skip();

    // Add the base identifier binding as a dependency.
    const binding = baseIdentifier.scope.getBinding(baseIdentifier.node.name);
    if (
      binding !== undefined &&
      pureScopes.has(binding.scope) &&
      !capturedIds.has(binding.identifier)
    ) {
      capturedIds.set(
        binding.identifier,
        path.node.loc ?? binding.identifier.loc ?? GeneratedSource,
      );
    }
  }

  fn.traverse({
    TypeAnnotation(path) {
      path.skip();
    },
    TSTypeAnnotation(path) {
      path.skip();
    },
    TypeAlias(path) {
      path.skip();
    },
    TSTypeAliasDeclaration(path) {
      path.skip();
    },
    Expression(path) {
      if (path.isAssignmentExpression()) {
        /*
         * Babel has a bug where it doesn't visit the LHS of an

Subdomains

Called By

Frequently Asked Questions

What does gatherCapturedContext() do?
gatherCapturedContext() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts.
Where is gatherCapturedContext() defined?
gatherCapturedContext() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts at line 4241.
What does gatherCapturedContext() call?
gatherCapturedContext() calls 2 function(s): captureScopes, invariant.
What calls gatherCapturedContext()?
gatherCapturedContext() is called by 1 function(s): lowerFunction.

Analyze Your Own Codebase

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

Try Supermodel Free