Home / Class/ Visitor Class — react Architecture

Visitor Class — react Architecture

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

Entity Profile

Dependency Diagram

graph TD
  e1b0b28f_0a55_63f6_4092_ecaa28a68023["Visitor"]
  a4218b71_262a_ca43_13e7_98514ab3bd4e["PruneInitializationDependencies.ts"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|defined in| a4218b71_262a_ca43_13e7_98514ab3bd4e
  7101e807_84e8_9a08_565f_6c3b681b3943["constructor()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| 7101e807_84e8_9a08_565f_6c3b681b3943
  a7715c66_6895_207b_7553_143987538107["join()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| a7715c66_6895_207b_7553_143987538107
  f96e5378_6102_8b6c_e959_c4faf30befb9["isCreateOnlyHook()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| f96e5378_6102_8b6c_e959_c4faf30befb9
  1f4c3be2_48dd_6671_2858_1a62b1026c5e["visitPlace()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| 1f4c3be2_48dd_6671_2858_1a62b1026c5e
  931349c6_5d77_346b_a8bf_6cb1066a37f8["visitBlock()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| 931349c6_5d77_346b_a8bf_6cb1066a37f8
  f6a9b2b0_32d7_a868_9762_f6fe2182d587["visitInstruction()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| f6a9b2b0_32d7_a868_9762_f6fe2182d587
  eb5a8bba_982f_3e93_34ad_57191041e6d7["visitScope()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| eb5a8bba_982f_3e93_34ad_57191041e6d7
  b605277e_3ebb_6ab0_460f_e933821d987f["visitTerminal()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| b605277e_3ebb_6ab0_460f_e933821d987f
  fc8d50b3_237c_c9d9_9028_4234c34df291["visitReactiveFunctionValue()"]
  e1b0b28f_0a55_63f6_4092_ecaa28a68023 -->|method| fc8d50b3_237c_c9d9_9028_4234c34df291

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneInitializationDependencies.ts lines 65–212

class Visitor extends ReactiveFunctionVisitor<CreateUpdate> {
  map: KindMap = new Map();
  aliases: DisjointSet<IdentifierId>;
  paths: Map<IdentifierId, Map<PropertyLiteral, IdentifierId>>;
  env: Environment;

  constructor(
    env: Environment,
    aliases: DisjointSet<IdentifierId>,
    paths: Map<IdentifierId, Map<PropertyLiteral, IdentifierId>>,
  ) {
    super();
    this.aliases = aliases;
    this.paths = paths;
    this.env = env;
  }

  join(values: Array<CreateUpdate>): CreateUpdate {
    function join2(l: CreateUpdate, r: CreateUpdate): CreateUpdate {
      if (l === 'Update' || r === 'Update') {
        return 'Update';
      } else if (l === 'Create' || r === 'Create') {
        return 'Create';
      } else if (l === 'Unknown' || r === 'Unknown') {
        return 'Unknown';
      }
      assertExhaustive(r, `Unhandled variable kind ${r}`);
    }
    return values.reduce(join2, 'Unknown');
  }

  isCreateOnlyHook(id: Identifier): boolean {
    return isUseStateType(id) || isUseRefType(id);
  }

  override visitPlace(
    _: InstructionId,
    place: Place,
    state: CreateUpdate,
  ): void {
    this.map.set(
      place.identifier.id,
      this.join([state, this.map.get(place.identifier.id) ?? 'Unknown']),
    );
  }

  override visitBlock(block: ReactiveBlock, state: CreateUpdate): void {
    super.visitBlock([...block].reverse(), state);
  }

  override visitInstruction(instruction: ReactiveInstruction): void {
    const state = this.join(
      [...eachInstructionLValue(instruction)].map(
        operand => this.map.get(operand.identifier.id) ?? 'Unknown',
      ),
    );

    const visitCallOrMethodNonArgs = (): void => {
      switch (instruction.value.kind) {
        case 'CallExpression': {
          this.visitPlace(instruction.id, instruction.value.callee, state);
          break;
        }
        case 'MethodCall': {
          this.visitPlace(instruction.id, instruction.value.property, state);
          this.visitPlace(instruction.id, instruction.value.receiver, state);
          break;
        }
      }
    };

    const isHook = (): boolean => {
      let callee = null;
      switch (instruction.value.kind) {
        case 'CallExpression': {
          callee = instruction.value.callee.identifier;
          break;
        }
        case 'MethodCall': {
          callee = instruction.value.property.identifier;
          break;

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/PruneInitializationDependencies.ts.
Where is Visitor defined?
Visitor is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneInitializationDependencies.ts at line 65.

Analyze Your Own Codebase

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

Try Supermodel Free