Home / Class/ PromoteTemporaries Class — react Architecture

PromoteTemporaries Class — react Architecture

Architecture documentation for the PromoteTemporaries class in PromoteUsedTemporaries.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  0978ed10_d7af_9dac_ac08_cbaa51b0ff27["PromoteTemporaries"]
  dea6b9e7_3c69_6e86_5d64_ba83c7d43ed6["PromoteUsedTemporaries.ts"]
  0978ed10_d7af_9dac_ac08_cbaa51b0ff27 -->|defined in| dea6b9e7_3c69_6e86_5d64_ba83c7d43ed6
  5e066f27_b301_d78c_0fde_a2fbb735acd8["visitScope()"]
  0978ed10_d7af_9dac_ac08_cbaa51b0ff27 -->|method| 5e066f27_b301_d78c_0fde_a2fbb735acd8
  db29a3b5_069a_8469_4b85_393df6b436e5["visitPrunedScope()"]
  0978ed10_d7af_9dac_ac08_cbaa51b0ff27 -->|method| db29a3b5_069a_8469_4b85_393df6b436e5
  8c39d4a9_3e8d_e9d4_58ed_f9e45399f829["visitParam()"]
  0978ed10_d7af_9dac_ac08_cbaa51b0ff27 -->|method| 8c39d4a9_3e8d_e9d4_58ed_f9e45399f829
  bd23d26f_7617_08ae_6b8f_87d751ee409d["visitValue()"]
  0978ed10_d7af_9dac_ac08_cbaa51b0ff27 -->|method| bd23d26f_7617_08ae_6b8f_87d751ee409d
  a5e11dc5_cbd8_fedf_abbb_98de9dd31fe3["visitReactiveFunctionValue()"]
  0978ed10_d7af_9dac_ac08_cbaa51b0ff27 -->|method| a5e11dc5_cbd8_fedf_abbb_98de9dd31fe3

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PromoteUsedTemporaries.ts lines 33–103

class PromoteTemporaries extends ReactiveFunctionVisitor<State> {
  override visitScope(scopeBlock: ReactiveScopeBlock, state: State): void {
    for (const dep of scopeBlock.scope.dependencies) {
      const {identifier} = dep;
      if (identifier.name == null) {
        promoteIdentifier(identifier, state);
      }
    }
    /*
     * This is technically optional. We could prune ReactiveScopes
     * whose outputs are not used in another computation or return
     * value.
     * Many of our current test fixtures do not return a value, so
     * it is better for now to promote (and memoize) every output.
     */
    for (const [, declaration] of scopeBlock.scope.declarations) {
      if (declaration.identifier.name == null) {
        promoteIdentifier(declaration.identifier, state);
      }
    }
    this.traverseScope(scopeBlock, state);
  }

  override visitPrunedScope(
    scopeBlock: PrunedReactiveScopeBlock,
    state: State,
  ): void {
    for (const [, declaration] of scopeBlock.scope.declarations) {
      if (
        declaration.identifier.name == null &&
        state.pruned.get(declaration.identifier.declarationId)
          ?.usedOutsideScope === true
      ) {
        promoteIdentifier(declaration.identifier, state);
      }
    }
    this.traversePrunedScope(scopeBlock, state);
  }

  override visitParam(place: Place, state: State): void {
    if (place.identifier.name === null) {
      promoteIdentifier(place.identifier, state);
    }
  }

  override visitValue(
    id: InstructionId,
    value: ReactiveValue,
    state: State,
  ): void {
    this.traverseValue(id, value, state);
    if (value.kind === 'FunctionExpression' || value.kind === 'ObjectMethod') {
      this.visitHirFunction(value.loweredFunc.func, state);
    }
  }

  override visitReactiveFunctionValue(
    _id: InstructionId,
    _dependencies: Array<Place>,
    fn: ReactiveFunction,
    state: State,
  ): void {
    for (const operand of fn.params) {
      const place = operand.kind === 'Identifier' ? operand : operand.place;
      if (place.identifier.name === null) {
        promoteIdentifier(place.identifier, state);
      }
    }
    visitReactiveFunction(fn, this, state);
  }
}

Domain

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free