Home / File/ PruneNonReactiveDependencies.ts — react Source File

PruneNonReactiveDependencies.ts — react Source File

Architecture documentation for PruneNonReactiveDependencies.ts, a typescript file in the react codebase. 8 imports, 0 dependents.

File typescript BabelCompiler Validation 8 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  48e6a237_50bb_99d9_4055_59aab6c5ede8["PruneNonReactiveDependencies.ts"]
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8 --> 2f3caf55_cc64_415c_55dd_9771ba7dc210
  f5637d03_fd91_50b8_9da7_b2a24c91bab7["eachPatternOperand"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8 --> f5637d03_fd91_50b8_9da7_b2a24c91bab7
  4021b024_bc48_5236_4c88_793e93a1cbe0["CollectReactiveIdentifiers.ts"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8 --> 4021b024_bc48_5236_4c88_793e93a1cbe0
  9da29979_02d0_1e15_8502_72e24be20690["collectReactiveIdentifiers"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8 --> 9da29979_02d0_1e15_8502_72e24be20690
  21609915_b03a_fd75_b58a_4cb86ef9315b["visitors.ts"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8 --> 21609915_b03a_fd75_b58a_4cb86ef9315b
  171a5d22_bb6b_1c99_05a4_6ad897438a35["ReactiveFunctionVisitor"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8 --> 171a5d22_bb6b_1c99_05a4_6ad897438a35
  2435b5f8_41a6_0458_ba88_4479b965455f["visitReactiveFunction"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8 --> 2435b5f8_41a6_0458_ba88_4479b965455f
  style 48e6a237_50bb_99d9_4055_59aab6c5ede8 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 {
  IdentifierId,
  ReactiveFunction,
  ReactiveInstruction,
  ReactiveScopeBlock,
  isStableType,
} from '../HIR';
import {eachPatternOperand} from '../HIR/visitors';
import {collectReactiveIdentifiers} from './CollectReactiveIdentifiers';
import {ReactiveFunctionVisitor, visitReactiveFunction} from './visitors';

/*
 * PropagateScopeDependencies infers dependencies without considering whether dependencies
 * are actually reactive or not (ie, whether their value can change over time).
 *
 * This pass prunes dependencies that are guaranteed to be non-reactive.
 */
export function pruneNonReactiveDependencies(fn: ReactiveFunction): void {
  const reactiveIdentifiers = collectReactiveIdentifiers(fn);
  visitReactiveFunction(fn, new Visitor(), reactiveIdentifiers);
}

type ReactiveIdentifiers = Set<IdentifierId>;

class Visitor extends ReactiveFunctionVisitor<ReactiveIdentifiers> {
  override visitInstruction(
    instruction: ReactiveInstruction,
    state: ReactiveIdentifiers,
  ): void {
    this.traverseInstruction(instruction, state);

    const {lvalue, value} = instruction;
    switch (value.kind) {
      case 'LoadLocal': {
        if (lvalue !== null && state.has(value.place.identifier.id)) {
          state.add(lvalue.identifier.id);
        }
        break;
      }
      case 'StoreLocal': {
        if (state.has(value.value.identifier.id)) {
          state.add(value.lvalue.place.identifier.id);
          if (lvalue !== null) {
            state.add(lvalue.identifier.id);
          }
        }
        break;
      }
      case 'Destructure': {
        if (state.has(value.value.identifier.id)) {
          for (const lvalue of eachPatternOperand(value.lvalue.pattern)) {
            if (isStableType(lvalue.identifier)) {
              continue;
            }
            state.add(lvalue.identifier.id);
          }
          if (lvalue !== null) {
            state.add(lvalue.identifier.id);
          }
        }
        break;
      }
      case 'PropertyLoad': {
        if (
          lvalue !== null &&
          state.has(value.object.identifier.id) &&
          !isStableType(lvalue.identifier)
        ) {
          state.add(lvalue.identifier.id);
        }
        break;
      }
      case 'ComputedLoad': {
        if (
          lvalue !== null &&
          (state.has(value.object.identifier.id) ||
            state.has(value.property.identifier.id))
        ) {
          state.add(lvalue.identifier.id);
        }
        break;
      }
    }
  }

  override visitScope(
    scopeBlock: ReactiveScopeBlock,
    state: ReactiveIdentifiers,
  ): void {
    this.traverseScope(scopeBlock, state);
    for (const dep of scopeBlock.scope.dependencies) {
      const isReactive = state.has(dep.identifier.id);
      if (!isReactive) {
        scopeBlock.scope.dependencies.delete(dep);
      }
    }
    if (scopeBlock.scope.dependencies.size !== 0) {
      /**
       * If any of a scope's dependencies are reactive, then all of its
       * outputs will re-evaluate whenever those dependencies change.
       * Mark all of the outputs as reactive to reflect the fact that
       * they may change in practice based on a reactive input.
       */
      for (const [, declaration] of scopeBlock.scope.declarations) {
        state.add(declaration.identifier.id);
      }
      for (const reassignment of scopeBlock.scope.reassignments) {
        state.add(reassignment.id);
      }
    }
  }
}

Domain

Subdomains

Classes

Frequently Asked Questions

What does PruneNonReactiveDependencies.ts do?
PruneNonReactiveDependencies.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 PruneNonReactiveDependencies.ts?
PruneNonReactiveDependencies.ts defines 1 function(s): pruneNonReactiveDependencies.
What does PruneNonReactiveDependencies.ts depend on?
PruneNonReactiveDependencies.ts imports 8 module(s): CollectReactiveIdentifiers.ts, ReactiveFunctionVisitor, collectReactiveIdentifiers, eachPatternOperand, index.ts, visitReactiveFunction, visitors.ts, visitors.ts.
Where is PruneNonReactiveDependencies.ts in the architecture?
PruneNonReactiveDependencies.ts is located at compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonReactiveDependencies.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