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

Class MappingsEncoder

src/utils/MappingsEncoder.ts:42–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected