Home / Function/ flattenScopesWithHooksOrUseHIR() — react Function Reference

flattenScopesWithHooksOrUseHIR() — react Function Reference

Architecture documentation for the flattenScopesWithHooksOrUseHIR() function in FlattenScopesWithHooksOrUseHIR.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  c58ff1cf_f3a5_ee82_aeb5_ba958870959d["flattenScopesWithHooksOrUseHIR()"]
  65735af7_dc59_7bd9_83bb_e14ac6df06ed["FlattenScopesWithHooksOrUseHIR.ts"]
  c58ff1cf_f3a5_ee82_aeb5_ba958870959d -->|defined in| 65735af7_dc59_7bd9_83bb_e14ac6df06ed
  c3bc3875_256f_8f5e_7800_2f9c5bae65eb["runWithEnvironment()"]
  c3bc3875_256f_8f5e_7800_2f9c5bae65eb -->|calls| c58ff1cf_f3a5_ee82_aeb5_ba958870959d
  c447b97e_0b8e_b187_e3a8_4be412d6f495["retainWhere()"]
  c58ff1cf_f3a5_ee82_aeb5_ba958870959d -->|calls| c447b97e_0b8e_b187_e3a8_4be412d6f495
  style c58ff1cf_f3a5_ee82_aeb5_ba958870959d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts lines 40–110

export function flattenScopesWithHooksOrUseHIR(fn: HIRFunction): void {
  const activeScopes: Array<{block: BlockId; fallthrough: BlockId}> = [];
  const prune: Array<BlockId> = [];

  for (const [, block] of fn.body.blocks) {
    retainWhere(activeScopes, current => current.fallthrough !== block.id);

    for (const instr of block.instructions) {
      const {value} = instr;
      switch (value.kind) {
        case 'MethodCall':
        case 'CallExpression': {
          const callee =
            value.kind === 'MethodCall' ? value.property : value.callee;
          if (
            getHookKind(fn.env, callee.identifier) != null ||
            isUseOperator(callee.identifier)
          ) {
            prune.push(...activeScopes.map(entry => entry.block));
            activeScopes.length = 0;
          }
        }
      }
    }
    if (block.terminal.kind === 'scope') {
      activeScopes.push({
        block: block.id,
        fallthrough: block.terminal.fallthrough,
      });
    }
  }

  for (const id of prune) {
    const block = fn.body.blocks.get(id)!;
    const terminal = block.terminal;
    CompilerError.invariant(terminal.kind === 'scope', {
      reason: `Expected block to have a scope terminal`,
      description: `Expected block bb${block.id} to end in a scope terminal`,
      loc: terminal.loc,
    });
    const body = fn.body.blocks.get(terminal.block)!;
    if (
      body.instructions.length === 1 &&
      body.terminal.kind === 'goto' &&
      body.terminal.block === terminal.fallthrough
    ) {
      /*
       * This was a scope just for a hook call, which doesn't need memoization.
       * flatten it away. We rely on the PrunedUnusedLabel step to do the actual
       * flattening
       */
      block.terminal = {
        kind: 'label',
        block: terminal.block,
        fallthrough: terminal.fallthrough,
        id: terminal.id,
        loc: terminal.loc,
      } as LabelTerminal;
      continue;
    }

    block.terminal = {
      kind: 'pruned-scope',
      block: terminal.block,
      fallthrough: terminal.fallthrough,
      id: terminal.id,
      loc: terminal.loc,
      scope: terminal.scope,
    } as PrunedScopeTerminal;
  }
}

Domain

Subdomains

Frequently Asked Questions

What does flattenScopesWithHooksOrUseHIR() do?
flattenScopesWithHooksOrUseHIR() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts.
Where is flattenScopesWithHooksOrUseHIR() defined?
flattenScopesWithHooksOrUseHIR() is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts at line 40.
What does flattenScopesWithHooksOrUseHIR() call?
flattenScopesWithHooksOrUseHIR() calls 1 function(s): retainWhere.
What calls flattenScopesWithHooksOrUseHIR()?
flattenScopesWithHooksOrUseHIR() is called by 1 function(s): runWithEnvironment.

Analyze Your Own Codebase

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

Try Supermodel Free