line-table.ts — tailwindcss Source File
Architecture documentation for line-table.ts, a typescript file in the tailwindcss codebase. 0 imports, 6 dependents.
Entity Profile
Dependency Diagram
graph LR c078df4e_9ae3_a02e_4feb_6380507fddd9["line-table.ts"] 7600373a_3645_efb1_bcbb_e7c3fcb813ac["utils.ts"] 7600373a_3645_efb1_bcbb_e7c3fcb813ac --> c078df4e_9ae3_a02e_4feb_6380507fddd9 9106ed35_a5a8_5f41_7f5e_a6fe5287f68d["ast.ts"] 9106ed35_a5a8_5f41_7f5e_a6fe5287f68d --> c078df4e_9ae3_a02e_4feb_6380507fddd9 8be42ab2_7e92_957a_da93_ffe4c7d161fe["css-parser.ts"] 8be42ab2_7e92_957a_da93_ffe4c7d161fe --> c078df4e_9ae3_a02e_4feb_6380507fddd9 ceff594b_f13e_c59e_3d35_89cdfa84dbea["line-table.bench.ts"] ceff594b_f13e_c59e_3d35_89cdfa84dbea --> c078df4e_9ae3_a02e_4feb_6380507fddd9 d62ece9e_b92f_5732_5e9f_7ca6cb6b9d0d["line-table.test.ts"] d62ece9e_b92f_5732_5e9f_7ca6cb6b9d0d --> c078df4e_9ae3_a02e_4feb_6380507fddd9 2638fb39_6a36_5dfe_4705_961a0b5ff737["source-map.ts"] 2638fb39_6a36_5dfe_4705_961a0b5ff737 --> c078df4e_9ae3_a02e_4feb_6380507fddd9 style c078df4e_9ae3_a02e_4feb_6380507fddd9 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
Imported By
Source
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 PostCSSPlugin domain, AST 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: PostCSSPlugin, subdomain: AST, 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