Home / Class/ FlowTypeEnv Class — react Architecture

FlowTypeEnv Class — react Architecture

Architecture documentation for the FlowTypeEnv class in Types.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  62703e39_4962_1fce_0d63_004c5aacd635["FlowTypeEnv"]
  912702ee_f06e_77d9_d237_a41c50607cdf["Types.ts"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|defined in| 912702ee_f06e_77d9_d237_a41c50607cdf
  e3d1f312_c52c_8d71_91e3_cec13c53ceb5["init()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| e3d1f312_c52c_8d71_91e3_cec13c53ceb5
  958dc543_2eac_8e1e_eeab_5ee0f4c31e9a["setType()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| 958dc543_2eac_8e1e_eeab_5ee0f4c31e9a
  ea542458_3f6c_6a0c_6d3b_af172f10f9a8["getType()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| ea542458_3f6c_6a0c_6d3b_af172f10f9a8
  0c14d2e3_45c6_0de6_ab48_697f22ac7b93["getTypeOrNull()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| 0c14d2e3_45c6_0de6_ab48_697f22ac7b93
  4aa8c0fe_a68b_522b_6b4b_4a13e1cfc33f["getTypeByLoc()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| 4aa8c0fe_a68b_522b_6b4b_4a13e1cfc33f
  a87cedcf_1a73_3cb6_7e6c_404466098c30["nextNominalId()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| a87cedcf_1a73_3cb6_7e6c_404466098c30
  65eb82af_3859_a923_18a7_5d031b060876["nextTypeParameterId()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| 65eb82af_3859_a923_18a7_5d031b060876
  6be2e2fa_5d8b_8fc2_0b53_1961df9ac847["addBinding()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| 6be2e2fa_5d8b_8fc2_0b53_1961df9ac847
  470aaa81_2c29_f3ea_7d2a_5a992750f466["resolveBinding()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| 470aaa81_2c29_f3ea_7d2a_5a992750f466
  794aecad_53b5_3094_457a_59c494ca9d13["pushGeneric()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| 794aecad_53b5_3094_457a_59c494ca9d13
  4903ccf9_19b5_1fcd_ec92_d646b1b5c544["popGeneric()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| 4903ccf9_19b5_1fcd_ec92_d646b1b5c544
  d9145e17_1895_b073_f36a_f2f0e0f597b6["getGeneric()"]
  62703e39_4962_1fce_0d63_004c5aacd635 -->|method| d9145e17_1895_b073_f36a_f2f0e0f597b6

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Flood/Types.ts lines 695–808

export class FlowTypeEnv implements ITypeEnv {
  moduleEnv: Map<string, ResolvedType> = new Map();
  #nextNominalId: number = 0;
  #nextTypeParameterId: number = 0;

  #types: Map<IdentifierId, ResolvedType> = new Map();
  #bindings: Map<t.Identifier, ResolvedType> = new Map();
  #generics: Array<[string, TypeParameter<ResolvedType>]> = [];
  #flowTypes: Map<string, ResolvedType> = new Map();

  init(env: Environment, source: string): void {
    // TODO: use flow-js only for web environments (e.g. playground)
    CompilerError.invariant(env.config.flowTypeProvider != null, {
      reason: 'Expected flowDumpTypes to be defined in environment config',
      loc: GeneratedSource,
    });
    let stdout: any;
    if (source === lastFlowSource) {
      stdout = lastFlowResult;
    } else {
      lastFlowSource = source;
      lastFlowResult = env.config.flowTypeProvider(source);
      stdout = lastFlowResult;
    }
    const flowTypes = buildTypeEnvironment(stdout);
    const resolvedFlowTypes = new Map<string, ResolvedType>();
    for (const [loc, type] of flowTypes) {
      if (typeof loc === 'symbol') continue;
      resolvedFlowTypes.set(loc, convertFlowType(JSON.parse(type), loc));
    }
    // =console.log(resolvedFlowTypes);
    this.#flowTypes = resolvedFlowTypes;
  }

  setType(identifier: Identifier, type: ResolvedType): void {
    if (
      typeof identifier.loc !== 'symbol' &&
      this.#flowTypes.has(serializeLoc(identifier.loc))
    ) {
      return;
    }
    this.#types.set(identifier.id, type);
  }

  getType(identifier: Identifier): ResolvedType {
    const result = this.getTypeOrNull(identifier);
    if (result == null) {
      throw new Error(
        `Type not found for ${identifier.id}, ${typeof identifier.loc === 'symbol' ? 'generated loc' : serializeLoc(identifier.loc)}`,
      );
    }
    return result;
  }

  getTypeOrNull(identifier: Identifier): ResolvedType | null {
    const result = this.#types.get(identifier.id) ?? null;
    if (result == null && typeof identifier.loc !== 'symbol') {
      const flowType = this.#flowTypes.get(serializeLoc(identifier.loc));
      return flowType ?? null;
    }
    return result;
  }

  getTypeByLoc(loc: SourceLocation): ResolvedType | null {
    if (typeof loc === 'symbol') {
      return null;
    }
    const flowType = this.#flowTypes.get(serializeLoc(loc));
    return flowType ?? null;
  }

  nextNominalId(): NominalId {
    return makeNominalId(this.#nextNominalId++);
  }

  nextTypeParameterId(): TypeParameterId {
    return makeTypeParameterId(this.#nextTypeParameterId++);
  }

  addBinding(bindingIdentifier: t.Identifier, type: ResolvedType): void {
    this.#bindings.set(bindingIdentifier, type);

Domain

Frequently Asked Questions

What is the FlowTypeEnv class?
FlowTypeEnv is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Flood/Types.ts.
Where is FlowTypeEnv defined?
FlowTypeEnv is defined in compiler/packages/babel-plugin-react-compiler/src/Flood/Types.ts at line 695.

Analyze Your Own Codebase

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

Try Supermodel Free