Home / Class/ Visitor Class — react Architecture

Visitor Class — react Architecture

Architecture documentation for the Visitor class in PruneNonReactiveDependencies.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  e2474ba6_08fb_6897_fff8_58e8e2c9fb71["Visitor"]
  48e6a237_50bb_99d9_4055_59aab6c5ede8["PruneNonReactiveDependencies.ts"]
  e2474ba6_08fb_6897_fff8_58e8e2c9fb71 -->|defined in| 48e6a237_50bb_99d9_4055_59aab6c5ede8
  98f294c7_67b7_0dd7_2e06_f6cd2c5c5526["visitInstruction()"]
  e2474ba6_08fb_6897_fff8_58e8e2c9fb71 -->|method| 98f294c7_67b7_0dd7_2e06_f6cd2c5c5526
  d775953f_f8c8_512f_135f_d3c1ff82f8bf["visitScope()"]
  e2474ba6_08fb_6897_fff8_58e8e2c9fb71 -->|method| d775953f_f8c8_512f_135f_d3c1ff82f8bf

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonReactiveDependencies.ts lines 32–119

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);

Domain

Frequently Asked Questions

What is the Visitor class?
Visitor is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonReactiveDependencies.ts.
Where is Visitor defined?
Visitor is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonReactiveDependencies.ts at line 32.

Analyze Your Own Codebase

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

Try Supermodel Free