Home / Class/ Transform Class — react Architecture

Transform Class — react Architecture

Architecture documentation for the Transform class in PruneAlwaysInvalidatingScopes.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  d9cfc6c8_2b94_462b_69b8_bedddf5c2c61["Transform"]
  fec8c712_326d_ff8d_0b77_541838e68d7b["PruneAlwaysInvalidatingScopes.ts"]
  d9cfc6c8_2b94_462b_69b8_bedddf5c2c61 -->|defined in| fec8c712_326d_ff8d_0b77_541838e68d7b
  ab01b4ae_5d3f_c6b4_3512_fc828a66df7c["transformInstruction()"]
  d9cfc6c8_2b94_462b_69b8_bedddf5c2c61 -->|method| ab01b4ae_5d3f_c6b4_3512_fc828a66df7c
  1e7c0dce_245f_f126_024d_01c422b5d768["transformScope()"]
  d9cfc6c8_2b94_462b_69b8_bedddf5c2c61 -->|method| 1e7c0dce_245f_f126_024d_01c422b5d768

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneAlwaysInvalidatingScopes.ts lines 31–118

class Transform extends ReactiveFunctionTransform<boolean> {
  alwaysInvalidatingValues: Set<Identifier> = new Set();
  unmemoizedValues: Set<Identifier> = new Set();

  override transformInstruction(
    instruction: ReactiveInstruction,
    withinScope: boolean,
  ): Transformed<ReactiveStatement> {
    this.visitInstruction(instruction, withinScope);

    const {lvalue, value} = instruction;
    switch (value.kind) {
      case 'ArrayExpression':
      case 'ObjectExpression':
      case 'JsxExpression':
      case 'JsxFragment':
      case 'NewExpression': {
        if (lvalue !== null) {
          this.alwaysInvalidatingValues.add(lvalue.identifier);
          if (!withinScope) {
            this.unmemoizedValues.add(lvalue.identifier);
          }
        }
        break;
      }
      case 'StoreLocal': {
        if (this.alwaysInvalidatingValues.has(value.value.identifier)) {
          this.alwaysInvalidatingValues.add(value.lvalue.place.identifier);
        }
        if (this.unmemoizedValues.has(value.value.identifier)) {
          this.unmemoizedValues.add(value.lvalue.place.identifier);
        }
        break;
      }
      case 'LoadLocal': {
        if (
          lvalue !== null &&
          this.alwaysInvalidatingValues.has(value.place.identifier)
        ) {
          this.alwaysInvalidatingValues.add(lvalue.identifier);
        }
        if (
          lvalue !== null &&
          this.unmemoizedValues.has(value.place.identifier)
        ) {
          this.unmemoizedValues.add(lvalue.identifier);
        }
        break;
      }
    }
    return {kind: 'keep'};
  }

  override transformScope(
    scopeBlock: ReactiveScopeBlock,
    _withinScope: boolean,
  ): Transformed<ReactiveStatement> {
    this.visitScope(scopeBlock, true);

    for (const dep of scopeBlock.scope.dependencies) {
      if (this.unmemoizedValues.has(dep.identifier)) {
        /*
         * This scope depends on an always-invalidating value so the scope will always invalidate:
         * prune it to avoid wasted comparisons
         */
        for (const [_, decl] of scopeBlock.scope.declarations) {
          if (this.alwaysInvalidatingValues.has(decl.identifier)) {
            this.unmemoizedValues.add(decl.identifier);
          }
        }
        for (const identifier of scopeBlock.scope.reassignments) {
          if (this.alwaysInvalidatingValues.has(identifier)) {
            this.unmemoizedValues.add(identifier);
          }
        }
        return {
          kind: 'replace',
          value: {
            kind: 'pruned-scope',
            scope: scopeBlock.scope,
            instructions: scopeBlock.instructions,

Domain

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free