Home / File/ line-table.ts — tailwindcss Source File

line-table.ts — tailwindcss Source File

Architecture documentation for line-table.ts, a typescript file in the tailwindcss codebase. 0 imports, 6 dependents.

File typescript OxideEngine Scanner 6 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  ac7e86e1_459b_f374_4516_afecc84f2a17["line-table.ts"]
  9ffd1dda_9675_c514_373d_0f4ab4648249["utils.ts"]
  9ffd1dda_9675_c514_373d_0f4ab4648249 --> ac7e86e1_459b_f374_4516_afecc84f2a17
  25f462e7_c718_35c5_7ff1_b1b41cc176bf["ast.ts"]
  25f462e7_c718_35c5_7ff1_b1b41cc176bf --> ac7e86e1_459b_f374_4516_afecc84f2a17
  54851997_7544_bab2_96ab_548e5f5df205["css-parser.ts"]
  54851997_7544_bab2_96ab_548e5f5df205 --> ac7e86e1_459b_f374_4516_afecc84f2a17
  c6e6fc66_f7c6_6475_0a59_270487b701d3["line-table.bench.ts"]
  c6e6fc66_f7c6_6475_0a59_270487b701d3 --> ac7e86e1_459b_f374_4516_afecc84f2a17
  decf24ed_f8e8_0417_e8e2_cfbab3000951["line-table.test.ts"]
  decf24ed_f8e8_0417_e8e2_cfbab3000951 --> ac7e86e1_459b_f374_4516_afecc84f2a17
  94f6a4ae_3b1f_8e71_a096_1f7d17efc49a["source-map.ts"]
  94f6a4ae_3b1f_8e71_a096_1f7d17efc49a --> ac7e86e1_459b_f374_4516_afecc84f2a17
  style ac7e86e1_459b_f374_4516_afecc84f2a17 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Line offset tables are the key to generating our source maps. They allow us
 * to store indexes with our AST nodes and later convert them into positions as
 * when given the source that the indexes refer to.
 */

const LINE_BREAK = 0x0a

/**
 * A position in source code
 *
 * https://tc39.es/ecma426/#sec-position-record-type
 */
export interface Position {
  /** The line number, one-based */
  line: number

  /** The column/character number, one-based */
  column: number
}

/**
 * A table that lets you turn an offset into a line number and column
 */
export interface LineTable {
  /**
   * Find the line/column position in the source code for a given offset
   *
   * Searching for a given offset takes O(log N) time where N is the number of
   * lines of code.
   *
   * @param offset The index for which to find the position
   */
  find(offset: number): Position

  /**
   * Find the most likely byte offset for given a position
   *
   * @param offset The position for which to find the byte offset
   */
  findOffset(pos: Position): number
}

/**
 * Compute a lookup table to allow for efficient line/column lookups based on
 * offsets in the source code.
 *
 * Creating this table is an O(N) operation where N is the length of the source
 */
export function createLineTable(source: string): LineTable {
  let table: number[] = [0]

  // Compute the offsets for the start of each line
  for (let i = 0; i < source.length; i++) {
    if (source.charCodeAt(i) === LINE_BREAK) {
      table.push(i + 1)
    }
  }

  function find(offset: number) {
    // Based on esbuild's binary search for line numbers
    let line = 0
    let count = table.length
    while (count > 0) {
      // `| 0` improves performance (in V8 at least)
      let mid = (count | 0) >> 1
      let i = line + mid
      if (table[i] <= offset) {
        line = i + 1
        count = count - mid - 1
      } else {
        count = mid
      }
    }

    line -= 1

    let column = offset - table[line]

    return {
      line: line + 1,
      column: column,
    }
  }

  function findOffset({ line, column }: Position) {
    line -= 1
    line = Math.min(Math.max(line, 0), table.length - 1)

    let offsetA = table[line]
    let offsetB = table[line + 1] ?? offsetA

    return Math.min(Math.max(offsetA + column, 0), offsetB)
  }

  return {
    find,
    findOffset,
  }
}

Domain

Subdomains

Functions

Frequently Asked Questions

What does line-table.ts do?
line-table.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the OxideEngine domain, Scanner subdomain.
What functions are defined in line-table.ts?
line-table.ts defines 1 function(s): createLineTable.
What files import line-table.ts?
line-table.ts is imported by 6 file(s): ast.ts, css-parser.ts, line-table.bench.ts, line-table.test.ts, source-map.ts, utils.ts.
Where is line-table.ts in the architecture?
line-table.ts is located at packages/tailwindcss/src/source-maps/line-table.ts (domain: OxideEngine, subdomain: Scanner, directory: packages/tailwindcss/src/source-maps).

Analyze Your Own Codebase

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

Try Supermodel Free