Home / File/ Stack.ts — react Source File

Stack.ts — react Source File

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

File typescript BabelCompiler Validation 3 dependents 3 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  a4f6856b_1fb6_fbaa_dad2_faf7da011133["Stack.ts"]
  76832af2_c0a7_f31c_e448_af5664da4b88["PropagateScopeDependenciesHIR.ts"]
  76832af2_c0a7_f31c_e448_af5664da4b88 --> a4f6856b_1fb6_fbaa_dad2_faf7da011133
  3393f920_76eb_7dd5_b95f_ab92de6cecce["InferEffectDependencies.ts"]
  3393f920_76eb_7dd5_b95f_ab92de6cecce --> a4f6856b_1fb6_fbaa_dad2_faf7da011133
  3b44eada_4dab_f6ef_3a59_8679cbd297bb["PruneHoistedContexts.ts"]
  3b44eada_4dab_f6ef_3a59_8679cbd297bb --> a4f6856b_1fb6_fbaa_dad2_faf7da011133
  style a4f6856b_1fb6_fbaa_dad2_faf7da011133 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.
 */

// An immutable stack data structure supporting O(1) push/pop operations.
export type Stack<T> = Node<T> | Empty<T>;

// Static assertion that Stack<T> is a StackInterface<T>
function _assertStackInterface<T>(stack: Stack<T>): void {
  let _: StackInterface<T> = stack;
}

/*
 * Internal interface to enforce consistent behavior btw Node/Empty variants
 * Note that we export a union rather than the interface so that it is impossible
 * to create additional variants: a Stack should always be exactly a Node or Empty
 * instance.
 */
interface StackInterface<T> {
  push(value: T): StackInterface<T>;

  pop(): StackInterface<T>;

  contains(value: T): boolean;
  find(fn: (value: T) => boolean): boolean;

  each(fn: (value: T) => void): void;

  get value(): T | null;

  print(fn: (node: T) => string): string;
}

export function create<T>(value: T): Stack<T> {
  return new Node(value);
}

export function empty<T>(): Stack<T> {
  return EMPTY as any;
}

class Node<T> implements StackInterface<T> {
  #value: T;
  #next: Stack<T>;

  constructor(value: T, next: Stack<T> = EMPTY as any) {
    this.#value = value;
    this.#next = next;
  }

  push(value: T): Node<T> {
    return new Node(value, this);
  }

  pop(): Stack<T> {
    return this.#next;
  }

  find(fn: (value: T) => boolean): boolean {
    return fn(this.#value) ? true : this.#next.find(fn);
  }

  contains(value: T): boolean {
    return (
      value === this.#value ||
      (this.#next !== null && this.#next.contains(value))
    );
  }
  each(fn: (value: T) => void): void {
    fn(this.#value);
    this.#next.each(fn);
  }

  get value(): T {
    return this.#value;
  }

  print(fn: (node: T) => string): string {
    return fn(this.#value) + this.#next.print(fn);
  }
}

class Empty<T> implements StackInterface<T> {
  push(value: T): Stack<T> {
    return new Node(value as T, this as Stack<T>);
  }
  pop(): Stack<T> {
    return this;
  }

  find(_fn: (value: T) => boolean): boolean {
    return false;
  }
  contains(_value: T): boolean {
    return false;
  }
  each(_fn: (value: T) => void): void {
    return;
  }
  get value(): T | null {
    return null;
  }
  print(_: (node: T) => string): string {
    return '';
  }
}

const EMPTY: Stack<void> = new Empty();

Domain

Subdomains

Classes

Frequently Asked Questions

What does Stack.ts do?
Stack.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 Stack.ts?
Stack.ts defines 3 function(s): _assertStackInterface, create, empty.
What files import Stack.ts?
Stack.ts is imported by 3 file(s): InferEffectDependencies.ts, PropagateScopeDependenciesHIR.ts, PruneHoistedContexts.ts.
Where is Stack.ts in the architecture?
Stack.ts is located at compiler/packages/babel-plugin-react-compiler/src/Utils/Stack.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/Utils).

Analyze Your Own Codebase

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

Try Supermodel Free