@remotex-labs/xmap 3.0.4 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +190 -122
- package/dist/cjs/formatter.component.js +3 -3
- package/dist/cjs/formatter.component.js.map +3 -3
- package/dist/cjs/highlighter.component.js +1 -1
- package/dist/cjs/highlighter.component.js.map +4 -4
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/parser.component.js +1 -1
- package/dist/cjs/parser.component.js.map +3 -3
- package/dist/esm/formatter.component.js +4 -4
- package/dist/esm/formatter.component.js.map +3 -3
- package/dist/esm/highlighter.component.js +1 -1
- package/dist/esm/highlighter.component.js.map +4 -4
- package/dist/esm/index.js +3 -3
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/parser.component.js +2 -2
- package/dist/esm/parser.component.js.map +3 -3
- package/dist/formatter.component.d.ts +198 -1453
- package/dist/highlighter.component.d.ts +147 -1357
- package/dist/index.d.ts +388 -1167
- package/dist/parser.component.d.ts +83 -1479
- package/package.json +19 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
2
|
/**
|
|
3
|
+
* This file was automatically generated by xBuild.
|
|
4
|
+
* DO NOT EDIT MANUALLY.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
3
7
|
* Represents a source map structure used for mapping code within a file to its original source
|
|
4
8
|
* @since 1.0.0
|
|
5
9
|
*/
|
|
@@ -138,6 +142,7 @@ export interface SourceOptionsInterface {
|
|
|
138
142
|
*/
|
|
139
143
|
linesBefore?: number;
|
|
140
144
|
}
|
|
145
|
+
|
|
141
146
|
/**
|
|
142
147
|
* Encodes a given number using Variable-Length Quantity (VLQ) encoding. Negative numbers are encoded by
|
|
143
148
|
* converting to a non-negative representation and the continuation bit is used to indicate if more bytes follow.
|
|
@@ -208,74 +213,13 @@ export declare function encodeArrayVLQ(values: number[]): string;
|
|
|
208
213
|
* @since 1.0.0
|
|
209
214
|
*/
|
|
210
215
|
export declare function decodeVLQ(data: string): number[];
|
|
216
|
+
|
|
211
217
|
/**
|
|
212
|
-
*
|
|
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
|
|
218
|
+
* Import will remove at compile time
|
|
241
219
|
*/
|
|
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
220
|
/**
|
|
251
|
-
*
|
|
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
|
|
221
|
+
* Imports
|
|
275
222
|
*/
|
|
276
|
-
export interface SegmentOffsetInterface extends SegmentInterface {
|
|
277
|
-
nameIndex: number;
|
|
278
|
-
}
|
|
279
223
|
/**
|
|
280
224
|
* Determines the matching behavior when searching for segments in a source map.
|
|
281
225
|
*
|
|
@@ -297,183 +241,358 @@ export interface SegmentOffsetInterface extends SegmentInterface {
|
|
|
297
241
|
*
|
|
298
242
|
* @since 1.0.0
|
|
299
243
|
*/
|
|
300
|
-
|
|
244
|
+
declare const enum Bias {
|
|
301
245
|
BOUND = 0,
|
|
302
246
|
LOWER_BOUND = 1,
|
|
303
247
|
UPPER_BOUND = 2
|
|
304
248
|
}
|
|
305
249
|
/**
|
|
306
|
-
*
|
|
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
|
|
250
|
+
* A service for validating and processing source maps that provides functionality for parsing,
|
|
251
|
+
* position mapping, concatenation, and code snippet extraction.
|
|
359
252
|
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
253
|
+
* @param source - Source map data (SourceService, SourceMapInterface object, or JSON string)
|
|
254
|
+
* @param file - Optional file name for the generated bundle
|
|
255
|
+
* @returns A new SourceService instance
|
|
362
256
|
*
|
|
363
257
|
* @example
|
|
364
258
|
* ```ts
|
|
365
|
-
*
|
|
366
|
-
* const
|
|
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();
|
|
259
|
+
* const sourceMapJSON = '{"version": 3, "file": "bundle.js", "sources": ["foo.ts"], "names": [], "mappings": "AAAA"}';
|
|
260
|
+
* const sourceService = new SourceService(sourceMapJSON);
|
|
261
|
+
* console.log(sourceService.file); // 'bundle.js'
|
|
373
262
|
* ```
|
|
374
263
|
*
|
|
375
264
|
* @since 1.0.0
|
|
376
265
|
*/
|
|
377
|
-
export declare class
|
|
378
|
-
private mapping;
|
|
266
|
+
export declare class SourceService {
|
|
379
267
|
/**
|
|
380
|
-
*
|
|
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.
|
|
268
|
+
* The name of the generated file this source map applies to.
|
|
393
269
|
*
|
|
394
270
|
* @since 1.0.0
|
|
395
271
|
*/
|
|
396
|
-
|
|
397
|
-
constructor(mapping: MapType, namesOffset?: number, sourceOffset?: number);
|
|
398
|
-
constructor(mapping: MappingProvider, namesOffset?: number, sourceOffset?: number);
|
|
272
|
+
readonly file: string | null;
|
|
399
273
|
/**
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
* @returns VLQ-encoded mapping string compatible with the source map format specification
|
|
274
|
+
* Provider for accessing and manipulating the base64 VLQ-encoded mappings.
|
|
403
275
|
*
|
|
404
|
-
* @
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
276
|
+
* @since 1.0.0
|
|
277
|
+
*/
|
|
278
|
+
readonly mappings: MappingProvider;
|
|
279
|
+
/**
|
|
280
|
+
* The root URL for resolving relative paths in the source files.
|
|
281
|
+
* @since 1.0.0
|
|
282
|
+
*/
|
|
283
|
+
readonly sourceRoot: string | null;
|
|
284
|
+
/**
|
|
285
|
+
* List of symbol names referenced by the mappings.
|
|
286
|
+
* @since 1.0.0
|
|
287
|
+
*/
|
|
288
|
+
readonly names: Array<string>;
|
|
289
|
+
/**
|
|
290
|
+
* Array of source file paths.
|
|
291
|
+
* @since 1.0.0
|
|
292
|
+
*/
|
|
293
|
+
readonly sources: Array<string>;
|
|
294
|
+
/**
|
|
295
|
+
* Array of source file contents.
|
|
296
|
+
* @since 1.0.0
|
|
297
|
+
*/
|
|
298
|
+
readonly sourcesContent: Array<string>;
|
|
299
|
+
/**
|
|
300
|
+
* Creates a new SourceService instance.
|
|
408
301
|
*
|
|
409
|
-
* @
|
|
302
|
+
* @param source - Source map data (SourceService, SourceMapInterface object, or JSON string)
|
|
303
|
+
* @throws Error - When a source map has an invalid format or missing required properties
|
|
410
304
|
*
|
|
411
305
|
* @since 1.0.0
|
|
412
306
|
*/
|
|
413
|
-
|
|
307
|
+
constructor(source: SourceService);
|
|
414
308
|
/**
|
|
415
|
-
*
|
|
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)
|
|
309
|
+
* Creates a new SourceService instance.
|
|
423
310
|
*
|
|
424
|
-
* @
|
|
425
|
-
*
|
|
426
|
-
* The format of the input mapping is automatically detected and processed accordingly.
|
|
311
|
+
* @param source - Source map data (SourceService, SourceMapInterface object, or JSON string)
|
|
312
|
+
* @param file - Optional file name for the generated bundle
|
|
427
313
|
*
|
|
428
|
-
* @
|
|
429
|
-
* @see MappingProvider
|
|
314
|
+
* @throws Error - When a source map has an invalid format or missing required properties
|
|
430
315
|
*
|
|
431
316
|
* @since 1.0.0
|
|
432
317
|
*/
|
|
433
|
-
|
|
318
|
+
constructor(source: SourceMapInterface | string, file?: string | null);
|
|
434
319
|
/**
|
|
435
|
-
*
|
|
320
|
+
* Converts the source map data to a plain object.
|
|
436
321
|
*
|
|
437
|
-
* @
|
|
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
|
|
322
|
+
* @returns A SourceMapInterface object representing the current state
|
|
444
323
|
*
|
|
445
|
-
* @
|
|
446
|
-
*
|
|
447
|
-
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* const mapObject = sourceService.getMapObject();
|
|
327
|
+
* console.log(mapObject.file); // 'bundle.js'
|
|
328
|
+
* ```
|
|
448
329
|
*
|
|
449
330
|
* @since 1.0.0
|
|
450
331
|
*/
|
|
451
|
-
|
|
332
|
+
getMapObject(): SourceMapInterface;
|
|
452
333
|
/**
|
|
453
|
-
*
|
|
334
|
+
* Concatenates additional source maps into the current instance.
|
|
454
335
|
*
|
|
455
|
-
* @param
|
|
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
|
|
336
|
+
* @param maps - Source maps to concatenate with the current map
|
|
463
337
|
*
|
|
464
|
-
* @
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
*
|
|
338
|
+
* @example
|
|
339
|
+
* ```ts
|
|
340
|
+
* sourceService.concat(anotherSourceMap);
|
|
341
|
+
* console.log(sourceService.sources); // Updated source paths
|
|
342
|
+
* ```
|
|
468
343
|
*
|
|
469
|
-
*
|
|
470
|
-
* scan the entire mapping structure.
|
|
344
|
+
* @throws Error - When no maps are provided
|
|
471
345
|
*
|
|
472
346
|
* @since 1.0.0
|
|
473
347
|
*/
|
|
474
|
-
|
|
348
|
+
concat(...maps: Array<SourceMapInterface | SourceService>): void;
|
|
475
349
|
/**
|
|
476
|
-
*
|
|
350
|
+
* Creates a new SourceService instance with concatenated source maps.
|
|
351
|
+
*
|
|
352
|
+
* @param maps - Source maps to concatenate with a copy of the current map
|
|
353
|
+
* @returns A new SourceService instance with the combined maps
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* const newService = sourceService.concatNewMap(anotherSourceMap);
|
|
358
|
+
* console.log(newService.sources); // Combined sources array
|
|
359
|
+
* ```
|
|
360
|
+
*
|
|
361
|
+
* @throws Error - When no maps are provided
|
|
362
|
+
*
|
|
363
|
+
* @since 1.0.0
|
|
364
|
+
*/
|
|
365
|
+
concatNewMap(...maps: Array<SourceMapInterface | SourceService>): SourceService;
|
|
366
|
+
/**
|
|
367
|
+
* Finds position in generated code based on original source position.
|
|
368
|
+
*
|
|
369
|
+
* @param line - Line number in the original source
|
|
370
|
+
* @param column - Column number in the original source
|
|
371
|
+
* @param sourceIndex - Index or file path of the original source
|
|
372
|
+
* @param bias - Position matching strategy (default: Bias.BOUND)
|
|
373
|
+
* @returns Position information or null if not found
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```ts
|
|
377
|
+
* const position = sourceService.getPositionByOriginal(1, 10, 'foo.ts');
|
|
378
|
+
* console.log(position?.generatedLine); // Line in generated code
|
|
379
|
+
* ```
|
|
380
|
+
*
|
|
381
|
+
* @since 1.0.0
|
|
382
|
+
*/
|
|
383
|
+
getPositionByOriginal(line: number, column: number, sourceIndex: number | string, bias?: Bias): PositionInterface | null;
|
|
384
|
+
/**
|
|
385
|
+
* Finds position in an original source based on generated code position.
|
|
386
|
+
*
|
|
387
|
+
* @param line - Line number in the generated code
|
|
388
|
+
* @param column - Column number in the generated code
|
|
389
|
+
* @param bias - Position matching strategy (default: Bias.BOUND)
|
|
390
|
+
* @returns Position information or null if not found
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* const position = sourceService.getPosition(2, 15);
|
|
395
|
+
* console.log(position?.source); // Original source file
|
|
396
|
+
* ```
|
|
397
|
+
*
|
|
398
|
+
* @since 1.0.0
|
|
399
|
+
*/
|
|
400
|
+
getPosition(line: number, column: number, bias?: Bias): PositionInterface | null;
|
|
401
|
+
/**
|
|
402
|
+
* Retrieves position with source content for a location in generated code.
|
|
403
|
+
*
|
|
404
|
+
* @param line - Line number in the generated code
|
|
405
|
+
* @param column - Column number in the generated code
|
|
406
|
+
* @param bias - Position matching strategy (default: Bias.BOUND)
|
|
407
|
+
* @returns Position with content information or null if not found
|
|
408
|
+
*
|
|
409
|
+
* @example
|
|
410
|
+
* ```ts
|
|
411
|
+
* const posWithContent = sourceService.getPositionWithContent(3, 5);
|
|
412
|
+
* console.log(posWithContent?.sourcesContent); // Original source content
|
|
413
|
+
* ```
|
|
414
|
+
*
|
|
415
|
+
* @since 1.0.0
|
|
416
|
+
*/
|
|
417
|
+
getPositionWithContent(line: number, column: number, bias?: Bias): PositionWithContentInterface | null;
|
|
418
|
+
/**
|
|
419
|
+
* Retrieves position with a code snippet from the original source.
|
|
420
|
+
*
|
|
421
|
+
* @param line - Line number in the generated code
|
|
422
|
+
* @param column - Column number in the generated code
|
|
423
|
+
* @param bias - Position matching strategy (default: Bias.BOUND)
|
|
424
|
+
* @param options - Configuration for the amount of surrounding lines
|
|
425
|
+
* @returns Position with code snippet or null if not found
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* ```ts
|
|
429
|
+
* const posWithCode = sourceService.getPositionWithCode(4, 8, Bias.BOUND, {
|
|
430
|
+
* linesBefore: 2,
|
|
431
|
+
* linesAfter: 2
|
|
432
|
+
* });
|
|
433
|
+
* console.log(posWithCode?.code); // Code snippet with context
|
|
434
|
+
* ```
|
|
435
|
+
*
|
|
436
|
+
* @since 1.0.0
|
|
437
|
+
*/
|
|
438
|
+
getPositionWithCode(line: number, column: number, bias?: Bias, options?: SourceOptionsInterface): PositionWithCodeInterface | null;
|
|
439
|
+
/**
|
|
440
|
+
* Serializes the source map to a JSON string.
|
|
441
|
+
*
|
|
442
|
+
* @returns JSON string representation of the source map
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
* ```ts
|
|
446
|
+
* const jsonString = sourceService.toString();
|
|
447
|
+
* console.log(jsonString); // '{"version":3,"file":"bundle.js",...}'
|
|
448
|
+
* ```
|
|
449
|
+
*
|
|
450
|
+
* @since 1.0.0
|
|
451
|
+
*/
|
|
452
|
+
toString(): string;
|
|
453
|
+
/**
|
|
454
|
+
* Validates a source map object has all required properties.
|
|
455
|
+
*
|
|
456
|
+
* @param input - Source map object to validate
|
|
457
|
+
*
|
|
458
|
+
* @throws Error - When required properties are missing
|
|
459
|
+
*
|
|
460
|
+
* @since 1.0.0
|
|
461
|
+
*/
|
|
462
|
+
private validateSourceMap;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Import will remove at compile time
|
|
467
|
+
*/
|
|
468
|
+
/**
|
|
469
|
+
* Imports
|
|
470
|
+
*/
|
|
471
|
+
/**
|
|
472
|
+
* Provides functionality for encoding and decoding source map mappings.
|
|
473
|
+
*
|
|
474
|
+
* The MappingProvider class handles the conversion between various mapping representations:
|
|
475
|
+
* - String format (VLQ-encoded mappings)
|
|
476
|
+
* - Structured array format (MapType)
|
|
477
|
+
* - Internal structured representation
|
|
478
|
+
*
|
|
479
|
+
* It also provides methods to query and retrieve source map segments based on
|
|
480
|
+
* generated or original source positions.
|
|
481
|
+
*
|
|
482
|
+
* @example
|
|
483
|
+
* ```ts
|
|
484
|
+
* // Create from VLQ-encoded mapping string
|
|
485
|
+
* const provider = new MappingProvider(mappingString);
|
|
486
|
+
*
|
|
487
|
+
* // Get a segment by generated position
|
|
488
|
+
* const segment = provider.getSegment(10, 15);
|
|
489
|
+
*
|
|
490
|
+
* // Convert back to mapping string
|
|
491
|
+
* const encoded = provider.encode();
|
|
492
|
+
* ```
|
|
493
|
+
*
|
|
494
|
+
* @since 1.0.0
|
|
495
|
+
*/
|
|
496
|
+
declare class MappingProvider {
|
|
497
|
+
private mapping;
|
|
498
|
+
/**
|
|
499
|
+
* Creates a new MappingProvider instance.
|
|
500
|
+
*
|
|
501
|
+
* @param mapping - Source map mapping data in one of three formats:
|
|
502
|
+
* - VLQ-encoded string
|
|
503
|
+
* - Structured array (MapType)
|
|
504
|
+
* - Another MappingProvider instance (copy constructor)
|
|
505
|
+
* @param namesOffset - Optional offset to apply to name indices (default: 0)
|
|
506
|
+
* @param sourceOffset - Optional offset to apply to source indices (default: 0)
|
|
507
|
+
*
|
|
508
|
+
* @remarks
|
|
509
|
+
* The constructor automatically detects the mapping format and decodes it accordingly.
|
|
510
|
+
* When providing offsets, these values will be added to the corresponding indices
|
|
511
|
+
* in the decoded mapping data, which is useful when concatenating multiple source maps.
|
|
512
|
+
*
|
|
513
|
+
* @since 1.0.0
|
|
514
|
+
*/
|
|
515
|
+
constructor(mapping: string, namesOffset?: number, sourceOffset?: number);
|
|
516
|
+
constructor(mapping: MapType, namesOffset?: number, sourceOffset?: number);
|
|
517
|
+
constructor(mapping: MappingProvider, namesOffset?: number, sourceOffset?: number);
|
|
518
|
+
/**
|
|
519
|
+
* Encodes the internal mapping representation to a VLQ-encoded mapping string.
|
|
520
|
+
*
|
|
521
|
+
* @returns VLQ-encoded mapping string compatible with the source map format specification
|
|
522
|
+
*
|
|
523
|
+
* @remarks
|
|
524
|
+
* This method converts the internal structured mapping representation into a compact
|
|
525
|
+
* string format using Variable Length Quantity (VLQ) encoding.
|
|
526
|
+
* The resulting string follows the source map v3 format for the 'mappings' field.
|
|
527
|
+
*
|
|
528
|
+
* @see https://sourcemaps.info/spec.html
|
|
529
|
+
*
|
|
530
|
+
* @since 1.0.0
|
|
531
|
+
*/
|
|
532
|
+
encode(): string;
|
|
533
|
+
/**
|
|
534
|
+
* Decodes mapping data into the internal representation.
|
|
535
|
+
*
|
|
536
|
+
* @param mapping - Mapping data to decode in one of three formats:
|
|
537
|
+
* - VLQ-encoded string
|
|
538
|
+
* - Structured array (MapType)
|
|
539
|
+
* - Another MappingProvider instance
|
|
540
|
+
* @param namesOffset - Optional offset for name indices (default: 0)
|
|
541
|
+
* @param sourcesOffset - Optional offset for source indices (default: 0)
|
|
542
|
+
*
|
|
543
|
+
* @remarks
|
|
544
|
+
* This method replaces the current internal mapping data with the newly decoded mapping.
|
|
545
|
+
* The format of the input mapping is automatically detected and processed accordingly.
|
|
546
|
+
*
|
|
547
|
+
* @see MapType
|
|
548
|
+
* @see MappingProvider
|
|
549
|
+
*
|
|
550
|
+
* @since 1.0.0
|
|
551
|
+
*/
|
|
552
|
+
decode(mapping: MappingProvider | MapType | string, namesOffset?: number, sourcesOffset?: number): void;
|
|
553
|
+
/**
|
|
554
|
+
* Retrieves a segment based on a position in the generated code.
|
|
555
|
+
*
|
|
556
|
+
* @param generatedLine - Line number in generated code (1-based)
|
|
557
|
+
* @param generatedColumn - Column number in generated code (0-based)
|
|
558
|
+
* @param bias - Controls matching behavior when exact position not found:
|
|
559
|
+
* - BOUND: No preference (default)
|
|
560
|
+
* - LOWER_BOUND: Prefer segment with lower column
|
|
561
|
+
* - UPPER_BOUND: Prefer segment with higher column
|
|
562
|
+
* @returns Matching segment or null if not found
|
|
563
|
+
*
|
|
564
|
+
* @remarks
|
|
565
|
+
* Uses binary search to efficiently locate matching segments.
|
|
566
|
+
* When no exact match is found, the bias parameter determines which nearby segment to return.
|
|
567
|
+
*
|
|
568
|
+
* @since 1.0.0
|
|
569
|
+
*/
|
|
570
|
+
getSegment(generatedLine: number, generatedColumn: number, bias?: Bias): SegmentInterface | null;
|
|
571
|
+
/**
|
|
572
|
+
* Retrieves a segment based on a position in the original source code.
|
|
573
|
+
*
|
|
574
|
+
* @param line - Line number in original source (1-based)
|
|
575
|
+
* @param column - Column number in original source (0-based)
|
|
576
|
+
* @param sourceIndex - Index of source file in the sources array
|
|
577
|
+
* @param bias - Controls matching behavior when exact position not found:
|
|
578
|
+
* - BOUND: No preference (default)
|
|
579
|
+
* - LOWER_BOUND: Prefer segment with lower column
|
|
580
|
+
* - UPPER_BOUND: Prefer segment with higher column
|
|
581
|
+
* @returns Matching segment or null if not found
|
|
582
|
+
*
|
|
583
|
+
* @remarks
|
|
584
|
+
* Searches across all mapping segments to find those matching the specified original source position.
|
|
585
|
+
* When multiple matches are possible, the bias
|
|
586
|
+
* parameter determines which segment to return.
|
|
587
|
+
*
|
|
588
|
+
* This operation is more expensive than getSegment as it must potentially
|
|
589
|
+
* scan the entire mapping structure.
|
|
590
|
+
*
|
|
591
|
+
* @since 1.0.0
|
|
592
|
+
*/
|
|
593
|
+
getOriginalSegment(line: number, column: number, sourceIndex: number, bias?: Bias): SegmentInterface | null;
|
|
594
|
+
/**
|
|
595
|
+
* Initializes a new segment offset object with default values.
|
|
477
596
|
*
|
|
478
597
|
* @param namesOffset - Initial name index offset value (default: 0)
|
|
479
598
|
* @param sourceIndex - Initial source index offset value (default: 0)
|
|
@@ -658,1017 +777,119 @@ export declare class MappingProvider {
|
|
|
658
777
|
*/
|
|
659
778
|
private decodeMappingArray;
|
|
660
779
|
}
|
|
780
|
+
|
|
661
781
|
/**
|
|
662
|
-
*
|
|
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
|
|
782
|
+
* Represents a source map mapping segment that links positions between original and generated code.
|
|
668
783
|
*
|
|
669
|
-
* @
|
|
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
|
-
* ```
|
|
784
|
+
* @interface SegmentInterface
|
|
675
785
|
*
|
|
676
|
-
* @
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
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
|
|
786
|
+
* @property line - Original line number in the source file
|
|
787
|
+
* @property column - Original column number in the source file
|
|
788
|
+
* @property nameIndex - Index of the symbol name in the source map's names array (null if no associated name)
|
|
789
|
+
* @property sourceIndex - Index of the source file in the source map's sources array
|
|
790
|
+
* @property generatedLine - Line number in the generated output code
|
|
791
|
+
* @property generatedColumn - Column number in the generated output code
|
|
1387
792
|
*
|
|
1388
793
|
* @remarks
|
|
1389
|
-
*
|
|
1390
|
-
*
|
|
794
|
+
* These segments form the core data structure of source maps, providing the necessary
|
|
795
|
+
* information to trace locations between original source code and generated output code.
|
|
796
|
+
* Each segment represents a single mapping point in the transformation process.
|
|
1391
797
|
*
|
|
1392
798
|
* @example
|
|
1393
799
|
* ```ts
|
|
1394
|
-
* const
|
|
1395
|
-
*
|
|
1396
|
-
*
|
|
1397
|
-
*
|
|
800
|
+
* const segment: SegmentInterface = {
|
|
801
|
+
* line: 42,
|
|
802
|
+
* column: 10,
|
|
803
|
+
* nameIndex: 5,
|
|
804
|
+
* sourceIndex: 0,
|
|
805
|
+
* generatedLine: 100,
|
|
806
|
+
* generatedColumn: 15
|
|
1398
807
|
* };
|
|
1399
|
-
* const highlightedCode = highlightCode(code, schema);
|
|
1400
|
-
* console.log(highlightedCode);
|
|
1401
808
|
* ```
|
|
1402
809
|
*
|
|
1403
|
-
* @see CodeHighlighter
|
|
1404
|
-
* @see HighlightSchemeInterface
|
|
1405
|
-
*
|
|
1406
810
|
* @since 1.0.0
|
|
1407
811
|
*/
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
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
|
|
812
|
+
interface SegmentInterface {
|
|
813
|
+
line: number;
|
|
814
|
+
column: number;
|
|
815
|
+
nameIndex: number | null;
|
|
816
|
+
sourceIndex: number;
|
|
817
|
+
generatedLine: number;
|
|
818
|
+
generatedColumn: number;
|
|
1471
819
|
}
|
|
1472
820
|
/**
|
|
1473
|
-
*
|
|
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
|
|
821
|
+
* A specialized segment interface used during source map offset calculations.
|
|
1571
822
|
*
|
|
1572
|
-
* @
|
|
1573
|
-
* @returns A StackFrame object containing the parsed information
|
|
823
|
+
* @property nameIndex - Index of the symbol name in the source map's names array (always numeric)
|
|
1574
824
|
*
|
|
1575
825
|
* @remarks
|
|
1576
|
-
*
|
|
1577
|
-
*
|
|
826
|
+
* Unlike the base SegmentInterface where nameIndex can be null,
|
|
827
|
+
* in offset calculations this value must always be a concrete numeric index.
|
|
828
|
+
* This specialized interface ensures type safety during mapping offset operations.
|
|
1578
829
|
*
|
|
1579
830
|
* @example
|
|
1580
831
|
* ```ts
|
|
1581
|
-
*
|
|
1582
|
-
*
|
|
1583
|
-
*
|
|
1584
|
-
* //
|
|
1585
|
-
*
|
|
832
|
+
* const offsetSegment: SegmentOffsetInterface = {
|
|
833
|
+
* line: 42,
|
|
834
|
+
* column: 10,
|
|
835
|
+
* nameIndex: 5, // Must be a number, not null
|
|
836
|
+
* sourceIndex: 0,
|
|
837
|
+
* generatedLine: 100,
|
|
838
|
+
* generatedColumn: 15
|
|
839
|
+
* };
|
|
1586
840
|
* ```
|
|
1587
841
|
*
|
|
1588
|
-
* @see
|
|
1589
|
-
* @see createDefaultFrame
|
|
842
|
+
* @see SegmentInterface
|
|
1590
843
|
*
|
|
1591
|
-
* @since
|
|
844
|
+
* @since 1.0.0
|
|
1592
845
|
*/
|
|
1593
|
-
|
|
846
|
+
interface SegmentOffsetInterface extends SegmentInterface {
|
|
847
|
+
nameIndex: number;
|
|
848
|
+
}
|
|
1594
849
|
/**
|
|
1595
|
-
*
|
|
1596
|
-
*
|
|
1597
|
-
* @param line - The stack trace line to parse
|
|
1598
|
-
* @returns A StackFrame object containing the parsed information
|
|
850
|
+
* Represents a collection of mapping segments for a single line in generated code.
|
|
1599
851
|
*
|
|
1600
852
|
* @remarks
|
|
1601
|
-
*
|
|
1602
|
-
*
|
|
853
|
+
* A frame contains all the segments that map to a particular line of generated code.
|
|
854
|
+
* Each segment within the frame provides mapping information for a specific range
|
|
855
|
+
* of columns within that line.
|
|
1603
856
|
*
|
|
1604
857
|
* @example
|
|
1605
858
|
* ```ts
|
|
1606
|
-
*
|
|
1607
|
-
*
|
|
1608
|
-
*
|
|
1609
|
-
*
|
|
1610
|
-
* parseJavaScriptCoreStackLine("eval code@");
|
|
859
|
+
* const lineFrame: FrameType = [
|
|
860
|
+
* { line: 10, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 5, generatedColumn: 0 },
|
|
861
|
+
* { line: 10, column: 5, nameIndex: 3, sourceIndex: 0, generatedLine: 5, generatedColumn: 10 }
|
|
862
|
+
* ];
|
|
1611
863
|
* ```
|
|
1612
864
|
*
|
|
1613
|
-
* @see
|
|
1614
|
-
* @see createDefaultFrame
|
|
865
|
+
* @see SegmentInterface
|
|
1615
866
|
*
|
|
1616
|
-
* @since
|
|
867
|
+
* @since 1.0.0
|
|
1617
868
|
*/
|
|
1618
|
-
|
|
869
|
+
type FrameType = Array<SegmentInterface>;
|
|
1619
870
|
/**
|
|
1620
|
-
*
|
|
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
|
|
871
|
+
* Represents the complete mapping structure of a source map.
|
|
1625
872
|
*
|
|
1626
873
|
* @remarks
|
|
1627
|
-
*
|
|
1628
|
-
*
|
|
1629
|
-
*
|
|
1630
|
-
*
|
|
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
|
|
874
|
+
* A source map contains an array where each index corresponds to a line in the generated code.
|
|
875
|
+
* Each entry is either:
|
|
876
|
+
* - A frame (array of segments) containing mappings for that line
|
|
877
|
+
* - Null, indicating the line has no mappings (represented by a semicolon in the source map)
|
|
1649
878
|
*
|
|
1650
|
-
*
|
|
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.
|
|
879
|
+
* The array index corresponds directly to the line number in generated code (0-based).
|
|
1654
880
|
*
|
|
1655
881
|
* @example
|
|
1656
882
|
* ```ts
|
|
1657
|
-
*
|
|
1658
|
-
*
|
|
1659
|
-
*
|
|
1660
|
-
*
|
|
1661
|
-
*
|
|
1662
|
-
* console.log(parsedStack.message); // "Something went wrong"
|
|
1663
|
-
* console.log(parsedStack.stack); // Array of StackFrame objects
|
|
1664
|
-
* }
|
|
883
|
+
* const sourceMap: MapType = [
|
|
884
|
+
* [{ line: 1, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 0, generatedColumn: 0 }], // Line 0
|
|
885
|
+
* null, // Line 1 has no mappings
|
|
886
|
+
* [{ line: 2, column: 0, nameIndex: 1, sourceIndex: 0, generatedLine: 2, generatedColumn: 0 }] // Line 2
|
|
887
|
+
* ];
|
|
1665
888
|
* ```
|
|
1666
889
|
*
|
|
1667
|
-
* @see
|
|
1668
|
-
* @see
|
|
1669
|
-
* @see parseStackLine
|
|
1670
|
-
* @see detectJSEngine
|
|
890
|
+
* @see FrameType
|
|
891
|
+
* @see SegmentInterface
|
|
1671
892
|
*
|
|
1672
|
-
* @since
|
|
893
|
+
* @since 1.0.0
|
|
1673
894
|
*/
|
|
1674
|
-
|
|
895
|
+
type MapType = Array<null | FrameType>;
|