MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / MappingsEncoder

Class MappingsEncoder

src/utils/MappingsEncoder.ts:40–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38// Buffers completed lines of segments and VLQ-encodes them in batches, so producing
39// an encoded map never holds more than ~SEGMENTS_PER_FLUSH decoded segments
40export class MappingsEncoder {
41 declare private out: string
42 declare private pos: number
43 declare private lines: FullSegment[][]
44 declare private buffered: number
45 declare private needsComma: boolean
46 declare private prevGenColumn: number
47 declare private prevSourceIndex: number
48 declare private prevSourceLine: number
49 declare private prevSourceColumn: number
50 declare private prevNameIndex: number
51
52 constructor() {
53 this.out = ''
54 this.pos = 0
55 this.lines = []
56 this.buffered = 0
57 this.needsComma = false
58 this.prevGenColumn = 0
59 this.prevSourceIndex = 0
60 this.prevSourceLine = 0
61 this.prevSourceColumn = 0
62 this.prevNameIndex = 0
63 }
64
65 endLine(segments: FullSegment[]): void {
66 this.lines.push(segments)
67 // count the line itself too, so content dominated by line breaks also drains
68 this.buffered += segments.length + 1
69 if (this.buffered >= SEGMENTS_PER_FLUSH)
70 this.drain(null)
71 }
72
73 segments(segments: FullSegment[]): void {
74 this.drain(segments)
75 }
76
77 finish(segments: FullSegment[]): string {
78 this.drain(segments)
79 this.flush()
80 return this.out
81 }
82
83 private drain(trailing: FullSegment[] | null): void {
84 const lines = this.lines
85 const lineCount = lines.length
86
87 for (let l = 0; l <= lineCount; l++) {
88 const line = l < lineCount ? lines[l] : trailing
89 if (line === null)
90 break
91
92 for (let i = 0; i < line.length; i++) {
93 if (this.pos > FLUSH_THRESHOLD)
94 this.flush()
95 const segment = line[i]
96 if (this.needsComma)
97 scratch[this.pos++] = COMMA

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected