@remotex-labs/xmap 3.0.3 → 3.0.4

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.
@@ -1,4 +1,989 @@
1
- import * as ts from "typescript";
1
+ import type * as ts from "typescript";
2
+ /**
3
+ * Represents a source map structure used for mapping code within a file to its original source
4
+ * @since 1.0.0
5
+ */
6
+ export interface SourceMapInterface {
7
+ /**
8
+ * The generated file's name that the source map is associated with
9
+ * @since 1.0.0
10
+ */
11
+ file?: string | null;
12
+ /**
13
+ * An array of variable/function names present in the original source
14
+ * @since 1.0.0
15
+ */
16
+ names: Array<string>;
17
+ /**
18
+ * The version of the source map specification (standard is 3)
19
+ * @since 1.0.0
20
+ */
21
+ version: number;
22
+ /**
23
+ * An array of URLs or paths to the original source files
24
+ * @since 1.0.0
25
+ */
26
+ sources: Array<string>;
27
+ /**
28
+ * VLQ encoded string that maps generated code back to original source code
29
+ * @since 1.0.0
30
+ */
31
+ mappings: string;
32
+ /**
33
+ * Root URL for resolving the sources
34
+ * @since 1.0.0
35
+ */
36
+ sourceRoot?: string | null;
37
+ /**
38
+ * Array containing the content of the original source files
39
+ * @since 1.0.0
40
+ */
41
+ sourcesContent?: Array<string>;
42
+ }
43
+ /**
44
+ * Represents a position in source code with mapping information
45
+ * @since 1.0.0
46
+ */
47
+ export interface PositionInterface {
48
+ /**
49
+ * Name of the identifier at this position
50
+ * @since 1.0.0
51
+ */
52
+ name: string | null;
53
+ /**
54
+ * Line number in the original source
55
+ * @since 1.0.0
56
+ */
57
+ line: number;
58
+ /**
59
+ * Column number in the original source
60
+ * @since 1.0.0
61
+ */
62
+ column: number;
63
+ /**
64
+ * Path or URL to the original source file
65
+ * @since 1.0.0
66
+ */
67
+ source: string;
68
+ /**
69
+ * Root URL for resolving the source
70
+ * @since 1.0.0
71
+ */
72
+ sourceRoot: string | null;
73
+ /**
74
+ * Index of the source in the sources array
75
+ * @since 1.0.0
76
+ */
77
+ sourceIndex: number;
78
+ /**
79
+ * Line number in the generated code
80
+ * @since 1.0.0
81
+ */
82
+ generatedLine: number;
83
+ /**
84
+ * Column number in the generated code
85
+ * @since 1.0.0
86
+ */
87
+ generatedColumn: number;
88
+ }
89
+ /**
90
+ * Position in source code including the original source content
91
+ *
92
+ * @see PositionInterface
93
+ * @since 1.0.0
94
+ */
95
+ export interface PositionWithContentInterface extends PositionInterface {
96
+ /**
97
+ * Content of the original source file
98
+ * @since 1.0.0
99
+ */
100
+ sourcesContent: string;
101
+ }
102
+ /**
103
+ * Position in source code including code fragment information
104
+ *
105
+ * @see PositionInterface
106
+ * @since 1.0.0
107
+ */
108
+ export interface PositionWithCodeInterface extends PositionInterface {
109
+ /**
110
+ * Code fragment from the original source
111
+ * @since 1.0.0
112
+ */
113
+ code: string;
114
+ /**
115
+ * Ending line number of the code fragment
116
+ * @since 1.0.0
117
+ */
118
+ endLine: number;
119
+ /**
120
+ * Starting line number of the code fragment
121
+ * @since 1.0.0
122
+ */
123
+ startLine: number;
124
+ }
125
+ /**
126
+ * Options for retrieving source code context
127
+ * @since 1.0.0
128
+ */
129
+ export interface SourceOptionsInterface {
130
+ /**
131
+ * Number of lines to include after the target line
132
+ * @since 1.0.0
133
+ */
134
+ linesAfter?: number;
135
+ /**
136
+ * Number of lines to include before the target line
137
+ * @since 1.0.0
138
+ */
139
+ linesBefore?: number;
140
+ }
141
+ /**
142
+ * Encodes a given number using Variable-Length Quantity (VLQ) encoding. Negative numbers are encoded by
143
+ * converting to a non-negative representation and the continuation bit is used to indicate if more bytes follow.
144
+ *
145
+ * @param value - The number to be encoded
146
+ * @returns The VLQ encoded string represented as Base64 characters
147
+ *
148
+ * @throws Error - If the value cannot be properly encoded
149
+ *
150
+ * @remarks The encoding process shifts the value left by 1 bit to accommodate a sign bit in the least significant position.
151
+ * Negative values have their sign bit set to 1, while positive values have it set to 0.
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * // Encoding a positive number
156
+ * const encoded = encodeVLQ(25); // Returns "Y"
157
+ *
158
+ * // Encoding a negative number
159
+ * const encodedNegative = encodeVLQ(-25); // Returns "Z"
160
+ * ```
161
+ *
162
+ * @see decodeVLQ - For the corresponding decode function
163
+ *
164
+ * @since 1.0.0
165
+ */
166
+ export declare function encodeVLQ(value: number): string;
167
+ /**
168
+ * Encodes an array of numbers using VLQ encoding by individually encoding each number and concatenating the results.
169
+ *
170
+ * @param values - The array of numbers to be encoded
171
+ * @returns The concatenated VLQ encoded string
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * // Encoding multiple values
176
+ * const encoded = encodeArrayVLQ([1, 0, -5]); // Returns "CAAK"
177
+ * ```
178
+ *
179
+ * @see encodeVLQ - The underlying function used to encode each number
180
+ *
181
+ * @since 1.0.0
182
+ */
183
+ export declare function encodeArrayVLQ(values: number[]): string;
184
+ /**
185
+ * Decodes a VLQ encoded string back into an array of numbers by processing Base64 characters and continuation bits.
186
+ *
187
+ * @param data - The VLQ encoded string
188
+ * @returns The array of decoded numbers
189
+ *
190
+ * @throws Error - If the string contains invalid Base64 characters
191
+ *
192
+ * @remarks The decoding process examines each Base64 character,
193
+ * checking for continuation bits and progressively building up numeric values.
194
+ * The sign bit is extracted from the least significant position
195
+ * to determine if the original number was negative.
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * // Decoding a simple VLQ string
200
+ * const decoded = decodeVLQ("Y"); // Returns [25]
201
+ *
202
+ * // Decoding multiple values
203
+ * const multiDecoded = decodeVLQ("CAAK"); // Returns [1, 0, -5]
204
+ * ```
205
+ *
206
+ * @see encodeVLQ - For the corresponding encode function
207
+ *
208
+ * @since 1.0.0
209
+ */
210
+ export declare function decodeVLQ(data: string): number[];
211
+ /**
212
+ * Represents a source map mapping segment that links positions between original and generated code.
213
+ *
214
+ * @interface SegmentInterface
215
+ *
216
+ * @property line - Original line number in the source file
217
+ * @property column - Original column number in the source file
218
+ * @property nameIndex - Index of the symbol name in the source map's names array (null if no associated name)
219
+ * @property sourceIndex - Index of the source file in the source map's sources array
220
+ * @property generatedLine - Line number in the generated output code
221
+ * @property generatedColumn - Column number in the generated output code
222
+ *
223
+ * @remarks
224
+ * These segments form the core data structure of source maps, providing the necessary
225
+ * information to trace locations between original source code and generated output code.
226
+ * Each segment represents a single mapping point in the transformation process.
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * const segment: SegmentInterface = {
231
+ * line: 42,
232
+ * column: 10,
233
+ * nameIndex: 5,
234
+ * sourceIndex: 0,
235
+ * generatedLine: 100,
236
+ * generatedColumn: 15
237
+ * };
238
+ * ```
239
+ *
240
+ * @since 1.0.0
241
+ */
242
+ export interface SegmentInterface {
243
+ line: number;
244
+ column: number;
245
+ nameIndex: number | null;
246
+ sourceIndex: number;
247
+ generatedLine: number;
248
+ generatedColumn: number;
249
+ }
250
+ /**
251
+ * A specialized segment interface used during source map offset calculations.
252
+ *
253
+ * @property nameIndex - Index of the symbol name in the source map's names array (always numeric)
254
+ *
255
+ * @remarks
256
+ * Unlike the base SegmentInterface where nameIndex can be null,
257
+ * in offset calculations this value must always be a concrete numeric index.
258
+ * This specialized interface ensures type safety during mapping offset operations.
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * const offsetSegment: SegmentOffsetInterface = {
263
+ * line: 42,
264
+ * column: 10,
265
+ * nameIndex: 5, // Must be a number, not null
266
+ * sourceIndex: 0,
267
+ * generatedLine: 100,
268
+ * generatedColumn: 15
269
+ * };
270
+ * ```
271
+ *
272
+ * @see SegmentInterface
273
+ *
274
+ * @since 1.0.0
275
+ */
276
+ export interface SegmentOffsetInterface extends SegmentInterface {
277
+ nameIndex: number;
278
+ }
279
+ /**
280
+ * Determines the matching behavior when searching for segments in a source map.
281
+ *
282
+ * @property BOUND - No directional preference; returns the first matching segment found
283
+ * @property LOWER_BOUND - Prefers segments with column values lower than or equal to the target
284
+ * @property UPPER_BOUND - Prefers segments with column values greater than or equal to the target
285
+ *
286
+ * @remarks
287
+ * The bias affects how segment lookups behave when an exact position match cannot be found.
288
+ * This provides flexibility in determining which nearby mapping should be preferred when
289
+ * working with source map data that might not have exact matches for every position.
290
+ *
291
+ * @example
292
+ * ```ts
293
+ * // When searching for a position not exactly in the map:
294
+ * findSegmentForPosition(line, column, Bias.LOWER_BOUND); // Prefers the position just before
295
+ * findSegmentForPosition(line, column, Bias.UPPER_BOUND); // Prefers the position just after
296
+ * ```
297
+ *
298
+ * @since 1.0.0
299
+ */
300
+ export declare const enum Bias {
301
+ BOUND = 0,
302
+ LOWER_BOUND = 1,
303
+ UPPER_BOUND = 2
304
+ }
305
+ /**
306
+ * Represents a collection of mapping segments for a single line in generated code.
307
+ *
308
+ * @remarks
309
+ * A frame contains all the segments that map to a particular line of generated code.
310
+ * Each segment within the frame provides mapping information for a specific range
311
+ * of columns within that line.
312
+ *
313
+ * @example
314
+ * ```ts
315
+ * const lineFrame: FrameType = [
316
+ * { line: 10, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 5, generatedColumn: 0 },
317
+ * { line: 10, column: 5, nameIndex: 3, sourceIndex: 0, generatedLine: 5, generatedColumn: 10 }
318
+ * ];
319
+ * ```
320
+ *
321
+ * @see SegmentInterface
322
+ *
323
+ * @since 1.0.0
324
+ */
325
+ export type FrameType = Array<SegmentInterface>;
326
+ /**
327
+ * Represents the complete mapping structure of a source map.
328
+ *
329
+ * @remarks
330
+ * A source map contains an array where each index corresponds to a line in the generated code.
331
+ * Each entry is either:
332
+ * - A frame (array of segments) containing mappings for that line
333
+ * - Null, indicating the line has no mappings (represented by a semicolon in the source map)
334
+ *
335
+ * The array index corresponds directly to the line number in generated code (0-based).
336
+ *
337
+ * @example
338
+ * ```ts
339
+ * const sourceMap: MapType = [
340
+ * [{ line: 1, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 0, generatedColumn: 0 }], // Line 0
341
+ * null, // Line 1 has no mappings
342
+ * [{ line: 2, column: 0, nameIndex: 1, sourceIndex: 0, generatedLine: 2, generatedColumn: 0 }] // Line 2
343
+ * ];
344
+ * ```
345
+ *
346
+ * @see FrameType
347
+ * @see SegmentInterface
348
+ *
349
+ * @since 1.0.0
350
+ */
351
+ export type MapType = Array<null | FrameType>;
352
+ /**
353
+ * Provides functionality for encoding and decoding source map mappings.
354
+ *
355
+ * The MappingProvider class handles the conversion between various mapping representations:
356
+ * - String format (VLQ-encoded mappings)
357
+ * - Structured array format (MapType)
358
+ * - Internal structured representation
359
+ *
360
+ * It also provides methods to query and retrieve source map segments based on
361
+ * generated or original source positions.
362
+ *
363
+ * @example
364
+ * ```ts
365
+ * // Create from VLQ-encoded mapping string
366
+ * const provider = new MappingProvider(mappingString);
367
+ *
368
+ * // Get a segment by generated position
369
+ * const segment = provider.getSegment(10, 15);
370
+ *
371
+ * // Convert back to mapping string
372
+ * const encoded = provider.encode();
373
+ * ```
374
+ *
375
+ * @since 1.0.0
376
+ */
377
+ export declare class MappingProvider {
378
+ private mapping;
379
+ /**
380
+ * Creates a new MappingProvider instance.
381
+ *
382
+ * @param mapping - Source map mapping data in one of three formats:
383
+ * - VLQ-encoded string
384
+ * - Structured array (MapType)
385
+ * - Another MappingProvider instance (copy constructor)
386
+ * @param namesOffset - Optional offset to apply to name indices (default: 0)
387
+ * @param sourceOffset - Optional offset to apply to source indices (default: 0)
388
+ *
389
+ * @remarks
390
+ * The constructor automatically detects the mapping format and decodes it accordingly.
391
+ * When providing offsets, these values will be added to the corresponding indices
392
+ * in the decoded mapping data, which is useful when concatenating multiple source maps.
393
+ *
394
+ * @since 1.0.0
395
+ */
396
+ constructor(mapping: string, namesOffset?: number, sourceOffset?: number);
397
+ constructor(mapping: MapType, namesOffset?: number, sourceOffset?: number);
398
+ constructor(mapping: MappingProvider, namesOffset?: number, sourceOffset?: number);
399
+ /**
400
+ * Encodes the internal mapping representation to a VLQ-encoded mapping string.
401
+ *
402
+ * @returns VLQ-encoded mapping string compatible with the source map format specification
403
+ *
404
+ * @remarks
405
+ * This method converts the internal structured mapping representation into a compact
406
+ * string format using Variable Length Quantity (VLQ) encoding.
407
+ * The resulting string follows the source map v3 format for the 'mappings' field.
408
+ *
409
+ * @see https://sourcemaps.info/spec.html
410
+ *
411
+ * @since 1.0.0
412
+ */
413
+ encode(): string;
414
+ /**
415
+ * Decodes mapping data into the internal representation.
416
+ *
417
+ * @param mapping - Mapping data to decode in one of three formats:
418
+ * - VLQ-encoded string
419
+ * - Structured array (MapType)
420
+ * - Another MappingProvider instance
421
+ * @param namesOffset - Optional offset for name indices (default: 0)
422
+ * @param sourcesOffset - Optional offset for source indices (default: 0)
423
+ *
424
+ * @remarks
425
+ * This method replaces the current internal mapping data with the newly decoded mapping.
426
+ * The format of the input mapping is automatically detected and processed accordingly.
427
+ *
428
+ * @see MapType
429
+ * @see MappingProvider
430
+ *
431
+ * @since 1.0.0
432
+ */
433
+ decode(mapping: MappingProvider | MapType | string, namesOffset?: number, sourcesOffset?: number): void;
434
+ /**
435
+ * Retrieves a segment based on a position in the generated code.
436
+ *
437
+ * @param generatedLine - Line number in generated code (1-based)
438
+ * @param generatedColumn - Column number in generated code (0-based)
439
+ * @param bias - Controls matching behavior when exact position not found:
440
+ * - BOUND: No preference (default)
441
+ * - LOWER_BOUND: Prefer segment with lower column
442
+ * - UPPER_BOUND: Prefer segment with higher column
443
+ * @returns Matching segment or null if not found
444
+ *
445
+ * @remarks
446
+ * Uses binary search to efficiently locate matching segments.
447
+ * When no exact match is found, the bias parameter determines which nearby segment to return.
448
+ *
449
+ * @since 1.0.0
450
+ */
451
+ getSegment(generatedLine: number, generatedColumn: number, bias?: Bias): SegmentInterface | null;
452
+ /**
453
+ * Retrieves a segment based on a position in the original source code.
454
+ *
455
+ * @param line - Line number in original source (1-based)
456
+ * @param column - Column number in original source (0-based)
457
+ * @param sourceIndex - Index of source file in the sources array
458
+ * @param bias - Controls matching behavior when exact position not found:
459
+ * - BOUND: No preference (default)
460
+ * - LOWER_BOUND: Prefer segment with lower column
461
+ * - UPPER_BOUND: Prefer segment with higher column
462
+ * @returns Matching segment or null if not found
463
+ *
464
+ * @remarks
465
+ * Searches across all mapping segments to find those matching the specified original source position.
466
+ * When multiple matches are possible, the bias
467
+ * parameter determines which segment to return.
468
+ *
469
+ * This operation is more expensive than getSegment as it must potentially
470
+ * scan the entire mapping structure.
471
+ *
472
+ * @since 1.0.0
473
+ */
474
+ getOriginalSegment(line: number, column: number, sourceIndex: number, bias?: Bias): SegmentInterface | null;
475
+ /**
476
+ * Initializes a new segment offset object with default values.
477
+ *
478
+ * @param namesOffset - Initial name index offset value (default: 0)
479
+ * @param sourceIndex - Initial source index offset value (default: 0)
480
+ * @returns A new segment offset object with initialized position tracking values
481
+ *
482
+ * @remarks
483
+ * This method creates an object that tracks position data during mapping operations.
484
+ * All position values (line, column, generatedLine, generatedColumn) are initialized to 0,
485
+ * while the nameIndex and sourceIndex can be initialized with custom offsets.
486
+ *
487
+ * @since 1.0.0
488
+ */
489
+ private initPositionOffsets;
490
+ /**
491
+ * Validates the format of an encoded mapping string.
492
+ *
493
+ * @param encodedSourceMap - The encoded source map string to validate
494
+ * @returns `true` if the string contains only valid VLQ mapping characters, otherwise `false`
495
+ *
496
+ * @remarks
497
+ * Checks if the string contains only characters valid in source map mappings:
498
+ * - Base64 characters (a-z, A-Z, 0-9, +, /)
499
+ * - Separators (commas for segments, semicolons for lines)
500
+ *
501
+ * This is a basic format validation and doesn't verify the semantic correctness
502
+ * of the VLQ encoding itself.
503
+ *
504
+ * @since 1.0.0
505
+ */
506
+ private validateMappingString;
507
+ /**
508
+ * Validates that a segment's properties conform to expected types.
509
+ *
510
+ * @param segment - The segment object to validate
511
+ *
512
+ * @remarks
513
+ * Performs the following validations on the segment properties:
514
+ * - line: Must be a finite number
515
+ * - column: Must be a finite number
516
+ * - nameIndex: Must be either null or a finite number
517
+ * - sourceIndex: Must be a finite number
518
+ * - generatedLine: Must be a finite number
519
+ * - generatedColumn: Must be a finite number
520
+ *
521
+ * This validation ensures that segments can be safely used in mapping operations
522
+ * and prevents potential issues with non-numeric or infinite values.
523
+ *
524
+ * @throws Error - When any property of the segment is invalid, with a message
525
+ * indicating which property failed validation and its value
526
+ *
527
+ * @since 1.0.0
528
+ */
529
+ private validateSegment;
530
+ /**
531
+ * Encodes a segment into a VLQ-encoded string based on relative offsets.
532
+ *
533
+ * @param segmentOffset - The current segment offset tracking state
534
+ * @param segmentObject - The segment to encode
535
+ * @returns A VLQ-encoded string representation of the segment
536
+ *
537
+ * @remarks
538
+ * The encoding process:
539
+ * 1. Adjusts line and column values (subtracts 1 to convert from 1-based to 0-based)
540
+ * 2. Calculates relative differences between current values and previous offsets
541
+ * 3. Creates an array with the following components:
542
+ * - generatedColumn difference
543
+ * - sourceIndex difference (0 if unchanged)
544
+ * - line difference
545
+ * - column difference
546
+ * - nameIndex difference (only if nameIndex is present)
547
+ * 4. Updates the segment offset state for the next encoding
548
+ * 5. Returns the array as a VLQ-encoded string
549
+ *
550
+ * This method implements the source map V3 specification's delta encoding scheme
551
+ * where values are stored as differences from previous positions.
552
+ *
553
+ * @since 1.0.0
554
+ */
555
+ private encodeSegment;
556
+ /**
557
+ * Encodes a mapping array into a VLQ-encoded mapping string following the source map V3 spec.
558
+ *
559
+ * @param map - The mapping array to encode, organized by generated lines and segments
560
+ * @returns A complete VLQ-encoded mapping string with line and segment separators
561
+ *
562
+ * @remarks
563
+ * The encoding process:
564
+ * 1. Initializes position offsets to track state across the entire mapping
565
+ * 2. Processes each frame (line) in the mapping array:
566
+ * - Resets generated column offset to 0 at the start of each line
567
+ * - Encodes each segment within the line using relative VLQ encoding
568
+ * - Joins segments with commas (,)
569
+ * 3. Joins lines with semicolons (;)
570
+ *
571
+ * Empty frames are preserved as empty strings in the output to maintain
572
+ * the correct line numbering in the resulting source map.
573
+ *
574
+ * @since 1.0.0
575
+ */
576
+ private encodeMappings;
577
+ /**
578
+ * Converts a VLQ-decoded segment array into a structured segment object.
579
+ *
580
+ * @param segmentOffset - The current positional state tracking offsets
581
+ * @param decodedSegment - Array of VLQ-decoded values representing relative offsets
582
+ * @returns A complete segment object with absolute positions
583
+ *
584
+ * @remarks
585
+ * The decoding process:
586
+ * 1. Extracts position values from the decoded array:
587
+ * - [0]: generatedColumn delta
588
+ * - [1]: sourceIndex delta
589
+ * - [2]: sourceLine delta
590
+ * - [3]: sourceColumn delta
591
+ * - [4]: nameIndex delta (optional)
592
+ * 2. Updates the segmentOffset state by adding each delta
593
+ * 3. Constructs a segment object with absolute positions (adding 1 to convert
594
+ * from 0-based to 1-based coordinates)
595
+ * 4. Handles nameIndex appropriately (null if not present in the input)
596
+ *
597
+ * This method implements the inverse operation of the delta encoding scheme
598
+ * defined in the source map V3 specification.
599
+ *
600
+ * @since 1.0.0
601
+ */
602
+ private decodedSegment;
603
+ /**
604
+ * Decodes a VLQ-encoded mapping string into the internal mapping data structure.
605
+ *
606
+ * @param encodedMap - The VLQ-encoded mapping string from a source map
607
+ * @param namesOffset - Base offset for name indices in the global names array
608
+ * @param sourceOffset - Base offset for source indices in the global sources array
609
+ *
610
+ * @remarks
611
+ * The decoding process:
612
+ * 1. Validates the mapping string format before processing
613
+ * 2. Splits the string into frames using semicolons (;) as line separators
614
+ * 3. Initializes position offsets with the provided name and source offsets
615
+ * 4. For each frame (line):
616
+ * - Adds `null` to the mapping array if the frame is empty
617
+ * - Resets the generated column offset to 0 for each new line
618
+ * - Sets the generated line index using the offset + current index
619
+ * - Splits segments using commas (,) and decodes each segment
620
+ * - Transforms each decoded segment into a segment object
621
+ * 5. Updates the internal mapping array with the decoded data
622
+ *
623
+ * Error handling includes validation checks and descriptive error messages
624
+ * indicating which frame caused a decoding failure.
625
+ *
626
+ * @throws Error - When the mapping string format is invalid or decoding fails
627
+ *
628
+ * @since 1.0.0
629
+ */
630
+ private decodeMappingString;
631
+ /**
632
+ * Decodes a structured mapping array into the internal mapping representation.
633
+ *
634
+ * @param encodedMap - The structured mapping array (array of frames, with each frame being an array of segments)
635
+ * @param namesOffset - Offset to add to each segment's nameIndex (for merging multiple source maps)
636
+ * @param sourceOffset - Offset to add to each segment's sourceIndex (for merging multiple source maps)
637
+ *
638
+ * @remarks
639
+ * The decoding process:
640
+ * 1. Validates that the input is a properly structured array
641
+ * 2. Tracks the current line offset based on the existing mapping length
642
+ * 3. For each frame (line) in the mapping:
643
+ * - Preserves null frames as-is (representing empty lines)
644
+ * - Validates that each frame is an array
645
+ * - For each segment in a frame:
646
+ * - Validates the segment structure
647
+ * - Applies the name and source offsets
648
+ * - Adjusts the generated line index by the line offset
649
+ * - Adds the processed frame to the internal mapping array
650
+ *
651
+ * This method is primarily used when combining multiple source maps or
652
+ * importing mapping data from pre-structured arrays rather than VLQ strings.
653
+ * The offsets enable proper indexing when concatenating multiple mappings.
654
+ *
655
+ * @throws Error - When the input format is invalid or segments don't conform to requirements
656
+ *
657
+ * @since 1.0.0
658
+ */
659
+ private decodeMappingArray;
660
+ }
661
+ /**
662
+ * A service for validating and processing source maps that provides functionality for parsing,
663
+ * position mapping, concatenation, and code snippet extraction.
664
+ *
665
+ * @param source - Source map data (SourceService, SourceMapInterface object, or JSON string)
666
+ * @param file - Optional file name for the generated bundle
667
+ * @returns A new SourceService instance
668
+ *
669
+ * @example
670
+ * ```ts
671
+ * const sourceMapJSON = '{"version": 3, "file": "bundle.js", "sources": ["foo.ts"], "names": [], "mappings": "AAAA"}';
672
+ * const sourceService = new SourceService(sourceMapJSON);
673
+ * console.log(sourceService.file); // 'bundle.js'
674
+ * ```
675
+ *
676
+ * @since 1.0.0
677
+ */
678
+ export declare class SourceService {
679
+ /**
680
+ * The name of the generated file this source map applies to.
681
+ *
682
+ * @since 1.0.0
683
+ */
684
+ readonly file: string | null;
685
+ /**
686
+ * Provider for accessing and manipulating the base64 VLQ-encoded mappings.
687
+ *
688
+ * @since 1.0.0
689
+ */
690
+ readonly mappings: MappingProvider;
691
+ /**
692
+ * The root URL for resolving relative paths in the source files.
693
+ * @since 1.0.0
694
+ */
695
+ readonly sourceRoot: string | null;
696
+ /**
697
+ * List of symbol names referenced by the mappings.
698
+ * @since 1.0.0
699
+ */
700
+ readonly names: Array<string>;
701
+ /**
702
+ * Array of source file paths.
703
+ * @since 1.0.0
704
+ */
705
+ readonly sources: Array<string>;
706
+ /**
707
+ * Array of source file contents.
708
+ * @since 1.0.0
709
+ */
710
+ readonly sourcesContent: Array<string>;
711
+ /**
712
+ * Creates a new SourceService instance.
713
+ *
714
+ * @param source - Source map data (SourceService, SourceMapInterface object, or JSON string)
715
+ * @param file - Optional file name for the generated bundle
716
+ *
717
+ * @throws Error - When a source map has an invalid format or missing required properties
718
+ *
719
+ * @since 1.0.0
720
+ */
721
+ constructor(source: SourceService);
722
+ constructor(source: SourceMapInterface | string, file?: string | null);
723
+ /**
724
+ * Converts the source map data to a plain object.
725
+ *
726
+ * @returns A SourceMapInterface object representing the current state
727
+ *
728
+ * @example
729
+ * ```ts
730
+ * const mapObject = sourceService.getMapObject();
731
+ * console.log(mapObject.file); // 'bundle.js'
732
+ * ```
733
+ *
734
+ * @since 1.0.0
735
+ */
736
+ getMapObject(): SourceMapInterface;
737
+ /**
738
+ * Concatenates additional source maps into the current instance.
739
+ *
740
+ * @param maps - Source maps to concatenate with the current map
741
+ *
742
+ * @example
743
+ * ```ts
744
+ * sourceService.concat(anotherSourceMap);
745
+ * console.log(sourceService.sources); // Updated source paths
746
+ * ```
747
+ *
748
+ * @throws Error - When no maps are provided
749
+ *
750
+ * @since 1.0.0
751
+ */
752
+ concat(...maps: Array<SourceMapInterface | SourceService>): void;
753
+ /**
754
+ * Creates a new SourceService instance with concatenated source maps.
755
+ *
756
+ * @param maps - Source maps to concatenate with a copy of the current map
757
+ * @returns A new SourceService instance with the combined maps
758
+ *
759
+ * @example
760
+ * ```ts
761
+ * const newService = sourceService.concatNewMap(anotherSourceMap);
762
+ * console.log(newService.sources); // Combined sources array
763
+ * ```
764
+ *
765
+ * @throws Error - When no maps are provided
766
+ *
767
+ * @since 1.0.0
768
+ */
769
+ concatNewMap(...maps: Array<SourceMapInterface | SourceService>): SourceService;
770
+ /**
771
+ * Finds position in generated code based on original source position.
772
+ *
773
+ * @param line - Line number in the original source
774
+ * @param column - Column number in the original source
775
+ * @param sourceIndex - Index or file path of the original source
776
+ * @param bias - Position matching strategy (default: Bias.BOUND)
777
+ * @returns Position information or null if not found
778
+ *
779
+ * @example
780
+ * ```ts
781
+ * const position = sourceService.getPositionByOriginal(1, 10, 'foo.ts');
782
+ * console.log(position?.generatedLine); // Line in generated code
783
+ * ```
784
+ *
785
+ * @since 1.0.0
786
+ */
787
+ getPositionByOriginal(line: number, column: number, sourceIndex: number | string, bias?: Bias): PositionInterface | null;
788
+ /**
789
+ * Finds position in an original source based on generated code position.
790
+ *
791
+ * @param line - Line number in the generated code
792
+ * @param column - Column number in the generated code
793
+ * @param bias - Position matching strategy (default: Bias.BOUND)
794
+ * @returns Position information or null if not found
795
+ *
796
+ * @example
797
+ * ```ts
798
+ * const position = sourceService.getPosition(2, 15);
799
+ * console.log(position?.source); // Original source file
800
+ * ```
801
+ *
802
+ * @since 1.0.0
803
+ */
804
+ getPosition(line: number, column: number, bias?: Bias): PositionInterface | null;
805
+ /**
806
+ * Retrieves position with source content for a location in generated code.
807
+ *
808
+ * @param line - Line number in the generated code
809
+ * @param column - Column number in the generated code
810
+ * @param bias - Position matching strategy (default: Bias.BOUND)
811
+ * @returns Position with content information or null if not found
812
+ *
813
+ * @example
814
+ * ```ts
815
+ * const posWithContent = sourceService.getPositionWithContent(3, 5);
816
+ * console.log(posWithContent?.sourcesContent); // Original source content
817
+ * ```
818
+ *
819
+ * @since 1.0.0
820
+ */
821
+ getPositionWithContent(line: number, column: number, bias?: Bias): PositionWithContentInterface | null;
822
+ /**
823
+ * Retrieves position with a code snippet from the original source.
824
+ *
825
+ * @param line - Line number in the generated code
826
+ * @param column - Column number in the generated code
827
+ * @param bias - Position matching strategy (default: Bias.BOUND)
828
+ * @param options - Configuration for the amount of surrounding lines
829
+ * @returns Position with code snippet or null if not found
830
+ *
831
+ * @example
832
+ * ```ts
833
+ * const posWithCode = sourceService.getPositionWithCode(4, 8, Bias.BOUND, {
834
+ * linesBefore: 2,
835
+ * linesAfter: 2
836
+ * });
837
+ * console.log(posWithCode?.code); // Code snippet with context
838
+ * ```
839
+ *
840
+ * @since 1.0.0
841
+ */
842
+ getPositionWithCode(line: number, column: number, bias?: Bias, options?: SourceOptionsInterface): PositionWithCodeInterface | null;
843
+ /**
844
+ * Serializes the source map to a JSON string.
845
+ *
846
+ * @returns JSON string representation of the source map
847
+ *
848
+ * @example
849
+ * ```ts
850
+ * const jsonString = sourceService.toString();
851
+ * console.log(jsonString); // '{"version":3,"file":"bundle.js",...}'
852
+ * ```
853
+ *
854
+ * @since 1.0.0
855
+ */
856
+ toString(): string;
857
+ /**
858
+ * Validates a source map object has all required properties.
859
+ *
860
+ * @param input - Source map object to validate
861
+ *
862
+ * @throws Error - When required properties are missing
863
+ *
864
+ * @since 1.0.0
865
+ */
866
+ private validateSourceMap;
867
+ }
868
+ /**
869
+ * A callback function for formatting code lines
870
+ *
871
+ * @param lineString - The content of the line to be formatted
872
+ * @param padding - The amount of padding to be applied to the line
873
+ * @param line - The line number of the line to be formatted
874
+ * @returns Formatted line string
875
+ *
876
+ * @since 1.0.0
877
+ */
878
+ export type FormatCodeCallbackType = (lineString: string, padding: number, line: number) => string;
879
+ /**
880
+ * Configuration options for formatting code
881
+ *
882
+ * @since 1.0.0
883
+ */
884
+ export interface FormatCodeInterface {
885
+ /**
886
+ * The amount of padding to be applied to each line
887
+ * @since 1.0.0
888
+ */
889
+ padding?: number;
890
+ /**
891
+ * The starting line number for formatting
892
+ * @since 1.0.0
893
+ */
894
+ startLine?: number;
895
+ /**
896
+ * An optional action object specifying a line where a callback function should be triggered.
897
+ * @since 1.0.0
898
+ */
899
+ action?: {
900
+ /**
901
+ * The line number at which the callback function should be triggered.
902
+ * @since 1.0.0
903
+ */
904
+ triggerLine: number;
905
+ /**
906
+ * The callback function to be executed when the trigger line is encountered.
907
+ * @since 1.0.0
908
+ */
909
+ callback: FormatCodeCallbackType;
910
+ };
911
+ }
912
+ /**
913
+ * Configuration for ANSI color styling of error pointers
914
+ * @since 1.0.0
915
+ */
916
+ export interface AnsiOptionInterface {
917
+ /**
918
+ * ANSI color code to apply to the error pointer
919
+ * @since 1.0.0
920
+ */
921
+ color: string;
922
+ /**
923
+ * ANSI reset code to restore default styling after the error pointer
924
+ * @since 1.0.0
925
+ */
926
+ reset: string;
927
+ }
928
+ /**
929
+ * Formats a code snippet with optional line padding and custom actions
930
+ *
931
+ * @param code - The source code | stack to be formatted
932
+ * @param options - Configuration options for formatting the code
933
+ * @returns A formatted string of the code snippet with applied padding and custom actions
934
+ *
935
+ * @remarks
936
+ * This function takes a code string and an options object to format the code snippet.
937
+ * It applies padding to line numbers and can trigger custom actions for specific lines.
938
+ * Options include padding (default 10), startLine (default 0), and custom actions for specific lines.
939
+ *
940
+ * @example
941
+ * ```ts
942
+ * const formattedCode = formatCode(code, {
943
+ * padding: 8,
944
+ * startLine: 5,
945
+ * action: {
946
+ * triggerLine: 7,
947
+ * callback: (lineString, padding, lineNumber) => {
948
+ * return `Custom formatting for line ${lineNumber}: ${lineString}`;
949
+ * }
950
+ * }
951
+ * });
952
+ * ```
953
+ *
954
+ * @since 1.0.0
955
+ */
956
+ export declare function formatCode(code: string, options?: FormatCodeInterface): string;
957
+ /**
958
+ * Formats a code snippet around an error location with special highlighting
959
+ *
960
+ * @param sourcePosition - An object containing information about the source code and error location
961
+ * @param ansiOption - Optional configuration for ANSI color codes
962
+ * @returns A formatted string representing the relevant code snippet with error highlighting
963
+ *
964
+ * @throws Error - If the provided sourcePosition object has invalid line or column numbers
965
+ *
966
+ * @remarks
967
+ * This function takes a sourcePosition object with code content and error location information,
968
+ * then uses formatCode to format and highlight the relevant code snippet around the error.
969
+ * The sourcePosition object should contain code (string), line (number), column (number),
970
+ * and optional startLine (number, defaults to 1).
971
+ *
972
+ * @example
973
+ * ```ts
974
+ * const formattedErrorCode = formatErrorCode({
975
+ * code: "const x = 1;\nconst y = x.undefined;\n",
976
+ * line: 2,
977
+ * column: 15,
978
+ * startLine: 1
979
+ * });
980
+ * ```
981
+ *
982
+ * @see formatCode - The underlying function used for basic code formatting
983
+ *
984
+ * @since 1.0.0
985
+ */
986
+ export declare function formatErrorCode(sourcePosition: PositionWithCodeInterface, ansiOption?: AnsiOptionInterface): string;
2
987
  /**
3
988
  * Defines the color scheme for syntax highlighting different code elements.
4
989
  *
@@ -107,7 +1092,7 @@ export interface HighlightNodeSegmentInterface {
107
1092
  *
108
1093
  * @since 1.0.0
109
1094
  */
110
- export const enum Colors {
1095
+ export declare const enum Colors {
111
1096
  reset = "\u001B[0m",
112
1097
  gray = "\u001B[38;5;243m",
113
1098
  darkGray = "\u001B[38;5;238m",
@@ -139,7 +1124,7 @@ export const enum Colors {
139
1124
  *
140
1125
  * @since 1.0.0
141
1126
  */
142
- export class CodeHighlighter {
1127
+ export declare class CodeHighlighter {
143
1128
  private sourceFile;
144
1129
  private code;
145
1130
  private schema;
@@ -420,4 +1405,270 @@ export class CodeHighlighter {
420
1405
  *
421
1406
  * @since 1.0.0
422
1407
  */
423
- export function highlightCode(code: string, schema?: Partial<HighlightSchemeInterface>): string;
1408
+ export declare function highlightCode(code: string, schema?: Partial<HighlightSchemeInterface>): string;
1409
+ /**
1410
+ * Interface representing a stack frame in a stack trace.
1411
+ *
1412
+ * This structure provides detailed information about the location
1413
+ * of a specific frame in the stack trace, primarily used in error debugging and stack analysis.
1414
+ * It optionally includes information about the line, column, file, function name, and other details
1415
+ * about the origin of the code.
1416
+ *
1417
+ * Properties:
1418
+ * - source: The source code relevant to the stack frame.
1419
+ * - line: An optional line number in the code associated with this frame.
1420
+ * - column: An optional column number in the line associated with this frame.
1421
+ * - fileName: The name of the file associated with the frame, if available.
1422
+ * - functionName: The name of the function where the stack frame is located, if available.
1423
+ * - eval: Indicates if the stack frame originates from an evaluated script.
1424
+ * - async: Indicates if the stack frame is part of an asynchronous operation.
1425
+ * - native: Indicates if the stack frame is part of native code execution.
1426
+ * - constructor: Indicates if the frame is related to an object constructor invocation.
1427
+ * - evalOrigin: Optional information about the origin of the code if it resulted from an eval execution,
1428
+ * including line number, column number, file name, and function name.
1429
+ *
1430
+ * @since 3.0.0
1431
+ */
1432
+ export interface StackFrame {
1433
+ source: string;
1434
+ line?: number;
1435
+ column?: number;
1436
+ fileName?: string;
1437
+ functionName?: string;
1438
+ eval: boolean;
1439
+ async: boolean;
1440
+ native: boolean;
1441
+ constructor: boolean;
1442
+ evalOrigin?: {
1443
+ line?: number;
1444
+ column?: number;
1445
+ fileName?: string;
1446
+ functionName?: string;
1447
+ };
1448
+ }
1449
+ /**
1450
+ * Represents a fully parsed error stack trace with structured information
1451
+ *
1452
+ * @see StackFrame
1453
+ * @since 2.1.0
1454
+ */
1455
+ export interface ParsedStackTrace {
1456
+ name: string;
1457
+ message: string;
1458
+ stack: StackFrame[];
1459
+ rawStack: string;
1460
+ }
1461
+ /**
1462
+ * Enumeration of JavaScript engines that can be detected from stack traces
1463
+ *
1464
+ * @since 2.1.0
1465
+ */
1466
+ export declare const enum JSEngines {
1467
+ V8 = 0,
1468
+ SPIDERMONKEY = 1,
1469
+ JAVASCRIPT_CORE = 2,
1470
+ UNKNOWN = 3
1471
+ }
1472
+ /**
1473
+ * Detects the JavaScript engine based on the format of a stack trace line
1474
+ *
1475
+ * @param stack - The stack trace to analyze
1476
+ * @returns The identified JavaScript engine type
1477
+ *
1478
+ * @example
1479
+ * ```ts
1480
+ * const engine = detectJSEngine("at functionName (/path/to/file.js:10:15)");
1481
+ * if (engine === JSEngines.V8) {
1482
+ * // Handle V8 specific logic
1483
+ * }
1484
+ * ```
1485
+ *
1486
+ * @since 2.1.0
1487
+ */
1488
+ export declare function detectJSEngine(stack: string): JSEngines;
1489
+ /**
1490
+ * Normalizes file paths from various formats to a standard format
1491
+ *
1492
+ * @param filePath - The file path to normalize, which may include protocol prefixes
1493
+ * @returns A normalized file path with consistent separators and without protocol prefixes
1494
+ *
1495
+ * @remarks
1496
+ * Handles both Windows and Unix-style paths, as well as file:// protocol URLs.
1497
+ * Converts all backslashes to forward slashes for consistency.
1498
+ *
1499
+ * @example
1500
+ * ```ts
1501
+ * // Windows file URL to a normal path
1502
+ * normalizePath("file:///C:/path/to/file.js"); // "C:/path/to/file.js"
1503
+ *
1504
+ * // Unix file URL to a normal path
1505
+ * normalizePath("file:///path/to/file.js"); // "/path/to/file.js"
1506
+ *
1507
+ * // Windows backslashes to forward slashes
1508
+ * normalizePath("C:\\path\\to\\file.js"); // "C:/path/to/file.js"
1509
+ * ```
1510
+ *
1511
+ * @since 2.1.0
1512
+ */
1513
+ export declare function normalizePath(filePath: string): string;
1514
+ /**
1515
+ * Creates a default stack frame object with initial values
1516
+ *
1517
+ * @param source - The original source line from the stack trace
1518
+ * @returns A new StackFrame object with default null values
1519
+ *
1520
+ * @see ParsedStackTrace
1521
+ * @see StackFrame
1522
+ *
1523
+ * @since 2.1.0
1524
+ */
1525
+ export declare function createDefaultFrame(source: string): StackFrame;
1526
+ /**
1527
+ * Safely parses a string value to an integer, handling undefined and null cases
1528
+ *
1529
+ * @param value - The string value to parse
1530
+ * @returns The parsed integer or null if the input is undefined/null
1531
+ *
1532
+ * @example
1533
+ * ```ts
1534
+ * safeParseInt("42"); // 42
1535
+ * safeParseInt(undefined); // null
1536
+ * safeParseInt(null); // null
1537
+ * ```
1538
+ *
1539
+ * @since 2.1.0
1540
+ */
1541
+ export declare function safeParseInt(value: string | undefined | null): number | undefined;
1542
+ /**
1543
+ * Parses a V8 JavaScript engine stack trace line into a structured StackFrame object
1544
+ *
1545
+ * @param line - The stack trace line to parse
1546
+ * @returns A StackFrame object containing the parsed information
1547
+ *
1548
+ * @remarks
1549
+ * Handles both standard V8 stack frames and eval-generated stack frames which
1550
+ * have a more complex structure with nested origin information.
1551
+ *
1552
+ * @example
1553
+ * ```ts
1554
+ * // Standard frame
1555
+ * parseV8StackLine("at functionName (/path/to/file.js:10:15)");
1556
+ *
1557
+ * // Eval frame
1558
+ * parseV8StackLine("at eval (eval at evalFn (/source.js:5:10), <anonymous>:1:5)");
1559
+ * ```
1560
+ *
1561
+ * @throws Error - If the line format doesn't match any known V8 pattern
1562
+ *
1563
+ * @see StackFrame
1564
+ * @see createDefaultFrame
1565
+ *
1566
+ * @since 2.1.0
1567
+ */
1568
+ export declare function parseV8StackLine(line: string): StackFrame;
1569
+ /**
1570
+ * Parses a SpiderMonkey JavaScript engine stack trace line into a structured StackFrame object
1571
+ *
1572
+ * @param line - The stack trace line to parse
1573
+ * @returns A StackFrame object containing the parsed information
1574
+ *
1575
+ * @remarks
1576
+ * Handles both standard SpiderMonkey stack frames and eval/Function-generated stack frames
1577
+ * which contain additional evaluation context information.
1578
+ *
1579
+ * @example
1580
+ * ```ts
1581
+ * // Standard frame
1582
+ * parseSpiderMonkeyStackLine("functionName@/path/to/file.js:10:15");
1583
+ *
1584
+ * // Eval frame
1585
+ * parseSpiderMonkeyStackLine("evalFn@/source.js line 5 > eval:1:5");
1586
+ * ```
1587
+ *
1588
+ * @see StackFrame
1589
+ * @see createDefaultFrame
1590
+ *
1591
+ * @since 2.1.0
1592
+ */
1593
+ export declare function parseSpiderMonkeyStackLine(line: string): StackFrame;
1594
+ /**
1595
+ * Parses a JavaScriptCore engine stack trace line into a structured StackFrame object
1596
+ *
1597
+ * @param line - The stack trace line to parse
1598
+ * @returns A StackFrame object containing the parsed information
1599
+ *
1600
+ * @remarks
1601
+ * Handles both standard JavaScriptCore stack frames and eval-generated stack frames.
1602
+ * Special handling is provided for "global code" references and native code.
1603
+ *
1604
+ * @example
1605
+ * ```ts
1606
+ * // Standard frame
1607
+ * parseJavaScriptCoreStackLine("functionName@/path/to/file.js:10:15");
1608
+ *
1609
+ * // Eval frame
1610
+ * parseJavaScriptCoreStackLine("eval code@");
1611
+ * ```
1612
+ *
1613
+ * @see StackFrame
1614
+ * @see createDefaultFrame
1615
+ *
1616
+ * @since 2.1.0
1617
+ */
1618
+ export declare function parseJavaScriptCoreStackLine(line: string): StackFrame;
1619
+ /**
1620
+ * Parses a stack trace line based on the detected JavaScript engine
1621
+ *
1622
+ * @param line - The stack trace line to parse
1623
+ * @param engine - The JavaScript engine type that generated the stack trace
1624
+ * @returns A StackFrame object containing the parsed information
1625
+ *
1626
+ * @remarks
1627
+ * Delegates to the appropriate parsing function based on the JavaScript engine.
1628
+ * Defaults to V8 parsing if the engine is unknown.
1629
+ *
1630
+ * @example
1631
+ * ```ts
1632
+ * const engine = detectJSEngine(stackLine);
1633
+ * const frame = parseStackLine(stackLine, engine);
1634
+ * ```
1635
+ *
1636
+ * @see JSEngines
1637
+ * @see parseV8StackLine
1638
+ * @see parseSpiderMonkeyStackLine
1639
+ * @see parseJavaScriptCoreStackLine
1640
+ *
1641
+ * @since 2.1.0
1642
+ */
1643
+ export declare function parseStackLine(line: string, engine: JSEngines): StackFrame;
1644
+ /**
1645
+ * Parses a complete error stack trace into a structured format
1646
+ *
1647
+ * @param error - Error object or error message string to parse
1648
+ * @returns A ParsedStackTrace object containing structured stack trace information
1649
+ *
1650
+ * @remarks
1651
+ * Automatically detects the JavaScript engine from the stack format.
1652
+ * Filters out redundant information like the error name/message line.
1653
+ * Handles both Error objects and string error messages.
1654
+ *
1655
+ * @example
1656
+ * ```ts
1657
+ * try {
1658
+ * throw new Error ("Something went wrong");
1659
+ * } catch (error) {
1660
+ * const parsedStack = parseErrorStack(error);
1661
+ * console.log(parsedStack.name); // "Error"
1662
+ * console.log(parsedStack.message); // "Something went wrong"
1663
+ * console.log(parsedStack.stack); // Array of StackFrame objects
1664
+ * }
1665
+ * ```
1666
+ *
1667
+ * @see ParsedStackTrace
1668
+ * @see StackFrame
1669
+ * @see parseStackLine
1670
+ * @see detectJSEngine
1671
+ *
1672
+ * @since 2.1.0
1673
+ */
1674
+ export declare function parseErrorStack(error: Error | string): ParsedStackTrace;