@remotex-labs/xmap 3.0.1 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,866 @@
1
- export type * from "./services/interfaces/source-service.interface";
2
- export * from "./components/base64.component";
3
- export * from "./services/source.service";
1
+ /**
2
+ * Represents a source map structure used for mapping code within a file to its original source
3
+ * @since 1.0.0
4
+ */
5
+ export interface SourceMapInterface {
6
+ /**
7
+ * The generated file's name that the source map is associated with
8
+ * @since 1.0.0
9
+ */
10
+ file?: string | null;
11
+ /**
12
+ * An array of variable/function names present in the original source
13
+ * @since 1.0.0
14
+ */
15
+ names: Array<string>;
16
+ /**
17
+ * The version of the source map specification (standard is 3)
18
+ * @since 1.0.0
19
+ */
20
+ version: number;
21
+ /**
22
+ * An array of URLs or paths to the original source files
23
+ * @since 1.0.0
24
+ */
25
+ sources: Array<string>;
26
+ /**
27
+ * VLQ encoded string that maps generated code back to original source code
28
+ * @since 1.0.0
29
+ */
30
+ mappings: string;
31
+ /**
32
+ * Root URL for resolving the sources
33
+ * @since 1.0.0
34
+ */
35
+ sourceRoot?: string | null;
36
+ /**
37
+ * Array containing the content of the original source files
38
+ * @since 1.0.0
39
+ */
40
+ sourcesContent?: Array<string>;
41
+ }
42
+ /**
43
+ * Represents a position in source code with mapping information
44
+ * @since 1.0.0
45
+ */
46
+ export interface PositionInterface {
47
+ /**
48
+ * Name of the identifier at this position
49
+ * @since 1.0.0
50
+ */
51
+ name: string | null;
52
+ /**
53
+ * Line number in the original source
54
+ * @since 1.0.0
55
+ */
56
+ line: number;
57
+ /**
58
+ * Column number in the original source
59
+ * @since 1.0.0
60
+ */
61
+ column: number;
62
+ /**
63
+ * Path or URL to the original source file
64
+ * @since 1.0.0
65
+ */
66
+ source: string;
67
+ /**
68
+ * Root URL for resolving the source
69
+ * @since 1.0.0
70
+ */
71
+ sourceRoot: string | null;
72
+ /**
73
+ * Index of the source in the sources array
74
+ * @since 1.0.0
75
+ */
76
+ sourceIndex: number;
77
+ /**
78
+ * Line number in the generated code
79
+ * @since 1.0.0
80
+ */
81
+ generatedLine: number;
82
+ /**
83
+ * Column number in the generated code
84
+ * @since 1.0.0
85
+ */
86
+ generatedColumn: number;
87
+ }
88
+ /**
89
+ * Position in source code including the original source content
90
+ *
91
+ * @see PositionInterface
92
+ * @since 1.0.0
93
+ */
94
+ export interface PositionWithContentInterface extends PositionInterface {
95
+ /**
96
+ * Content of the original source file
97
+ * @since 1.0.0
98
+ */
99
+ sourcesContent: string;
100
+ }
101
+ /**
102
+ * Position in source code including code fragment information
103
+ *
104
+ * @see PositionInterface
105
+ * @since 1.0.0
106
+ */
107
+ export interface PositionWithCodeInterface extends PositionInterface {
108
+ /**
109
+ * Code fragment from the original source
110
+ * @since 1.0.0
111
+ */
112
+ code: string;
113
+ /**
114
+ * Ending line number of the code fragment
115
+ * @since 1.0.0
116
+ */
117
+ endLine: number;
118
+ /**
119
+ * Starting line number of the code fragment
120
+ * @since 1.0.0
121
+ */
122
+ startLine: number;
123
+ }
124
+ /**
125
+ * Options for retrieving source code context
126
+ * @since 1.0.0
127
+ */
128
+ export interface SourceOptionsInterface {
129
+ /**
130
+ * Number of lines to include after the target line
131
+ * @since 1.0.0
132
+ */
133
+ linesAfter?: number;
134
+ /**
135
+ * Number of lines to include before the target line
136
+ * @since 1.0.0
137
+ */
138
+ linesBefore?: number;
139
+ }
140
+ /**
141
+ * Encodes a given number using Variable-Length Quantity (VLQ) encoding. Negative numbers are encoded by
142
+ * converting to a non-negative representation and the continuation bit is used to indicate if more bytes follow.
143
+ *
144
+ * @param value - The number to be encoded
145
+ * @returns The VLQ encoded string represented as Base64 characters
146
+ *
147
+ * @throws Error - If the value cannot be properly encoded
148
+ *
149
+ * @remarks The encoding process shifts the value left by 1 bit to accommodate a sign bit in the least significant position.
150
+ * Negative values have their sign bit set to 1, while positive values have it set to 0.
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * // Encoding a positive number
155
+ * const encoded = encodeVLQ(25); // Returns "Y"
156
+ *
157
+ * // Encoding a negative number
158
+ * const encodedNegative = encodeVLQ(-25); // Returns "Z"
159
+ * ```
160
+ *
161
+ * @see decodeVLQ - For the corresponding decode function
162
+ *
163
+ * @since 1.0.0
164
+ */
165
+ export function encodeVLQ(value: number): string;
166
+ /**
167
+ * Encodes an array of numbers using VLQ encoding by individually encoding each number and concatenating the results.
168
+ *
169
+ * @param values - The array of numbers to be encoded
170
+ * @returns The concatenated VLQ encoded string
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * // Encoding multiple values
175
+ * const encoded = encodeArrayVLQ([1, 0, -5]); // Returns "CAAK"
176
+ * ```
177
+ *
178
+ * @see encodeVLQ - The underlying function used to encode each number
179
+ *
180
+ * @since 1.0.0
181
+ */
182
+ export function encodeArrayVLQ(values: number[]): string;
183
+ /**
184
+ * Decodes a VLQ encoded string back into an array of numbers by processing Base64 characters and continuation bits.
185
+ *
186
+ * @param data - The VLQ encoded string
187
+ * @returns The array of decoded numbers
188
+ *
189
+ * @throws Error - If the string contains invalid Base64 characters
190
+ *
191
+ * @remarks The decoding process examines each Base64 character,
192
+ * checking for continuation bits and progressively building up numeric values.
193
+ * The sign bit is extracted from the least significant position
194
+ * to determine if the original number was negative.
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * // Decoding a simple VLQ string
199
+ * const decoded = decodeVLQ("Y"); // Returns [25]
200
+ *
201
+ * // Decoding multiple values
202
+ * const multiDecoded = decodeVLQ("CAAK"); // Returns [1, 0, -5]
203
+ * ```
204
+ *
205
+ * @see encodeVLQ - For the corresponding encode function
206
+ *
207
+ * @since 1.0.0
208
+ */
209
+ export function decodeVLQ(data: string): number[];
210
+ /**
211
+ * Represents a source map mapping segment that links positions between original and generated code.
212
+ *
213
+ * @interface SegmentInterface
214
+ *
215
+ * @property line - Original line number in the source file
216
+ * @property column - Original column number in the source file
217
+ * @property nameIndex - Index of the symbol name in the source map's names array (null if no associated name)
218
+ * @property sourceIndex - Index of the source file in the source map's sources array
219
+ * @property generatedLine - Line number in the generated output code
220
+ * @property generatedColumn - Column number in the generated output code
221
+ *
222
+ * @remarks
223
+ * These segments form the core data structure of source maps, providing the necessary
224
+ * information to trace locations between original source code and generated output code.
225
+ * Each segment represents a single mapping point in the transformation process.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * const segment: SegmentInterface = {
230
+ * line: 42,
231
+ * column: 10,
232
+ * nameIndex: 5,
233
+ * sourceIndex: 0,
234
+ * generatedLine: 100,
235
+ * generatedColumn: 15
236
+ * };
237
+ * ```
238
+ *
239
+ * @since 1.0.0
240
+ */
241
+ export interface SegmentInterface {
242
+ line: number;
243
+ column: number;
244
+ nameIndex: number | null;
245
+ sourceIndex: number;
246
+ generatedLine: number;
247
+ generatedColumn: number;
248
+ }
249
+ /**
250
+ * A specialized segment interface used during source map offset calculations.
251
+ *
252
+ * @property nameIndex - Index of the symbol name in the source map's names array (always numeric)
253
+ *
254
+ * @remarks
255
+ * Unlike the base SegmentInterface where nameIndex can be null,
256
+ * in offset calculations this value must always be a concrete numeric index.
257
+ * This specialized interface ensures type safety during mapping offset operations.
258
+ *
259
+ * @example
260
+ * ```ts
261
+ * const offsetSegment: SegmentOffsetInterface = {
262
+ * line: 42,
263
+ * column: 10,
264
+ * nameIndex: 5, // Must be a number, not null
265
+ * sourceIndex: 0,
266
+ * generatedLine: 100,
267
+ * generatedColumn: 15
268
+ * };
269
+ * ```
270
+ *
271
+ * @see SegmentInterface
272
+ *
273
+ * @since 1.0.0
274
+ */
275
+ export interface SegmentOffsetInterface extends SegmentInterface {
276
+ nameIndex: number;
277
+ }
278
+ /**
279
+ * Determines the matching behavior when searching for segments in a source map.
280
+ *
281
+ * @property BOUND - No directional preference; returns the first matching segment found
282
+ * @property LOWER_BOUND - Prefers segments with column values lower than or equal to the target
283
+ * @property UPPER_BOUND - Prefers segments with column values greater than or equal to the target
284
+ *
285
+ * @remarks
286
+ * The bias affects how segment lookups behave when an exact position match cannot be found.
287
+ * This provides flexibility in determining which nearby mapping should be preferred when
288
+ * working with source map data that might not have exact matches for every position.
289
+ *
290
+ * @example
291
+ * ```ts
292
+ * // When searching for a position not exactly in the map:
293
+ * findSegmentForPosition(line, column, Bias.LOWER_BOUND); // Prefers the position just before
294
+ * findSegmentForPosition(line, column, Bias.UPPER_BOUND); // Prefers the position just after
295
+ * ```
296
+ *
297
+ * @since 1.0.0
298
+ */
299
+ export const enum Bias {
300
+ BOUND = 0,
301
+ LOWER_BOUND = 1,
302
+ UPPER_BOUND = 2
303
+ }
304
+ /**
305
+ * Represents a collection of mapping segments for a single line in generated code.
306
+ *
307
+ * @remarks
308
+ * A frame contains all the segments that map to a particular line of generated code.
309
+ * Each segment within the frame provides mapping information for a specific range
310
+ * of columns within that line.
311
+ *
312
+ * @example
313
+ * ```ts
314
+ * const lineFrame: FrameType = [
315
+ * { line: 10, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 5, generatedColumn: 0 },
316
+ * { line: 10, column: 5, nameIndex: 3, sourceIndex: 0, generatedLine: 5, generatedColumn: 10 }
317
+ * ];
318
+ * ```
319
+ *
320
+ * @see SegmentInterface
321
+ *
322
+ * @since 1.0.0
323
+ */
324
+ export type FrameType = Array<SegmentInterface>;
325
+ /**
326
+ * Represents the complete mapping structure of a source map.
327
+ *
328
+ * @remarks
329
+ * A source map contains an array where each index corresponds to a line in the generated code.
330
+ * Each entry is either:
331
+ * - A frame (array of segments) containing mappings for that line
332
+ * - Null, indicating the line has no mappings (represented by a semicolon in the source map)
333
+ *
334
+ * The array index corresponds directly to the line number in generated code (0-based).
335
+ *
336
+ * @example
337
+ * ```ts
338
+ * const sourceMap: MapType = [
339
+ * [{ line: 1, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 0, generatedColumn: 0 }], // Line 0
340
+ * null, // Line 1 has no mappings
341
+ * [{ line: 2, column: 0, nameIndex: 1, sourceIndex: 0, generatedLine: 2, generatedColumn: 0 }] // Line 2
342
+ * ];
343
+ * ```
344
+ *
345
+ * @see FrameType
346
+ * @see SegmentInterface
347
+ *
348
+ * @since 1.0.0
349
+ */
350
+ export type MapType = Array<null | FrameType>;
351
+ /**
352
+ * Provides functionality for encoding and decoding source map mappings.
353
+ *
354
+ * The MappingProvider class handles the conversion between various mapping representations:
355
+ * - String format (VLQ-encoded mappings)
356
+ * - Structured array format (MapType)
357
+ * - Internal structured representation
358
+ *
359
+ * It also provides methods to query and retrieve source map segments based on
360
+ * generated or original source positions.
361
+ *
362
+ * @example
363
+ * ```ts
364
+ * // Create from VLQ-encoded mapping string
365
+ * const provider = new MappingProvider(mappingString);
366
+ *
367
+ * // Get a segment by generated position
368
+ * const segment = provider.getSegment(10, 15);
369
+ *
370
+ * // Convert back to mapping string
371
+ * const encoded = provider.encode();
372
+ * ```
373
+ *
374
+ * @since 1.0.0
375
+ */
376
+ export class MappingProvider {
377
+ private mapping;
378
+ /**
379
+ * Creates a new MappingProvider instance.
380
+ *
381
+ * @param mapping - Source map mapping data in one of three formats:
382
+ * - VLQ-encoded string
383
+ * - Structured array (MapType)
384
+ * - Another MappingProvider instance (copy constructor)
385
+ * @param namesOffset - Optional offset to apply to name indices (default: 0)
386
+ * @param sourceOffset - Optional offset to apply to source indices (default: 0)
387
+ *
388
+ * @remarks
389
+ * The constructor automatically detects the mapping format and decodes it accordingly.
390
+ * When providing offsets, these values will be added to the corresponding indices
391
+ * in the decoded mapping data, which is useful when concatenating multiple source maps.
392
+ *
393
+ * @since 1.0.0
394
+ */
395
+ constructor(mapping: string, namesOffset?: number, sourceOffset?: number);
396
+ constructor(mapping: MapType, namesOffset?: number, sourceOffset?: number);
397
+ constructor(mapping: MappingProvider, namesOffset?: number, sourceOffset?: number);
398
+ /**
399
+ * Encodes the internal mapping representation to a VLQ-encoded mapping string.
400
+ *
401
+ * @returns VLQ-encoded mapping string compatible with the source map format specification
402
+ *
403
+ * @remarks
404
+ * This method converts the internal structured mapping representation into a compact
405
+ * string format using Variable Length Quantity (VLQ) encoding.
406
+ * The resulting string follows the source map v3 format for the 'mappings' field.
407
+ *
408
+ * @see https://sourcemaps.info/spec.html
409
+ *
410
+ * @since 1.0.0
411
+ */
412
+ encode(): string;
413
+ /**
414
+ * Decodes mapping data into the internal representation.
415
+ *
416
+ * @param mapping - Mapping data to decode in one of three formats:
417
+ * - VLQ-encoded string
418
+ * - Structured array (MapType)
419
+ * - Another MappingProvider instance
420
+ * @param namesOffset - Optional offset for name indices (default: 0)
421
+ * @param sourcesOffset - Optional offset for source indices (default: 0)
422
+ *
423
+ * @remarks
424
+ * This method replaces the current internal mapping data with the newly decoded mapping.
425
+ * The format of the input mapping is automatically detected and processed accordingly.
426
+ *
427
+ * @see MapType
428
+ * @see MappingProvider
429
+ *
430
+ * @since 1.0.0
431
+ */
432
+ decode(mapping: MappingProvider | MapType | string, namesOffset?: number, sourcesOffset?: number): void;
433
+ /**
434
+ * Retrieves a segment based on a position in the generated code.
435
+ *
436
+ * @param generatedLine - Line number in generated code (1-based)
437
+ * @param generatedColumn - Column number in generated code (0-based)
438
+ * @param bias - Controls matching behavior when exact position not found:
439
+ * - BOUND: No preference (default)
440
+ * - LOWER_BOUND: Prefer segment with lower column
441
+ * - UPPER_BOUND: Prefer segment with higher column
442
+ * @returns Matching segment or null if not found
443
+ *
444
+ * @remarks
445
+ * Uses binary search to efficiently locate matching segments.
446
+ * When no exact match is found, the bias parameter determines which nearby segment to return.
447
+ *
448
+ * @since 1.0.0
449
+ */
450
+ getSegment(generatedLine: number, generatedColumn: number, bias?: Bias): SegmentInterface | null;
451
+ /**
452
+ * Retrieves a segment based on a position in the original source code.
453
+ *
454
+ * @param line - Line number in original source (1-based)
455
+ * @param column - Column number in original source (0-based)
456
+ * @param sourceIndex - Index of source file in the sources array
457
+ * @param bias - Controls matching behavior when exact position not found:
458
+ * - BOUND: No preference (default)
459
+ * - LOWER_BOUND: Prefer segment with lower column
460
+ * - UPPER_BOUND: Prefer segment with higher column
461
+ * @returns Matching segment or null if not found
462
+ *
463
+ * @remarks
464
+ * Searches across all mapping segments to find those matching the specified original source position.
465
+ * When multiple matches are possible, the bias
466
+ * parameter determines which segment to return.
467
+ *
468
+ * This operation is more expensive than getSegment as it must potentially
469
+ * scan the entire mapping structure.
470
+ *
471
+ * @since 1.0.0
472
+ */
473
+ getOriginalSegment(line: number, column: number, sourceIndex: number, bias?: Bias): SegmentInterface | null;
474
+ /**
475
+ * Initializes a new segment offset object with default values.
476
+ *
477
+ * @param namesOffset - Initial name index offset value (default: 0)
478
+ * @param sourceIndex - Initial source index offset value (default: 0)
479
+ * @returns A new segment offset object with initialized position tracking values
480
+ *
481
+ * @remarks
482
+ * This method creates an object that tracks position data during mapping operations.
483
+ * All position values (line, column, generatedLine, generatedColumn) are initialized to 0,
484
+ * while the nameIndex and sourceIndex can be initialized with custom offsets.
485
+ *
486
+ * @since 1.0.0
487
+ */
488
+ private initPositionOffsets;
489
+ /**
490
+ * Validates the format of an encoded mapping string.
491
+ *
492
+ * @param encodedSourceMap - The encoded source map string to validate
493
+ * @returns `true` if the string contains only valid VLQ mapping characters, otherwise `false`
494
+ *
495
+ * @remarks
496
+ * Checks if the string contains only characters valid in source map mappings:
497
+ * - Base64 characters (a-z, A-Z, 0-9, +, /)
498
+ * - Separators (commas for segments, semicolons for lines)
499
+ *
500
+ * This is a basic format validation and doesn't verify the semantic correctness
501
+ * of the VLQ encoding itself.
502
+ *
503
+ * @since 1.0.0
504
+ */
505
+ private validateMappingString;
506
+ /**
507
+ * Validates that a segment's properties conform to expected types.
508
+ *
509
+ * @param segment - The segment object to validate
510
+ *
511
+ * @remarks
512
+ * Performs the following validations on the segment properties:
513
+ * - line: Must be a finite number
514
+ * - column: Must be a finite number
515
+ * - nameIndex: Must be either null or a finite number
516
+ * - sourceIndex: Must be a finite number
517
+ * - generatedLine: Must be a finite number
518
+ * - generatedColumn: Must be a finite number
519
+ *
520
+ * This validation ensures that segments can be safely used in mapping operations
521
+ * and prevents potential issues with non-numeric or infinite values.
522
+ *
523
+ * @throws Error - When any property of the segment is invalid, with a message
524
+ * indicating which property failed validation and its value
525
+ *
526
+ * @since 1.0.0
527
+ */
528
+ private validateSegment;
529
+ /**
530
+ * Encodes a segment into a VLQ-encoded string based on relative offsets.
531
+ *
532
+ * @param segmentOffset - The current segment offset tracking state
533
+ * @param segmentObject - The segment to encode
534
+ * @returns A VLQ-encoded string representation of the segment
535
+ *
536
+ * @remarks
537
+ * The encoding process:
538
+ * 1. Adjusts line and column values (subtracts 1 to convert from 1-based to 0-based)
539
+ * 2. Calculates relative differences between current values and previous offsets
540
+ * 3. Creates an array with the following components:
541
+ * - generatedColumn difference
542
+ * - sourceIndex difference (0 if unchanged)
543
+ * - line difference
544
+ * - column difference
545
+ * - nameIndex difference (only if nameIndex is present)
546
+ * 4. Updates the segment offset state for the next encoding
547
+ * 5. Returns the array as a VLQ-encoded string
548
+ *
549
+ * This method implements the source map V3 specification's delta encoding scheme
550
+ * where values are stored as differences from previous positions.
551
+ *
552
+ * @since 1.0.0
553
+ */
554
+ private encodeSegment;
555
+ /**
556
+ * Encodes a mapping array into a VLQ-encoded mapping string following the source map V3 spec.
557
+ *
558
+ * @param map - The mapping array to encode, organized by generated lines and segments
559
+ * @returns A complete VLQ-encoded mapping string with line and segment separators
560
+ *
561
+ * @remarks
562
+ * The encoding process:
563
+ * 1. Initializes position offsets to track state across the entire mapping
564
+ * 2. Processes each frame (line) in the mapping array:
565
+ * - Resets generated column offset to 0 at the start of each line
566
+ * - Encodes each segment within the line using relative VLQ encoding
567
+ * - Joins segments with commas (,)
568
+ * 3. Joins lines with semicolons (;)
569
+ *
570
+ * Empty frames are preserved as empty strings in the output to maintain
571
+ * the correct line numbering in the resulting source map.
572
+ *
573
+ * @since 1.0.0
574
+ */
575
+ private encodeMappings;
576
+ /**
577
+ * Converts a VLQ-decoded segment array into a structured segment object.
578
+ *
579
+ * @param segmentOffset - The current positional state tracking offsets
580
+ * @param decodedSegment - Array of VLQ-decoded values representing relative offsets
581
+ * @returns A complete segment object with absolute positions
582
+ *
583
+ * @remarks
584
+ * The decoding process:
585
+ * 1. Extracts position values from the decoded array:
586
+ * - [0]: generatedColumn delta
587
+ * - [1]: sourceIndex delta
588
+ * - [2]: sourceLine delta
589
+ * - [3]: sourceColumn delta
590
+ * - [4]: nameIndex delta (optional)
591
+ * 2. Updates the segmentOffset state by adding each delta
592
+ * 3. Constructs a segment object with absolute positions (adding 1 to convert
593
+ * from 0-based to 1-based coordinates)
594
+ * 4. Handles nameIndex appropriately (null if not present in the input)
595
+ *
596
+ * This method implements the inverse operation of the delta encoding scheme
597
+ * defined in the source map V3 specification.
598
+ *
599
+ * @since 1.0.0
600
+ */
601
+ private decodedSegment;
602
+ /**
603
+ * Decodes a VLQ-encoded mapping string into the internal mapping data structure.
604
+ *
605
+ * @param encodedMap - The VLQ-encoded mapping string from a source map
606
+ * @param namesOffset - Base offset for name indices in the global names array
607
+ * @param sourceOffset - Base offset for source indices in the global sources array
608
+ *
609
+ * @remarks
610
+ * The decoding process:
611
+ * 1. Validates the mapping string format before processing
612
+ * 2. Splits the string into frames using semicolons (;) as line separators
613
+ * 3. Initializes position offsets with the provided name and source offsets
614
+ * 4. For each frame (line):
615
+ * - Adds `null` to the mapping array if the frame is empty
616
+ * - Resets the generated column offset to 0 for each new line
617
+ * - Sets the generated line index using the offset + current index
618
+ * - Splits segments using commas (,) and decodes each segment
619
+ * - Transforms each decoded segment into a segment object
620
+ * 5. Updates the internal mapping array with the decoded data
621
+ *
622
+ * Error handling includes validation checks and descriptive error messages
623
+ * indicating which frame caused a decoding failure.
624
+ *
625
+ * @throws Error - When the mapping string format is invalid or decoding fails
626
+ *
627
+ * @since 1.0.0
628
+ */
629
+ private decodeMappingString;
630
+ /**
631
+ * Decodes a structured mapping array into the internal mapping representation.
632
+ *
633
+ * @param encodedMap - The structured mapping array (array of frames, with each frame being an array of segments)
634
+ * @param namesOffset - Offset to add to each segment's nameIndex (for merging multiple source maps)
635
+ * @param sourceOffset - Offset to add to each segment's sourceIndex (for merging multiple source maps)
636
+ *
637
+ * @remarks
638
+ * The decoding process:
639
+ * 1. Validates that the input is a properly structured array
640
+ * 2. Tracks the current line offset based on the existing mapping length
641
+ * 3. For each frame (line) in the mapping:
642
+ * - Preserves null frames as-is (representing empty lines)
643
+ * - Validates that each frame is an array
644
+ * - For each segment in a frame:
645
+ * - Validates the segment structure
646
+ * - Applies the name and source offsets
647
+ * - Adjusts the generated line index by the line offset
648
+ * - Adds the processed frame to the internal mapping array
649
+ *
650
+ * This method is primarily used when combining multiple source maps or
651
+ * importing mapping data from pre-structured arrays rather than VLQ strings.
652
+ * The offsets enable proper indexing when concatenating multiple mappings.
653
+ *
654
+ * @throws Error - When the input format is invalid or segments don't conform to requirements
655
+ *
656
+ * @since 1.0.0
657
+ */
658
+ private decodeMappingArray;
659
+ }
660
+ /**
661
+ * A service for validating and processing source maps that provides functionality for parsing,
662
+ * position mapping, concatenation, and code snippet extraction.
663
+ *
664
+ * @param source - Source map data (SourceService, SourceMapInterface object, or JSON string)
665
+ * @param file - Optional file name for the generated bundle
666
+ * @returns A new SourceService instance
667
+ *
668
+ * @example
669
+ * ```ts
670
+ * const sourceMapJSON = '{"version": 3, "file": "bundle.js", "sources": ["foo.ts"], "names": [], "mappings": "AAAA"}';
671
+ * const sourceService = new SourceService(sourceMapJSON);
672
+ * console.log(sourceService.file); // 'bundle.js'
673
+ * ```
674
+ *
675
+ * @since 1.0.0
676
+ */
677
+ export class SourceService {
678
+ /**
679
+ * The name of the generated file this source map applies to.
680
+ *
681
+ * @since 1.0.0
682
+ */
683
+ readonly file: string | null;
684
+ /**
685
+ * Provider for accessing and manipulating the base64 VLQ-encoded mappings.
686
+ *
687
+ * @since 1.0.0
688
+ */
689
+ readonly mappings: MappingProvider;
690
+ /**
691
+ * The root URL for resolving relative paths in the source files.
692
+ * @since 1.0.0
693
+ */
694
+ readonly sourceRoot: string | null;
695
+ /**
696
+ * List of symbol names referenced by the mappings.
697
+ * @since 1.0.0
698
+ */
699
+ readonly names: Array<string>;
700
+ /**
701
+ * Array of source file paths.
702
+ * @since 1.0.0
703
+ */
704
+ readonly sources: Array<string>;
705
+ /**
706
+ * Array of source file contents.
707
+ * @since 1.0.0
708
+ */
709
+ readonly sourcesContent: Array<string>;
710
+ /**
711
+ * Creates a new SourceService instance.
712
+ *
713
+ * @param source - Source map data (SourceService, SourceMapInterface object, or JSON string)
714
+ * @param file - Optional file name for the generated bundle
715
+ *
716
+ * @throws Error - When a source map has an invalid format or missing required properties
717
+ *
718
+ * @since 1.0.0
719
+ */
720
+ constructor(source: SourceService);
721
+ constructor(source: SourceMapInterface | string, file?: string | null);
722
+ /**
723
+ * Converts the source map data to a plain object.
724
+ *
725
+ * @returns A SourceMapInterface object representing the current state
726
+ *
727
+ * @example
728
+ * ```ts
729
+ * const mapObject = sourceService.getMapObject();
730
+ * console.log(mapObject.file); // 'bundle.js'
731
+ * ```
732
+ *
733
+ * @since 1.0.0
734
+ */
735
+ getMapObject(): SourceMapInterface;
736
+ /**
737
+ * Concatenates additional source maps into the current instance.
738
+ *
739
+ * @param maps - Source maps to concatenate with the current map
740
+ *
741
+ * @example
742
+ * ```ts
743
+ * sourceService.concat(anotherSourceMap);
744
+ * console.log(sourceService.sources); // Updated source paths
745
+ * ```
746
+ *
747
+ * @throws Error - When no maps are provided
748
+ *
749
+ * @since 1.0.0
750
+ */
751
+ concat(...maps: Array<SourceMapInterface | SourceService>): void;
752
+ /**
753
+ * Creates a new SourceService instance with concatenated source maps.
754
+ *
755
+ * @param maps - Source maps to concatenate with a copy of the current map
756
+ * @returns A new SourceService instance with the combined maps
757
+ *
758
+ * @example
759
+ * ```ts
760
+ * const newService = sourceService.concatNewMap(anotherSourceMap);
761
+ * console.log(newService.sources); // Combined sources array
762
+ * ```
763
+ *
764
+ * @throws Error - When no maps are provided
765
+ *
766
+ * @since 1.0.0
767
+ */
768
+ concatNewMap(...maps: Array<SourceMapInterface | SourceService>): SourceService;
769
+ /**
770
+ * Finds position in generated code based on original source position.
771
+ *
772
+ * @param line - Line number in the original source
773
+ * @param column - Column number in the original source
774
+ * @param sourceIndex - Index or file path of the original source
775
+ * @param bias - Position matching strategy (default: Bias.BOUND)
776
+ * @returns Position information or null if not found
777
+ *
778
+ * @example
779
+ * ```ts
780
+ * const position = sourceService.getPositionByOriginal(1, 10, 'foo.ts');
781
+ * console.log(position?.generatedLine); // Line in generated code
782
+ * ```
783
+ *
784
+ * @since 1.0.0
785
+ */
786
+ getPositionByOriginal(line: number, column: number, sourceIndex: number | string, bias?: Bias): PositionInterface | null;
787
+ /**
788
+ * Finds position in an original source based on generated code position.
789
+ *
790
+ * @param line - Line number in the generated code
791
+ * @param column - Column number in the generated code
792
+ * @param bias - Position matching strategy (default: Bias.BOUND)
793
+ * @returns Position information or null if not found
794
+ *
795
+ * @example
796
+ * ```ts
797
+ * const position = sourceService.getPosition(2, 15);
798
+ * console.log(position?.source); // Original source file
799
+ * ```
800
+ *
801
+ * @since 1.0.0
802
+ */
803
+ getPosition(line: number, column: number, bias?: Bias): PositionInterface | null;
804
+ /**
805
+ * Retrieves position with source content for a location in generated code.
806
+ *
807
+ * @param line - Line number in the generated code
808
+ * @param column - Column number in the generated code
809
+ * @param bias - Position matching strategy (default: Bias.BOUND)
810
+ * @returns Position with content information or null if not found
811
+ *
812
+ * @example
813
+ * ```ts
814
+ * const posWithContent = sourceService.getPositionWithContent(3, 5);
815
+ * console.log(posWithContent?.sourcesContent); // Original source content
816
+ * ```
817
+ *
818
+ * @since 1.0.0
819
+ */
820
+ getPositionWithContent(line: number, column: number, bias?: Bias): PositionWithContentInterface | null;
821
+ /**
822
+ * Retrieves position with a code snippet from the original source.
823
+ *
824
+ * @param line - Line number in the generated code
825
+ * @param column - Column number in the generated code
826
+ * @param bias - Position matching strategy (default: Bias.BOUND)
827
+ * @param options - Configuration for the amount of surrounding lines
828
+ * @returns Position with code snippet or null if not found
829
+ *
830
+ * @example
831
+ * ```ts
832
+ * const posWithCode = sourceService.getPositionWithCode(4, 8, Bias.BOUND, {
833
+ * linesBefore: 2,
834
+ * linesAfter: 2
835
+ * });
836
+ * console.log(posWithCode?.code); // Code snippet with context
837
+ * ```
838
+ *
839
+ * @since 1.0.0
840
+ */
841
+ getPositionWithCode(line: number, column: number, bias?: Bias, options?: SourceOptionsInterface): PositionWithCodeInterface | null;
842
+ /**
843
+ * Serializes the source map to a JSON string.
844
+ *
845
+ * @returns JSON string representation of the source map
846
+ *
847
+ * @example
848
+ * ```ts
849
+ * const jsonString = sourceService.toString();
850
+ * console.log(jsonString); // '{"version":3,"file":"bundle.js",...}'
851
+ * ```
852
+ *
853
+ * @since 1.0.0
854
+ */
855
+ toString(): string;
856
+ /**
857
+ * Validates a source map object has all required properties.
858
+ *
859
+ * @param input - Source map object to validate
860
+ *
861
+ * @throws Error - When required properties are missing
862
+ *
863
+ * @since 1.0.0
864
+ */
865
+ private validateSourceMap;
866
+ }