Home / Class/ Unifier Class — react Architecture

Unifier Class — react Architecture

Architecture documentation for the Unifier class in InferTypes.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  e91596b5_bd35_ace7_a141_7dbab722b96d["Unifier"]
  35147ed6_ce97_e85f_570c_faf2d25f42f4["InferTypes.ts"]
  e91596b5_bd35_ace7_a141_7dbab722b96d -->|defined in| 35147ed6_ce97_e85f_570c_faf2d25f42f4
  8386db79_cd6a_1e57_a45b_178c954d0187["constructor()"]
  e91596b5_bd35_ace7_a141_7dbab722b96d -->|method| 8386db79_cd6a_1e57_a45b_178c954d0187
  1c006d07_fe01_836d_2c9c_015b0c23187c["unify()"]
  e91596b5_bd35_ace7_a141_7dbab722b96d -->|method| 1c006d07_fe01_836d_2c9c_015b0c23187c
  6938eece_f870_22b1_d136_01eb0a26f7b9["bindVariableTo()"]
  e91596b5_bd35_ace7_a141_7dbab722b96d -->|method| 6938eece_f870_22b1_d136_01eb0a26f7b9
  3586dd2f_f748_328e_8e11_2899a16d4f45["tryResolveType()"]
  e91596b5_bd35_ace7_a141_7dbab722b96d -->|method| 3586dd2f_f748_328e_8e11_2899a16d4f45
  aa93304b_656c_028f_c640_ad8396517027["occursCheck()"]
  e91596b5_bd35_ace7_a141_7dbab722b96d -->|method| aa93304b_656c_028f_c640_ad8396517027
  f0603215_bdb0_88a0_8649_8c18b423f04f["get()"]
  e91596b5_bd35_ace7_a141_7dbab722b96d -->|method| f0603215_bdb0_88a0_8649_8c18b423f04f

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts lines 578–834

class Unifier {
  substitutions: Substitution = new Map();
  env: Environment;

  constructor(env: Environment) {
    this.env = env;
  }

  unify(tA: Type, tB: Type): void {
    if (tB.kind === 'Property') {
      if (
        this.env.config.enableTreatRefLikeIdentifiersAsRefs &&
        isRefLikeName(tB)
      ) {
        this.unify(tB.objectType, {
          kind: 'Object',
          shapeId: BuiltInUseRefId,
        });
        this.unify(tA, {
          kind: 'Object',
          shapeId: BuiltInRefValueId,
        });
        return;
      }
      const objectType = this.get(tB.objectType);
      const propertyType =
        tB.propertyName.kind === 'literal'
          ? this.env.getPropertyType(objectType, tB.propertyName.value)
          : this.env.getFallthroughPropertyType(
              objectType,
              tB.propertyName.value,
            );
      if (propertyType !== null) {
        this.unify(tA, propertyType);
      }
      /*
       * We do not error if tB is not a known object or function (even if it
       * is a primitive), since JS implicit conversion to objects
       */
      return;
    }

    if (typeEquals(tA, tB)) {
      return;
    }

    if (tA.kind === 'Type') {
      this.bindVariableTo(tA, tB);
      return;
    }

    if (tB.kind === 'Type') {
      this.bindVariableTo(tB, tA);
      return;
    }

    if (
      tB.kind === 'Function' &&
      tA.kind === 'Function' &&
      tA.isConstructor === tB.isConstructor
    ) {
      this.unify(tA.return, tB.return);
      return;
    }
  }

  bindVariableTo(v: TypeVar, type: Type): void {
    if (type.kind === 'Poly') {
      //  Ignore PolyType, since we don't support polymorphic types correctly.
      return;
    }

    if (this.substitutions.has(v.id)) {
      this.unify(this.substitutions.get(v.id)!, type);
      return;
    }

    if (type.kind === 'Type' && this.substitutions.has(type.id)) {
      this.unify(v, this.substitutions.get(type.id)!);
      return;
    }

Frequently Asked Questions

What is the Unifier class?
Unifier is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts.
Where is Unifier defined?
Unifier is defined in compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts at line 578.

Analyze Your Own Codebase

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

Try Supermodel Free