Home / Function/ inferEffectDependencies() — react Function Reference

inferEffectDependencies() — react Function Reference

Architecture documentation for the inferEffectDependencies() function in InferEffectDependencies.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  3522f83b_2dff_3c9f_920f_3e428eb62b31["inferEffectDependencies()"]
  3393f920_76eb_7dd5_b95f_ab92de6cecce["InferEffectDependencies.ts"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|defined in| 3393f920_76eb_7dd5_b95f_ab92de6cecce
  4663af75_e270_25e3_3415_1230be609d66["getOrInsertWith()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 4663af75_e270_25e3_3415_1230be609d66
  82ed3f4d_ad20_778d_000e_ca92ed3492fd["inferReactiveIdentifiers()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 82ed3f4d_ad20_778d_000e_ca92ed3492fd
  49446ae1_b830_9411_8258_1139d21b314b["createTemporaryPlace()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 49446ae1_b830_9411_8258_1139d21b314b
  ca434b67_031d_2bec_f10c_3b8a790002ed["inferMinimalDependencies()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| ca434b67_031d_2bec_f10c_3b8a790002ed
  af90f530_fefd_4286_d27e_d198190f06bf["truncateDepAtCurrent()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| af90f530_fefd_4286_d27e_d198190f06bf
  30907c46_684f_39cf_12ac_900130b902f4["buildDependencyInstructions()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 30907c46_684f_39cf_12ac_900130b902f4
  9851361f_9c96_24d6_e407_3d4f7245f8ea["collectDepUsages()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 9851361f_9c96_24d6_e407_3d4f7245f8ea
  289382ca_3f09_e2a4_102d_6227edb1872f["rewriteSplices()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 289382ca_3f09_e2a4_102d_6227edb1872f
  0ed953ae_5345_6a11_8ecf_c387365fdf84["reversePostorderBlocks()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 0ed953ae_5345_6a11_8ecf_c387365fdf84
  6d209f7d_6d38_4dee_c66c_76af0358f508["markPredecessors()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 6d209f7d_6d38_4dee_c66c_76af0358f508
  0c09df5a_6c07_11e5_1a6b_9253007463b8["markInstructionIds()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 0c09df5a_6c07_11e5_1a6b_9253007463b8
  04a5191f_97c5_f799_5b35_df80b526da7f["fixScopeAndIdentifierRanges()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 04a5191f_97c5_f799_5b35_df80b526da7f
  53244187_914c_cc90_5880_7bfc1fc9c0bb["push()"]
  3522f83b_2dff_3c9f_920f_3e428eb62b31 -->|calls| 53244187_914c_cc90_5880_7bfc1fc9c0bb
  style 3522f83b_2dff_3c9f_920f_3e428eb62b31 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Inference/InferEffectDependencies.ts lines 67–351

export function inferEffectDependencies(fn: HIRFunction): void {
  const fnExpressions = new Map<
    IdentifierId,
    TInstruction<FunctionExpression>
  >();

  const autodepFnConfigs = new Map<string, Map<string, number>>();
  for (const effectTarget of fn.env.config.inferEffectDependencies!) {
    const moduleTargets = getOrInsertWith(
      autodepFnConfigs,
      effectTarget.function.source,
      () => new Map<string, number>(),
    );
    moduleTargets.set(
      effectTarget.function.importSpecifierName,
      effectTarget.autodepsIndex,
    );
  }
  const autodepFnLoads = new Map<IdentifierId, number>();
  const autodepModuleLoads = new Map<IdentifierId, Map<string, number>>();

  const scopeInfos = new Map<ScopeId, ReactiveScopeDependencies>();

  const loadGlobals = new Set<IdentifierId>();

  /**
   * When inserting LoadLocals, we need to retain the reactivity of the base
   * identifier, as later passes e.g. PruneNonReactiveDeps take the reactivity of
   * a base identifier as the "maximal" reactivity of all its references.
   * Concretely,
   * reactive(Identifier i) = Union_{reference of i}(reactive(reference))
   */
  const reactiveIds = inferReactiveIdentifiers(fn);
  const rewriteBlocks: Array<BasicBlock> = [];

  for (const [, block] of fn.body.blocks) {
    if (block.terminal.kind === 'scope') {
      const scopeBlock = fn.body.blocks.get(block.terminal.block)!;
      if (
        scopeBlock.instructions.length === 1 &&
        scopeBlock.terminal.kind === 'goto' &&
        scopeBlock.terminal.block === block.terminal.fallthrough
      ) {
        scopeInfos.set(
          block.terminal.scope.id,
          block.terminal.scope.dependencies,
        );
      }
    }
    const rewriteInstrs: Array<SpliceInfo> = [];
    for (const instr of block.instructions) {
      const {value, lvalue} = instr;
      if (value.kind === 'FunctionExpression') {
        fnExpressions.set(
          lvalue.identifier.id,
          instr as TInstruction<FunctionExpression>,
        );
      } else if (value.kind === 'PropertyLoad') {
        if (
          typeof value.property === 'string' &&
          autodepModuleLoads.has(value.object.identifier.id)
        ) {
          const moduleTargets = autodepModuleLoads.get(
            value.object.identifier.id,
          )!;
          const propertyName = value.property;
          const numRequiredArgs = moduleTargets.get(propertyName);
          if (numRequiredArgs != null) {
            autodepFnLoads.set(lvalue.identifier.id, numRequiredArgs);
          }
        }
      } else if (value.kind === 'LoadGlobal') {
        loadGlobals.add(lvalue.identifier.id);
        /*
         * TODO: Handle properties on default exports, like
         * import React from 'react';
         * React.useEffect(...);
         */
        if (value.binding.kind === 'ImportNamespace') {
          const moduleTargets = autodepFnConfigs.get(value.binding.module);
          if (moduleTargets != null) {

Domain

Subdomains

Frequently Asked Questions

What does inferEffectDependencies() do?
inferEffectDependencies() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Inference/InferEffectDependencies.ts.
Where is inferEffectDependencies() defined?
inferEffectDependencies() is defined in compiler/packages/babel-plugin-react-compiler/src/Inference/InferEffectDependencies.ts at line 67.
What does inferEffectDependencies() call?
inferEffectDependencies() calls 13 function(s): buildDependencyInstructions, collectDepUsages, createTemporaryPlace, fixScopeAndIdentifierRanges, getOrInsertWith, inferMinimalDependencies, inferReactiveIdentifiers, markInstructionIds, and 5 more.

Analyze Your Own Codebase

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

Try Supermodel Free