@remotex-labs/xmap 3.0.2 → 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,63 +1,4 @@
1
- /**
2
- * A callback function for formatting code lines
3
- *
4
- * @param lineString - The content of the line to be formatted
5
- * @param padding - The amount of padding to be applied to the line
6
- * @param line - The line number of the line to be formatted
7
- * @returns Formatted line string
8
- *
9
- * @since 1.0.0
10
- */
11
- export type FormatCodeCallbackType = (lineString: string, padding: number, line: number) => string;
12
- /**
13
- * Configuration options for formatting code
14
- *
15
- * @since 1.0.0
16
- */
17
- export interface FormatCodeInterface {
18
- /**
19
- * The amount of padding to be applied to each line
20
- * @since 1.0.0
21
- */
22
- padding?: number;
23
- /**
24
- * The starting line number for formatting
25
- * @since 1.0.0
26
- */
27
- startLine?: number;
28
- /**
29
- * An optional action object specifying a line where a callback function should be triggered.
30
- * @since 1.0.0
31
- */
32
- action?: {
33
- /**
34
- * The line number at which the callback function should be triggered.
35
- * @since 1.0.0
36
- */
37
- triggerLine: number;
38
- /**
39
- * The callback function to be executed when the trigger line is encountered.
40
- * @since 1.0.0
41
- */
42
- callback: FormatCodeCallbackType;
43
- };
44
- }
45
- /**
46
- * Configuration for ANSI color styling of error pointers
47
- * @since 1.0.0
48
- */
49
- export interface AnsiOptionInterface {
50
- /**
51
- * ANSI color code to apply to the error pointer
52
- * @since 1.0.0
53
- */
54
- color: string;
55
- /**
56
- * ANSI reset code to restore default styling after the error pointer
57
- * @since 1.0.0
58
- */
59
- reset: string;
60
- }
1
+ import type * as ts from "typescript";
61
2
  /**
62
3
  * Represents a source map structure used for mapping code within a file to its original source
63
4
  * @since 1.0.0
@@ -198,61 +139,1536 @@ export interface SourceOptionsInterface {
198
139
  linesBefore?: number;
199
140
  }
200
141
  /**
201
- * Formats a code snippet with optional line padding and custom actions
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.
202
144
  *
203
- * @param code - The source code | stack to be formatted
204
- * @param options - Configuration options for formatting the code
205
- * @returns A formatted string of the code snippet with applied padding and custom actions
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
206
222
  *
207
223
  * @remarks
208
- * This function takes a code string and an options object to format the code snippet.
209
- * It applies padding to line numbers and can trigger custom actions for specific lines.
210
- * Options include padding (default 10), startLine (default 0), and custom actions for specific lines.
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.
211
227
  *
212
228
  * @example
213
229
  * ```ts
214
- * const formattedCode = formatCode(code, {
215
- * padding: 8,
216
- * startLine: 5,
217
- * action: {
218
- * triggerLine: 7,
219
- * callback: (lineString, padding, lineNumber) => {
220
- * return `Custom formatting for line ${lineNumber}: ${lineString}`;
221
- * }
222
- * }
223
- * });
230
+ * const segment: SegmentInterface = {
231
+ * line: 42,
232
+ * column: 10,
233
+ * nameIndex: 5,
234
+ * sourceIndex: 0,
235
+ * generatedLine: 100,
236
+ * generatedColumn: 15
237
+ * };
224
238
  * ```
225
239
  *
226
240
  * @since 1.0.0
227
241
  */
228
- export function formatCode(code: string, options?: FormatCodeInterface): string;
242
+ export interface SegmentInterface {
243
+ line: number;
244
+ column: number;
245
+ nameIndex: number | null;
246
+ sourceIndex: number;
247
+ generatedLine: number;
248
+ generatedColumn: number;
249
+ }
229
250
  /**
230
- * Formats a code snippet around an error location with special highlighting
251
+ * A specialized segment interface used during source map offset calculations.
231
252
  *
232
- * @param sourcePosition - An object containing information about the source code and error location
233
- * @param ansiOption - Optional configuration for ANSI color codes
234
- * @returns A formatted string representing the relevant code snippet with error highlighting
253
+ * @property nameIndex - Index of the symbol name in the source map's names array (always numeric)
235
254
  *
236
- * @throws Error - If the provided sourcePosition object has invalid line or column numbers
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
237
285
  *
238
286
  * @remarks
239
- * This function takes a sourcePosition object with code content and error location information,
240
- * then uses formatCode to format and highlight the relevant code snippet around the error.
241
- * The sourcePosition object should contain code (string), line (number), column (number),
242
- * and optional startLine (number, defaults to 1).
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.
243
290
  *
244
291
  * @example
245
292
  * ```ts
246
- * const formattedErrorCode = formatErrorCode({
247
- * code: "const x = 1;\nconst y = x.undefined;\n",
248
- * line: 2,
249
- * column: 15,
250
- * startLine: 1
251
- * });
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
252
296
  * ```
253
297
  *
254
- * @see formatCode - The underlying function used for basic code formatting
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
+ * ```
255
675
  *
256
676
  * @since 1.0.0
257
677
  */
258
- export function formatErrorCode(sourcePosition: PositionWithCodeInterface, ansiOption?: AnsiOptionInterface): string;
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;
987
+ /**
988
+ * Defines the color scheme for syntax highlighting different code elements.
989
+ *
990
+ * @interface HighlightSchemeInterface
991
+ *
992
+ * @property enumColor - Color code for enum declarations and references
993
+ * @property typeColor - Color code for type annotations and primitive types
994
+ * @property classColor - Color code for class declarations and references
995
+ * @property stringColor - Color code for string literals and template strings
996
+ * @property keywordColor - Color code for language keywords
997
+ * @property commentColor - Color code for comments (single-line and multi-line)
998
+ * @property functionColor - Color code for function declarations and calls
999
+ * @property variableColor - Color code for variable declarations and references
1000
+ * @property interfaceColor - Color code for interface declarations and references
1001
+ * @property parameterColor - Color code for function and method parameters
1002
+ * @property getAccessorColor - Color code for getter accessor methods
1003
+ * @property numericLiteralColor - Color code for numeric literals
1004
+ * @property methodSignatureColor - Color code for method signatures in interfaces
1005
+ * @property regularExpressionColor - Color code for regular expression literals
1006
+ * @property propertyAssignmentColor - Color code for property assignments in object literals
1007
+ * @property propertyAccessExpressionColor - Color code for property access expressions
1008
+ * @property expressionWithTypeArgumentsColor - Color code for type arguments in expressions
1009
+ *
1010
+ * @example
1011
+ * ```ts
1012
+ * const darkTheme: HighlightSchemeInterface = {
1013
+ * enumColor: Colors.cyan,
1014
+ * typeColor: Colors.blue,
1015
+ * classColor: Colors.yellow,
1016
+ * // ...other color definitions
1017
+ * };
1018
+ * ```
1019
+ *
1020
+ * @since 1.0.0
1021
+ */
1022
+ export interface HighlightSchemeInterface {
1023
+ enumColor: string;
1024
+ typeColor: string;
1025
+ classColor: string;
1026
+ stringColor: string;
1027
+ keywordColor: string;
1028
+ commentColor: string;
1029
+ functionColor: string;
1030
+ variableColor: string;
1031
+ interfaceColor: string;
1032
+ parameterColor: string;
1033
+ getAccessorColor: string;
1034
+ numericLiteralColor: string;
1035
+ methodSignatureColor: string;
1036
+ regularExpressionColor: string;
1037
+ propertyAssignmentColor: string;
1038
+ propertyAccessExpressionColor: string;
1039
+ expressionWithTypeArgumentsColor: string;
1040
+ }
1041
+ /**
1042
+ * Represents a segment of source code to be highlighted with specific styling.
1043
+ *
1044
+ * @interface HighlightNodeSegmentInterface
1045
+ *
1046
+ * @property start - The starting character position of the segment in the source text
1047
+ * @property end - The ending character position of the segment in the source text
1048
+ * @property color - The color or style code to apply to this segment
1049
+ * @property reset - The reset code that returns formatting to normal after this segment
1050
+ *
1051
+ * @remarks
1052
+ * Segments are the fundamental units of the highlighting system.
1053
+ * Each segment represents a portion of text that should receive specific styling.
1054
+ * When the source code is processed for display,
1055
+ * these segments are used to insert the appropriate color/style codes at the correct positions.
1056
+ *
1057
+ * The highlighter maintains a collection of these segments and applies them
1058
+ * in position order to create the complete highlighted output.
1059
+ *
1060
+ * @example
1061
+ * ```ts
1062
+ * const keywordSegment: HighlightNodeSegmentInterface = {
1063
+ * start: 0,
1064
+ * end: 6,
1065
+ * color: '\x1b[34m', // Blue for the keyword "import"
1066
+ * reset: '\x1b[0m' // Reset to default
1067
+ * };
1068
+ * ```
1069
+ *
1070
+ * @see HighlightSchemeInterface
1071
+ * @see addSegment
1072
+ *
1073
+ * @since 1.0.0
1074
+ */
1075
+ export interface HighlightNodeSegmentInterface {
1076
+ end: number;
1077
+ start: number;
1078
+ color: string;
1079
+ reset: string;
1080
+ }
1081
+ /**
1082
+ * An enum containing ANSI escape sequences for various colors
1083
+ *
1084
+ * @remarks
1085
+ * This enum is primarily intended for terminal output and won't work directly in JavaScript for web development.
1086
+ * It defines color codes for various colors and a reset code to return to the default text color.
1087
+ *
1088
+ * @example
1089
+ * ```ts
1090
+ * console.log(`${Colors.red}This text will be red in the terminal.${Colors.reset}`);
1091
+ * ```
1092
+ *
1093
+ * @since 1.0.0
1094
+ */
1095
+ export declare const enum Colors {
1096
+ reset = "\u001B[0m",
1097
+ gray = "\u001B[38;5;243m",
1098
+ darkGray = "\u001B[38;5;238m",
1099
+ lightCoral = "\u001B[38;5;203m",
1100
+ lightOrange = "\u001B[38;5;215m",
1101
+ oliveGreen = "\u001B[38;5;149m",
1102
+ burntOrange = "\u001B[38;5;208m",
1103
+ lightGoldenrodYellow = "\u001B[38;5;221m",
1104
+ lightYellow = "\u001B[38;5;230m",
1105
+ canaryYellow = "\u001B[38;5;227m",
1106
+ deepOrange = "\u001B[38;5;166m",
1107
+ lightGray = "\u001B[38;5;252m",
1108
+ brightPink = "\u001B[38;5;197m"
1109
+ }
1110
+ /**
1111
+ * Class responsible for applying semantic highlighting to a source code string based on a given color scheme
1112
+ *
1113
+ * @remarks
1114
+ * Processes TypeScript AST nodes and applies color formatting to different code elements
1115
+ * according to the provided color scheme.
1116
+ *
1117
+ * @example
1118
+ * ```ts
1119
+ * const sourceFile = ts.createSourceFile('example.ts', code, ts.ScriptTarget.Latest);
1120
+ * const highlighter = new CodeHighlighter(sourceFile, code, customScheme);
1121
+ * highlighter.parseNode(sourceFile);
1122
+ * const highlightedCode = highlighter.highlight();
1123
+ * ```
1124
+ *
1125
+ * @since 1.0.0
1126
+ */
1127
+ export declare class CodeHighlighter {
1128
+ private sourceFile;
1129
+ private code;
1130
+ private schema;
1131
+ /**
1132
+ * A Map of segments where the key is a combination of start and end positions.
1133
+ *
1134
+ * @remarks
1135
+ * This structure ensures unique segments and allows for fast lookups and updates.
1136
+ *
1137
+ * @see HighlightNodeSegmentInterface
1138
+ * @since 1.0.0
1139
+ */
1140
+ private segments;
1141
+ /**
1142
+ * Creates an instance of the CodeHighlighter class.
1143
+ *
1144
+ * @param sourceFile - The TypeScript AST node representing the source file
1145
+ * @param code - The source code string to be highlighted
1146
+ * @param schema - The color scheme used for highlighting different elements in the code
1147
+ *
1148
+ * @since 1.0.0
1149
+ */
1150
+ constructor(sourceFile: ts.Node, code: string, schema: HighlightSchemeInterface);
1151
+ /**
1152
+ * Parses a TypeScript AST node and processes its comments to identify segments that need highlighting.
1153
+ *
1154
+ * @param node - The TypeScript AST node to be parsed
1155
+ *
1156
+ * @since 1.0.0
1157
+ */
1158
+ parseNode(node: ts.Node): void;
1159
+ /**
1160
+ * Generates a string with highlighted code segments based on the provided color scheme.
1161
+ *
1162
+ * @returns The highlighted code as a string, with ANSI color codes applied to the segments
1163
+ *
1164
+ * @remarks
1165
+ * This method processes the stored segments, applies the appropriate colors to each segment,
1166
+ * and returns the resulting highlighted code as a single string.
1167
+ *
1168
+ * @since 1.0.0
1169
+ */
1170
+ highlight(): string;
1171
+ /**
1172
+ * Extracts a text segment from the source code using position indices.
1173
+ *
1174
+ * @param start - The starting index position in the source text
1175
+ * @param end - The ending index position in the source text (optional)
1176
+ * @returns The substring of source text between the start and end positions
1177
+ *
1178
+ * @remarks
1179
+ * This utility method provides access to specific portions of the source code
1180
+ * based on character positions. When the end parameter is omitted, the extraction
1181
+ * will continue to the end of the source text.
1182
+ *
1183
+ * This method is typically used during the highlighting process to access the
1184
+ * actual text content that corresponds to syntax nodes or other text ranges
1185
+ * before applying formatting.
1186
+ *
1187
+ * @example
1188
+ * ```ts
1189
+ * // Extract a variable name
1190
+ * const variableName = this.getSegmentSource(10, 15);
1191
+ *
1192
+ * // Extract from a position to the end of source
1193
+ * const remaining = this.getSegmentSource(100);
1194
+ * ```
1195
+ *
1196
+ * @see addSegment
1197
+ * @see highlight
1198
+ *
1199
+ * @since 1.0.0
1200
+ */
1201
+ private getSegmentSource;
1202
+ /**
1203
+ * Registers a text segment for syntax highlighting with specified style information.
1204
+ *
1205
+ * @param start - The starting position of the segment in the source text
1206
+ * @param end - The ending position of the segment in the source text
1207
+ * @param color - The color code to apply to this segment
1208
+ * @param reset - The color reset code to apply after the segment (defaults to the standard reset code)
1209
+ *
1210
+ * @remarks
1211
+ * This method creates a unique key for each segment based on its position and stores the segment information in a map.
1212
+ * Each segment contains its position information, styling code,
1213
+ * and reset code which will later be used during the highlighting process.
1214
+ *
1215
+ * If multiple segments are added with the same positions, the later additions will
1216
+ * overwrite earlier ones due to the map's key-based storage.
1217
+ *
1218
+ * @example
1219
+ * ```ts
1220
+ * // Highlight a variable name in red
1221
+ * this.addSegment(10, 15, Colors.red);
1222
+ *
1223
+ * // Highlight a keyword with custom color and reset
1224
+ * this.addSegment(20, 26, Colors.blue, Colors.customReset);
1225
+ * ```
1226
+ *
1227
+ * @see Colors
1228
+ * @see processNode
1229
+ *
1230
+ * @since 1.0.0
1231
+ */
1232
+ private addSegment;
1233
+ /**
1234
+ * Processes and highlights comments associated with a TypeScript AST node.
1235
+ *
1236
+ * @param node - The TypeScript AST node whose comments are to be processed
1237
+ *
1238
+ * @remarks
1239
+ * This method identifies both leading and trailing comments associated with the given node
1240
+ * and adds them to the highlighting segments.
1241
+ * The comments are extracted from the full source text using TypeScript's utility functions
1242
+ * and are highlighted using the color specified
1243
+ * in the schema's commentColor property.
1244
+ *
1245
+ * Leading comments appear before the node, while trailing comments appear after it.
1246
+ * Both types are processed with the same highlighting style.
1247
+ *
1248
+ * @example
1249
+ * ```ts
1250
+ * // For a node that might have comments like:
1251
+ * // This is a leading comment
1252
+ * const x = 5; // This is a trailing comment
1253
+ *
1254
+ * this.processComments(someNode);
1255
+ * // Both comments will be added to segments with the comment color
1256
+ * ```
1257
+ *
1258
+ * @see addSegment
1259
+ * @see ts.getLeadingCommentRanges
1260
+ * @see ts.getTrailingCommentRanges
1261
+ *
1262
+ * @since 1.0.0
1263
+ */
1264
+ private processComments;
1265
+ /**
1266
+ * Processes TypeScript keywords and primitive type references in an AST node for syntax highlighting.
1267
+ *
1268
+ * @param node - The TypeScript AST node to be processed for keywords
1269
+ *
1270
+ * @remarks
1271
+ * This method handles two categories of tokens that require special highlighting:
1272
+ *
1273
+ * 1. Primitive type references: Highlights references to built-in types like `null`,
1274
+ * `void`, `string`, `number`, `boolean`, and `undefined` using the type color.
1275
+ *
1276
+ * 2. TypeScript keywords: Identifies any node whose kind falls within the TypeScript
1277
+ * keyword range (between FirstKeyword and LastKeyword) and highlights it using
1278
+ * the keyword color.
1279
+ *
1280
+ * Each identified token is added to the segments collection with appropriate position
1281
+ * and color information.
1282
+ *
1283
+ * @example
1284
+ * ```ts
1285
+ * // Inside syntax highlighting process
1286
+ * this.processKeywords(someNode);
1287
+ * // If the node represents a keyword like 'const' or a primitive type like 'string',
1288
+ * // it will be added to the segments with the appropriate color
1289
+ * ```
1290
+ *
1291
+ * @see addSegment
1292
+ * @see ts.SyntaxKind
1293
+ *
1294
+ * @since 1.0.0
1295
+ */
1296
+ private processKeywords;
1297
+ /**
1298
+ * Processes identifier nodes and applies appropriate syntax highlighting based on their context.
1299
+ *
1300
+ * @param node - The TypeScript AST node representing the identifier to be processed
1301
+ *
1302
+ * @remarks
1303
+ * This method determines the appropriate color for an identifier by examining its parent node's kind.
1304
+ * Different colors are applied based on the identifier's role in the code:
1305
+ * - Enum members use enumColor
1306
+ * - Interface names use interfaceColor
1307
+ * - Class names use classColor
1308
+ * - Function and method names use functionColor
1309
+ * - Parameters use parameterColor
1310
+ * - Variables and properties use variableColor
1311
+ * - Types use typeColor
1312
+ * - And more specialized cases for other syntax kinds
1313
+ *
1314
+ * Special handling is applied to property access expressions to differentiate between
1315
+ * the object being accessed and the property being accessed.
1316
+ *
1317
+ * @example
1318
+ * ```ts
1319
+ * // Inside the CodeHighlighter class
1320
+ * const identifierNode = getIdentifierNode(); // Get some identifier node
1321
+ * this.processIdentifier(identifierNode);
1322
+ * // The identifier is now added to segments with appropriate color based on its context
1323
+ * ```
1324
+ *
1325
+ * @see addSegment
1326
+ * @see HighlightSchemeInterface
1327
+ *
1328
+ * @since 1.0.0
1329
+ */
1330
+ private processIdentifier;
1331
+ /**
1332
+ * Processes a TypeScript template expression and adds highlighting segments for its literal parts.
1333
+ *
1334
+ * @param templateExpression - The TypeScript template expression to be processed
1335
+ *
1336
+ * @remarks
1337
+ * This method adds color segments for both the template head and each template span's literal part.
1338
+ * All template string components are highlighted using the color defined in the schema's stringColor.
1339
+ *
1340
+ * @example
1341
+ * ```ts
1342
+ * // Given a template expression like: `Hello ${name}`
1343
+ * this.processTemplateExpression(templateNode);
1344
+ * // Both "Hello " and the closing part after the expression will be highlighted
1345
+ * ```
1346
+ *
1347
+ * @see addSegment
1348
+ *
1349
+ * @since 1.0.0
1350
+ */
1351
+ private processTemplateExpression;
1352
+ /**
1353
+ * Processes a TypeScript AST node and adds highlighting segments based on the node's kind.
1354
+ *
1355
+ * @param node - The TypeScript AST node to be processed
1356
+ *
1357
+ * @remarks
1358
+ * This method identifies the node's kind and applies the appropriate color for highlighting.
1359
+ * It handles various syntax kinds including literals (string, numeric, regular expressions),
1360
+ * template expressions, identifiers, and type references.
1361
+ * For complex node types like template expressions and identifiers, it delegates to specialized processing methods.
1362
+ *
1363
+ * @throws Error - When casting to TypeParameterDeclaration fails for non-compatible node kinds
1364
+ *
1365
+ * @example
1366
+ * ```ts
1367
+ * // Inside the CodeHighlighter class
1368
+ * const node = sourceFile.getChildAt(0);
1369
+ * this.processNode(node);
1370
+ * // Node is now added to the segments map with appropriate colors
1371
+ * ```
1372
+ *
1373
+ * @see processTemplateExpression
1374
+ * @see processIdentifier
1375
+ *
1376
+ * @since 1.0.0
1377
+ */
1378
+ private processNode;
1379
+ }
1380
+ /**
1381
+ * Applies semantic highlighting to the provided code string using the specified color scheme.
1382
+ *
1383
+ * @param code - The source code to be highlighted
1384
+ * @param schema - An optional partial schema defining the color styles for various code elements
1385
+ *
1386
+ * @returns A string with the code elements wrapped in the appropriate color styles
1387
+ *
1388
+ * @remarks
1389
+ * If no schema is provided, the default schema will be used. The function creates a TypeScript
1390
+ * source file from the provided code and walks through its AST to apply syntax highlighting.
1391
+ *
1392
+ * @example
1393
+ * ```ts
1394
+ * const code = 'const x: number = 42;';
1395
+ * const schema = {
1396
+ * keywordColor: '\x1b[34m', // Blue
1397
+ * numberColor: '\x1b[31m', // Red
1398
+ * };
1399
+ * const highlightedCode = highlightCode(code, schema);
1400
+ * console.log(highlightedCode);
1401
+ * ```
1402
+ *
1403
+ * @see CodeHighlighter
1404
+ * @see HighlightSchemeInterface
1405
+ *
1406
+ * @since 1.0.0
1407
+ */
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;