Home / File/ FlattenReactiveLoopsHIR.ts — react Source File

FlattenReactiveLoopsHIR.ts — react Source File

Architecture documentation for FlattenReactiveLoopsHIR.ts, a typescript file in the react codebase. 4 imports, 1 dependents.

File typescript BabelCompiler Validation 4 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  17d394b5_a235_d9d6_15c9_133daa2aab98["FlattenReactiveLoopsHIR.ts"]
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  17d394b5_a235_d9d6_15c9_133daa2aab98 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  eb9d33f9_42c1_205c_93e6_8e1365a31839["utils.ts"]
  17d394b5_a235_d9d6_15c9_133daa2aab98 --> eb9d33f9_42c1_205c_93e6_8e1365a31839
  d7fde76c_4fd9_feb3_299b_798689f05bc6["assertExhaustive"]
  17d394b5_a235_d9d6_15c9_133daa2aab98 --> d7fde76c_4fd9_feb3_299b_798689f05bc6
  c447b97e_0b8e_b187_e3a8_4be412d6f495["retainWhere"]
  17d394b5_a235_d9d6_15c9_133daa2aab98 --> c447b97e_0b8e_b187_e3a8_4be412d6f495
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> 17d394b5_a235_d9d6_15c9_133daa2aab98
  style 17d394b5_a235_d9d6_15c9_133daa2aab98 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {BlockId, HIRFunction, PrunedScopeTerminal} from '../HIR';
import {assertExhaustive, retainWhere} from '../Utils/utils';

/**
 * Prunes any reactive scopes that are within a loop (for, while, etc). We don't yet
 * support memoization within loops because this would require an extra layer of reconciliation
 * (plus a way to identify values across runs, similar to how we use `key` in JSX for lists).
 * Eventually we may integrate more deeply into the runtime so that we can do a single level
 * of reconciliation, but for now we've found it's sufficient to memoize *around* the loop.
 */
export function flattenReactiveLoopsHIR(fn: HIRFunction): void {
  const activeLoops = Array<BlockId>();
  for (const [, block] of fn.body.blocks) {
    retainWhere(activeLoops, id => id !== block.id);
    const {terminal} = block;
    switch (terminal.kind) {
      case 'do-while':
      case 'for':
      case 'for-in':
      case 'for-of':
      case 'while': {
        activeLoops.push(terminal.fallthrough);
        break;
      }
      case 'scope': {
        if (activeLoops.length !== 0) {
          block.terminal = {
            kind: 'pruned-scope',
            block: terminal.block,
            fallthrough: terminal.fallthrough,
            id: terminal.id,
            loc: terminal.loc,
            scope: terminal.scope,
          } as PrunedScopeTerminal;
        }
        break;
      }
      case 'branch':
      case 'goto':
      case 'if':
      case 'label':
      case 'logical':
      case 'maybe-throw':
      case 'optional':
      case 'pruned-scope':
      case 'return':
      case 'sequence':
      case 'switch':
      case 'ternary':
      case 'throw':
      case 'try':
      case 'unreachable':
      case 'unsupported': {
        break;
      }
      default: {
        assertExhaustive(
          terminal,
          `Unexpected terminal kind \`${(terminal as any).kind}\``,
        );
      }
    }
  }
}

Domain

Subdomains

Frequently Asked Questions

What does FlattenReactiveLoopsHIR.ts do?
FlattenReactiveLoopsHIR.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in FlattenReactiveLoopsHIR.ts?
FlattenReactiveLoopsHIR.ts defines 1 function(s): flattenReactiveLoopsHIR.
What does FlattenReactiveLoopsHIR.ts depend on?
FlattenReactiveLoopsHIR.ts imports 4 module(s): assertExhaustive, index.ts, retainWhere, utils.ts.
What files import FlattenReactiveLoopsHIR.ts?
FlattenReactiveLoopsHIR.ts is imported by 1 file(s): Pipeline.ts.
Where is FlattenReactiveLoopsHIR.ts in the architecture?
FlattenReactiveLoopsHIR.ts is located at compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenReactiveLoopsHIR.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes).

Analyze Your Own Codebase

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

Try Supermodel Free