Home / File/ ValidateContextVariableLValues.ts — react Source File

ValidateContextVariableLValues.ts — react Source File

Architecture documentation for ValidateContextVariableLValues.ts, a typescript file in the react codebase. 7 imports, 0 dependents.

File typescript BabelCompiler Validation 7 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  e43116d7_42b9_7724_244c_9ae9f04a7964["ValidateContextVariableLValues.ts"]
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  e43116d7_42b9_7724_244c_9ae9f04a7964 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  6976a9ee_9d8e_4f16_3016_495f39aff2fd["PrintHIR.ts"]
  e43116d7_42b9_7724_244c_9ae9f04a7964 --> 6976a9ee_9d8e_4f16_3016_495f39aff2fd
  bf7f1cf7_fc0e_6bac_827c_8d36d98126da["printPlace"]
  e43116d7_42b9_7724_244c_9ae9f04a7964 --> bf7f1cf7_fc0e_6bac_827c_8d36d98126da
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  e43116d7_42b9_7724_244c_9ae9f04a7964 --> 2f3caf55_cc64_415c_55dd_9771ba7dc210
  21b1eb1e_eaf5_5238_3a24_f56eb8ef7278["eachInstructionValueLValue"]
  e43116d7_42b9_7724_244c_9ae9f04a7964 --> 21b1eb1e_eaf5_5238_3a24_f56eb8ef7278
  f5637d03_fd91_50b8_9da7_b2a24c91bab7["eachPatternOperand"]
  e43116d7_42b9_7724_244c_9ae9f04a7964 --> f5637d03_fd91_50b8_9da7_b2a24c91bab7
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  e43116d7_42b9_7724_244c_9ae9f04a7964 --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  style e43116d7_42b9_7724_244c_9ae9f04a7964 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {CompilerError} from '..';
import {HIRFunction, IdentifierId, Place} from '../HIR';
import {printPlace} from '../HIR/PrintHIR';
import {eachInstructionValueLValue, eachPatternOperand} from '../HIR/visitors';

/**
 * Validates that all store/load references to a given named identifier align with the
 * "kind" of that variable (normal variable or context variable). For example, a context
 * variable may not be loaded/stored with regular StoreLocal/LoadLocal/Destructure instructions.
 */
export function validateContextVariableLValues(fn: HIRFunction): void {
  const identifierKinds: IdentifierKinds = new Map();
  validateContextVariableLValuesImpl(fn, identifierKinds);
}

function validateContextVariableLValuesImpl(
  fn: HIRFunction,
  identifierKinds: IdentifierKinds,
): void {
  for (const [, block] of fn.body.blocks) {
    for (const instr of block.instructions) {
      const {value} = instr;
      switch (value.kind) {
        case 'DeclareContext':
        case 'StoreContext': {
          visit(identifierKinds, value.lvalue.place, 'context');
          break;
        }
        case 'LoadContext': {
          visit(identifierKinds, value.place, 'context');
          break;
        }
        case 'StoreLocal':
        case 'DeclareLocal': {
          visit(identifierKinds, value.lvalue.place, 'local');
          break;
        }
        case 'LoadLocal': {
          visit(identifierKinds, value.place, 'local');
          break;
        }
        case 'PostfixUpdate':
        case 'PrefixUpdate': {
          visit(identifierKinds, value.lvalue, 'local');
          break;
        }
        case 'Destructure': {
          for (const lvalue of eachPatternOperand(value.lvalue.pattern)) {
            visit(identifierKinds, lvalue, 'destructure');
          }
          break;
        }
        case 'ObjectMethod':
        case 'FunctionExpression': {
          validateContextVariableLValuesImpl(
            value.loweredFunc.func,
            identifierKinds,
          );
          break;
        }
        default: {
          for (const _ of eachInstructionValueLValue(value)) {
            CompilerError.throwTodo({
              reason:
                'ValidateContextVariableLValues: unhandled instruction variant',
              loc: value.loc,
              description: `Handle '${value.kind} lvalues`,
              suggestions: null,
            });
          }
        }
      }
    }
  }
}

type IdentifierKinds = Map<
  IdentifierId,
  {place: Place; kind: 'local' | 'context' | 'destructure'}
>;

function visit(
  identifiers: IdentifierKinds,
  place: Place,
  kind: 'local' | 'context' | 'destructure',
): void {
  const prev = identifiers.get(place.identifier.id);
  if (prev !== undefined) {
    const wasContext = prev.kind === 'context';
    const isContext = kind === 'context';
    if (wasContext !== isContext) {
      if (prev.kind === 'destructure' || kind === 'destructure') {
        CompilerError.throwTodo({
          reason: `Support destructuring of context variables`,
          loc: kind === 'destructure' ? place.loc : prev.place.loc,
          description: null,
          suggestions: null,
        });
      }

      CompilerError.invariant(false, {
        reason:
          'Expected all references to a variable to be consistently local or context references',
        description: `Identifier ${printPlace(place)} is referenced as a ${kind} variable, but was previously referenced as a ${prev.kind} variable`,
        message: `this is ${prev.kind}`,
        loc: place.loc,
      });
    }
  }
  identifiers.set(place.identifier.id, {place, kind});
}

Domain

Subdomains

Frequently Asked Questions

What does ValidateContextVariableLValues.ts do?
ValidateContextVariableLValues.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in ValidateContextVariableLValues.ts?
ValidateContextVariableLValues.ts defines 3 function(s): validateContextVariableLValues, validateContextVariableLValuesImpl, visit.
What does ValidateContextVariableLValues.ts depend on?
ValidateContextVariableLValues.ts imports 7 module(s): .., PrintHIR.ts, eachInstructionValueLValue, eachPatternOperand, index.ts, printPlace, visitors.ts.
Where is ValidateContextVariableLValues.ts in the architecture?
ValidateContextVariableLValues.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateContextVariableLValues.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/Validation).

Analyze Your Own Codebase

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

Try Supermodel Free