@remotex-labs/xmap 1.1.0 → 2.0.1

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.
Files changed (34) hide show
  1. package/README.md +206 -143
  2. package/dist/{components → cjs/components}/formatter.component.d.ts +2 -2
  3. package/dist/cjs/index.js +7 -0
  4. package/dist/cjs/index.js.map +8 -0
  5. package/dist/cjs/providers/interfaces/mapping.interface.d.ts +52 -0
  6. package/dist/cjs/providers/mapping.provider.d.ts +229 -0
  7. package/dist/cjs/services/interfaces/source.interface.d.ts +53 -0
  8. package/dist/cjs/services/source.service.d.ts +217 -0
  9. package/dist/esm/components/base64.component.d.ts +26 -0
  10. package/dist/esm/components/formatter.component.d.ts +66 -0
  11. package/dist/esm/components/highlighter.component.d.ts +186 -0
  12. package/dist/esm/components/interfaces/formatter.interface.d.ts +42 -0
  13. package/dist/esm/components/interfaces/highlighter.interface.d.ts +48 -0
  14. package/dist/esm/components/interfaces/parse.interface.d.ts +31 -0
  15. package/dist/esm/components/parser.component.d.ts +11 -0
  16. package/dist/esm/index.d.ts +9 -0
  17. package/dist/esm/index.js +7 -0
  18. package/dist/esm/index.js.map +8 -0
  19. package/dist/esm/providers/interfaces/mapping.interface.d.ts +52 -0
  20. package/dist/esm/providers/mapping.provider.d.ts +229 -0
  21. package/dist/esm/services/interfaces/source.interface.d.ts +53 -0
  22. package/dist/esm/services/source.service.d.ts +217 -0
  23. package/package.json +24 -9
  24. package/dist/index.js +0 -9
  25. package/dist/index.js.map +0 -7
  26. package/dist/services/interfaces/source.interface.d.ts +0 -252
  27. package/dist/services/source.service.d.ts +0 -478
  28. /package/dist/{components → cjs/components}/base64.component.d.ts +0 -0
  29. /package/dist/{components → cjs/components}/highlighter.component.d.ts +0 -0
  30. /package/dist/{components → cjs/components}/interfaces/formatter.interface.d.ts +0 -0
  31. /package/dist/{components → cjs/components}/interfaces/highlighter.interface.d.ts +0 -0
  32. /package/dist/{components → cjs/components}/interfaces/parse.interface.d.ts +0 -0
  33. /package/dist/{components → cjs/components}/parser.component.d.ts +0 -0
  34. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
@@ -1,478 +0,0 @@
1
- /**
2
- * Import will remove at compile time
3
- */
4
- import type { PositionInterface, SourceMapInterface, SourceOptionsInterface, PositionSourceInterface } from "./interfaces/source.interface";
5
- /**
6
- * Imports
7
- */
8
- import { Bias } from "./interfaces/source.interface";
9
- /**
10
- * A service for validating and processing source maps.
11
- */
12
- export declare class SourceService {
13
- /**
14
- * The name of the generated file (bundle) that this source map applies to.
15
- */
16
- readonly file: string | null;
17
- /**
18
- * The root URL for the sources, if present in the source map.
19
- */
20
- readonly sourceRoot: string | null;
21
- /**
22
- * A list of symbol names used by the “mappings” entry.
23
- */
24
- private readonly names;
25
- /**
26
- * An array of source file paths.
27
- */
28
- private readonly sources;
29
- /**
30
- * A string of base64 VLQ-encoded mappings.
31
- */
32
- private readonly mappings;
33
- /**
34
- * An array of source files contents.
35
- */
36
- private readonly sourcesContent;
37
- /**
38
- * Creates a new instance of the SourceService class.
39
- *
40
- * This constructor initializes a `SourceService` instance using the provided source map object.
41
- * It performs validation to ensure that the source map contains all required properties.
42
- *
43
- * @param source - An object conforming to the `SourceMapInterface` representing the source map to be validated and processed.
44
- * @throws Error If any required property is missing from the source map object.
45
- */
46
- constructor(source: SourceMapInterface);
47
- /**
48
- * Returns a plain object representation of the source map data.
49
- *
50
- * This method constructs and returns an object that includes key properties of the source map:
51
- * - `version`: The version of the source map specification (typically 3).
52
- * - `file`: The name of the generated file (optional).
53
- * - `names`: An array of function or variable names extracted from the original source code.
54
- * - `sources`: An array of file paths for the source files referenced in the mappings.
55
- * - `sourceRoot`: An optional root URL for resolving source file paths.
56
- * - `mappings`: A base64 VLQ-encoded string representing the source map mappings, generated by `encodeMappings`.
57
- * - `sourcesContent`: An optional array containing the content of the source files. This property may not be present in all source maps.
58
- *
59
- * @returns A plain object conforming to the `SourceMapInterface` structure,
60
- * representing the source map data.
61
- */
62
- getMapObject(): SourceMapInterface;
63
- /**
64
- * Retrieves the source code snippet from the original source based on the specified
65
- * file index, line number, and column number. This method provides context for the
66
- * specified location in the source code, useful for debugging or inspection purposes.
67
- *
68
- * This method can accept both string and numeric file indices. If a string is provided,
69
- * it will search for the corresponding index in the list of source file names. If the
70
- * index is not found or if an invalid location is provided, it will return null.
71
- *
72
- * @param fileIndex - The index of the source file (as a number) or the name of the
73
- * source file (as a string) from which to retrieve the code.
74
- * If a string is provided, the method will search through the
75
- * available sources to find the corresponding index.
76
- * @param line - The line number in the original source file from which to retrieve
77
- * the code snippet.
78
- * @param column - The column number in the original source file that indicates the
79
- * specific position for the code snippet.
80
- * @param options - Optional settings that can customize the behavior of the method:
81
- * - `bias`: Specifies the bias for the mapping retrieval (default is `Bias.LOWER_BOUND`).
82
- * - `linesAfter`: The number of lines to include after the specified line (default is 4).
83
- * - `linesBefore`: The number of lines to include before the specified line (default is 3).
84
- *
85
- * @returns A `PositionSourceInterface` object containing:
86
- * - `code`: A snippet of the original source code surrounding the specified line and column.
87
- * - `line`: The line number in the original source code.
88
- * - `column`: The column number in the original source code.
89
- * - `startLine`: The starting line number of the returned source code snippet.
90
- * - `endLine`: The ending line number of the returned source code snippet.
91
- * - `name`: The name of the function or variable at the matched position, if available.
92
- * - `source`: The file name of the original source.
93
- * - `sourceRoot`: The root directory of the original source file.
94
- *
95
- * @returns Null if no valid source file is found or if no matching position is found
96
- * in the source file.
97
- *
98
- * @example
99
- * ```typescript
100
- * const sourceCode = getSourceCodeByLocation('app.js', 10, 15);
101
- * if (sourceCode) {
102
- * console.log(sourceCode);
103
- * // {
104
- * // code: 'function example() {...}',
105
- * // line: 10,
106
- * // column: 15,
107
- * // startLine: 7,
108
- * // endLine: 11,
109
- * // name: 'example',
110
- * // source: 'app.js',
111
- * // sourceRoot: '/src'
112
- * // }
113
- * }
114
- * ```
115
- */
116
- getSourceCodeByLocation(fileIndex: string, line: number, column: number, options?: SourceOptionsInterface): PositionSourceInterface | null;
117
- getSourceCodeByLocation(fileIndex: number, line: number, column: number, options?: SourceOptionsInterface): PositionSourceInterface | null;
118
- /**
119
- * Retrieves the original source location and optional source code snippet for a given line and column in the generated code.
120
- *
121
- * This function uses the source map to locate the corresponding position in the original source code. Depending on the `options` provided,
122
- * it can include a snippet of the original source code surrounding the located position, offering context for debugging purposes.
123
- *
124
- * @param line - The line number in the generated code.
125
- * @param column - The column number in the generated code.
126
- * @param options - Optional settings for retrieving the source position and context:
127
- * - `linesBefore` (default: 3): Number of lines before the matched source line to include in the returned snippet.
128
- * - `linesAfter` (default: 4): Number of lines after the matched source line to include in the returned snippet.
129
- * - `includeSourceContent` (default: false): When true, includes a snippet of the source code in the result.
130
- * - `bias` (optional): Determines how to resolve ambiguous mappings (e.g., when there are multiple possible matches for the line/column).
131
- *
132
- * @returns An object containing detailed source information, including:
133
- * - `line`: The line number in the original source code.
134
- * - `column`: The column number in the original source code.
135
- * - `startLine`: The starting line number of the returned source code snippet.
136
- * - `endLine`: The ending line number of the returned source code snippet.
137
- * - `name`: The name of the function or variable at the matched position, if available.
138
- * - `source`: The file name of the original source.
139
- * - `sourceRoot`: The root directory of the original source file.
140
- * - `code` (if `includeSourceContent` is true): A snippet of the original source code surrounding the specified line and column.
141
- *
142
- * @returns Null if no matching source position is found.
143
- *
144
- * @example
145
- * ```typescript
146
- * const position = getSourcePosition(10, 5, { linesBefore: 2, linesAfter: 2, includeSourceContent: true });
147
- * console.log(position);
148
- * // {
149
- * // line: 7,
150
- * // column: 15,
151
- * // startLine: 5,
152
- * // endLine: 9,
153
- * // name: 'myFunction',
154
- * // source: 'app.js',
155
- * // sourceRoot: '/src',
156
- * // code: 'function myFunction() {...}'
157
- * // }
158
- * ```
159
- */
160
- getSourcePosition(line: number, column: number, options?: SourceOptionsInterface): PositionSourceInterface | null;
161
- /**
162
- * Retrieves the position information in the original source code for a given line and column in the generated code.
163
- *
164
- * This function locates the corresponding position in the original source code based on the provided line and column in the generated code.
165
- * It uses the specified bias to determine the best match when multiple mappings are possible.
166
- *
167
- * @param line - The line number in the generated code.
168
- * @param column - The column number in the generated code.
169
- * @param bias - Optional parameter specifying how to handle cases where only the line number matches:
170
- * - `Bias.LOWER_BOUND` (default): If the line number matches but the column is less, returns the closest mapping with a lower column.
171
- * - `Bias.UPPER_BOUND`: If the line number matches but the column is greater, returns the closest mapping with a higher column.
172
- * - `Bias.BOUND`: If the line number matches but the column doesn't, returns null.
173
- *
174
- * @returns A `PositionInterface` object representing the position in the original source code, including:
175
- * - `line`: The line number in the original source code.
176
- * - `column`: The column number in the original source code.
177
- * - `name`: The function or variable name at the source position, if available.
178
- * - `source`: The file name of the original source code.
179
- * - `sourceRoot`: The root path of the original source code.
180
- * Returns `null` if no matching position is found.
181
- *
182
- * @example
183
- * ```typescript
184
- * const position = getPosition(10, 5, Bias.LOWER_BOUND);
185
- * console.log(position);
186
- * // {
187
- * // line: 7,
188
- * // column: 15,
189
- * // name: 'myFunction',
190
- * // source: 'app.js',
191
- * // sourceRoot: '/src'
192
- * // }
193
- * ```
194
- */
195
- getPosition(line: number, column: number, bias?: Bias): PositionInterface | null;
196
- /**
197
- * Merges multiple source maps into this source map object.
198
- * The order of the provided source maps must correspond to the order in which the source files were concatenated.
199
- *
200
- * This method appends the names, sources, and source contents from each provided source map to the current source map.
201
- * It also adjusts the mappings to account for the concatenation of the source files.
202
- *
203
- * @param maps - An array of `SourceMapInterface` instances representing the source maps to be merged.
204
- * @throws Error If no source maps are provided for concatenation.
205
- *
206
- * @example
207
- * // Merging two source maps
208
- * const map1: SourceMapInterface = { /* ... *\/ };
209
- * const map2: SourceMapInterface = { /* ... *\/ };
210
- * sourceMapService.concat(map1, map2);
211
- */
212
- concat(...maps: Array<SourceMapInterface>): void;
213
- /**
214
- * Converts the source map object to a JSON string representation.
215
- *
216
- * This method performs the following steps:
217
- * 1. Creates a plain object representation of the source map using the `getMapObject` method.
218
- * 2. Converts the plain object to a JSON string using `JSON.stringify`.
219
- *
220
- * @returns A JSON string representation of the source map.
221
- *
222
- * @example
223
- * const sourceMapString = sourceMapService.toString();
224
- * // sourceMapString contains the JSON string representation of the source map.
225
- */
226
- toString(): string;
227
- /**
228
- * Retrieves the position information in the original source code along with a snippet
229
- * of the surrounding source code based on the provided mapping.
230
- *
231
- * This method takes a mapping object that represents a position in the generated code
232
- * and locates the corresponding position in the original source code. It extracts a
233
- * relevant code snippet surrounding the identified position, providing context for
234
- * debugging or inspection purposes.
235
- *
236
- * @param map - A `MappingInterface` object representing the mapping from the generated
237
- * code to the original source code. If the mapping is null or invalid,
238
- * the method will return null.
239
- * @param settings - An object containing configuration options for retrieving the
240
- * surrounding code snippet, including:
241
- * - `linesAfter`: The number of lines to include after the specified line (default is 4).
242
- * - `linesBefore`: The number of lines to include before the specified line (default is 3).
243
- *
244
- * @returns An object containing detailed source information, including:
245
- * - `line`: The line number in the original source code.
246
- * - `column`: The column number in the original source code.
247
- * - `startLine`: The starting line number of the returned source code snippet.
248
- * - `endLine`: The ending line number of the returned source code snippet.
249
- * - `name`: The name of the function or variable at the matched position, if available.
250
- * - `source`: The file name of the original source.
251
- * - `sourceRoot`: The root directory of the original source file.
252
- * - `code`: A snippet of the original source code surrounding the specified line and column.
253
- *
254
- * @returns Null if no valid mapping is provided or if no matching source position is found.
255
- *
256
- * @example
257
- * ```typescript
258
- * const positionInfo = getPositionWithSource(mapping, { linesAfter: 4, linesBefore: 3 });
259
- * if (positionInfo) {
260
- * console.log(positionInfo);
261
- * // {
262
- * // code: 'function myFunction() {...}',
263
- * // line: 7,
264
- * // column: 15,
265
- * // startLine: 5,
266
- * // endLine: 9,
267
- * // name: 'myFunction',
268
- * // source: 'app.js',
269
- * // sourceRoot: '/src'
270
- * // }
271
- * }
272
- * ```
273
- */
274
- private getPositionWithSource;
275
- /**
276
- * Validates the provided source map object.
277
- *
278
- * This method checks whether all required keys are present in the source map object.
279
- * It throws an error if any required keys are missing.
280
- *
281
- * @private
282
- * @param input - The source map object to be validated.
283
- * @throws Error If any required key is missing from the source map.
284
- *
285
- * @example
286
- * const sourceMap = {
287
- * version: 3,
288
- * file: 'example.js',
289
- * names: ['src', 'maps', 'example', 'function', 'line', 'column'],
290
- * sources: ['source1.js', 'source2.js'],
291
- * mappings: 'AAAA,SAASA,CAAC,CAAC,CAAC;AAAA,CAAC,CAAC;AAAC,CAAC',
292
- * sourcesContent: ['console.log("Hello world");', 'console.log("Another file");']
293
- * };
294
- *
295
- * try {
296
- * validateSourceMap(sourceMap);
297
- * console.log('Source map is valid.');
298
- * } catch (error) {
299
- * console.error('Invalid source map:', error.message);
300
- * }
301
- */
302
- private validateSourceMap;
303
- /**
304
- * Decodes and processes the base64 VLQ-encoded mappings from a source map.
305
- *
306
- * This method interprets the encoded mappings from the `mappings` property of a source map. It adjusts
307
- * the shift state for both generated and original positions in the source code, and processes each
308
- * decoded mapping segment using the `decodedSegment` method.
309
- *
310
- * The decoding process involves:
311
- * 1. Parsing the base64 VLQ-encoded mappings.
312
- * 2. Adjusting the shift state based on the provided `thresholdSegment` or default values.
313
- * 3. Handling each decoded mapping segment with the `decodedSegment` method.
314
- *
315
- * @param encodedMappings - A string representing the encoded mappings in base64 VLQ format. This string
316
- * is typically found in the `mappings` property of a source map.
317
- * @param thresholdSegment - Optional. An object containing offset information that adjusts the starting
318
- * point for decoding. This can include offsets for line, column, or file index.
319
- * If not provided, default values are used.
320
- * @throws Error - Throws an error if the decoding process encounters an issue, such as an invalid
321
- * encoding or unexpected format.
322
- *
323
- * @example
324
- * const encodedMappings = 'AAAA,CAAC,CAAC,CAAC,CAAC;AAAA,CAAC,CAAC,CAAC,CAAC';
325
- * const threshold = {
326
- * fileIndex: 0,
327
- * nameIndex: 0,
328
- * sourceLine: 1,
329
- * sourceColumn: 1,
330
- * generatedLine: 1,
331
- * generatedColumn: 1
332
- * };
333
- * try {
334
- * decodeMappings(encodedMappings, threshold);
335
- * } catch (error) {
336
- * console.error('Failed to decode mappings:', error.message);
337
- * }
338
- */
339
- private decodeMappings;
340
- /**
341
- * Processes a decoded VLQ segment and updates the mapping information.
342
- *
343
- * This method adjusts the current mapping state based on the decoded VLQ segment and the current
344
- * line in the generated code. It then updates the internal mappings list with the new information.
345
- *
346
- * The decoding process involves:
347
- * 1. Extracting the mapping details from the `decodedSegment` array.
348
- * 2. Updating the shift values for file index, name index, source line, source column, and generated column.
349
- * 3. Adding the processed mapping to the internal `mappings` array.
350
- *
351
- * @param shift - The current state of mapping information. This object tracks the cumulative state
352
- * of file index, name index, source line, source column, and generated column.
353
- * @param decodedSegment - An array representing the decoded VLQ segment, containing:
354
- * - `generatedColumn`: The column number in the generated code.
355
- * - `fileIndex`: The index in the sources array.
356
- * - `sourceLine`: The line number in the original source code.
357
- * - `sourceColumn`: The column number in the original source code.
358
- * - `nameIndex`: The index in the names array.
359
- * @param generatedLine - The line index in the generated code where this segment applies.
360
- *
361
- * @example
362
- * const shift = {
363
- * fileIndex: 0,
364
- * nameIndex: 0,
365
- * sourceLine: 1,
366
- * sourceColumn: 1,
367
- * generatedLine: 1,
368
- * generatedColumn: 1
369
- * };
370
- * const decodedSegment = [2, 1, 3, 4, 5];
371
- * const generatedLine = 1;
372
- * this.decodedSegment(shift, decodedSegment, generatedLine);
373
- *
374
- * @see ShiftSegmentInterface for details on the `shift` parameter.
375
- * @see SourceMapInterface for details on the mapping properties.
376
- */
377
- private decodedSegment;
378
- /**
379
- * Encodes an array of mappings into a VLQ-encoded string representation.
380
- *
381
- * This method converts an array of `MappingInterface` objects into a base64 VLQ-encoded string,
382
- * which is used in source maps to represent the mapping between generated and original source code positions.
383
- *
384
- * The encoding process involves:
385
- * 1. Initializing the shift state to track the current line and column in the generated code.
386
- * 2. Iterating through the mappings to group them by line number and encode each segment.
387
- * 3. Concatenating encoded segments with the appropriate separator characters (`,` and `;`).
388
- *
389
- * @param mappings - An array of `MappingInterface` objects representing the mappings to encode. Each mapping object contains:
390
- * - `nameIndex`: The index in the names array.
391
- * - `fileIndex`: The index in the sources array.
392
- * - `sourceLine`: The line number in the original source code.
393
- * - `sourceColumn`: The column number in the original source code.
394
- * - `generatedLine`: The line number in the generated code.
395
- * - `generatedColumn`: The column number in the generated code.
396
- * @returns A VLQ-encoded string representing the mappings, with segments separated by commas and lines by semicolons.
397
- *
398
- * @example
399
- * const mappings = [
400
- * { nameIndex: 0, fileIndex: 1, sourceLine: 2, sourceColumn: 3, generatedLine: 1, generatedColumn: 4 },
401
- * { nameIndex: 1, fileIndex: 1, sourceLine: 3, sourceColumn: 4, generatedLine: 2, generatedColumn: 5 }
402
- * ];
403
- * const encodedMappings = this.encodeMappings(mappings);
404
- * console.log(encodedMappings); // Outputs the VLQ-encoded string
405
- *
406
- * @see MappingInterface for details on the mapping properties.
407
- */
408
- private encodeMappings;
409
- /**
410
- * Encodes a single segment of the mappings into VLQ format.
411
- *
412
- * This method processes a `MappingInterface` object and updates the list of encoded segments. It calculates the differences
413
- * between the current and previous mapping states, then encodes these differences using VLQ (Variable-Length Quantity) encoding.
414
- * The encoded segment is added to the provided `segments` array.
415
- *
416
- * The encoding process involves:
417
- * 1. Calculating the delta values for the file index, source line, source column, and generated column.
418
- * 2. Updating the `shift` state to reflect the current mapping information.
419
- * 3. Encoding the segment using VLQ and adding it to the `segments` array.
420
- *
421
- * @param map - The `MappingInterface` object representing a single mapping to encode. This object contains:
422
- * - `nameIndex`: The index in the names array.
423
- * - `fileIndex`: The index in the sources array.
424
- * - `sourceLine`: The line number in the original source code.
425
- * - `sourceColumn`: The column number in the original source code.
426
- * - `generatedColumn`: The column number in the generated code.
427
- * @param segments - An array of encoded segments that will be updated with the new encoded segment.
428
- * @param shift - The current state of the mapping information, including the latest file index, name index, source line,
429
- * source column, and generated column. This state is updated as new mappings are processed.
430
- *
431
- * @example
432
- * const map: MappingInterface = { nameIndex: 0, fileIndex: 1, sourceLine: 2, sourceColumn: 3, generatedLine: 1, generatedColumn: 4 };
433
- * const segments: Array<string> = [];
434
- * const shift: ShiftSegmentInterface = { fileIndex: 0, nameIndex: 0, sourceLine: 1, sourceColumn: 1, generatedLine: 1, generatedColumn: 1 };
435
- * this.encodeSegment(map, segments, shift);
436
- * console.log(segments); // Outputs the encoded VLQ segment
437
- *
438
- * @see MappingInterface for details on the mapping properties.
439
- * @see encodeArrayVLQ for the encoding function used.
440
- */
441
- private encodeSegment;
442
- /**
443
- * Determines whether a mapping should be skipped based on the specified file index.
444
- *
445
- * This function checks if the given mapping's file index matches the provided file index.
446
- * If the file index is not -1 (which indicates that filtering is desired),
447
- * the function returns true if the mapping's file index does not match the specified file index.
448
- *
449
- * @param mapping - The mapping object to evaluate.
450
- * @param fileIndex - The file index to compare against the mapping's file index.
451
- * A value of -1 indicates that no filtering should occur.
452
- *
453
- * @returns True if the mapping should be skipped, otherwise false.
454
- */
455
- private shouldSkipMapping;
456
- /**
457
- * Retrieves the closest mapping from the mappings array based on the specified line and column.
458
- *
459
- * This function performs a binary search to find the mapping that corresponds to the given line and column
460
- * parameters. It takes into account the specified bias for the column in case of a tie in the line number,
461
- * and can also filter results by a specific file index if provided.
462
- *
463
- * @param line - The target line number to search for in the mappings.
464
- * @param column - The target column number to search for in the mappings.
465
- * @param bias - The bias to use when the line matches; it can be:
466
- * - Bias.BOUND (default): No preference for column matching.
467
- * - Bias.LOWER_BOUND: Prefer the closest mapping with a lower column value.
468
- * - Bias.UPPER_BOUND: Prefer the closest mapping with a higher column value.
469
- * @param fileIndex - The index of the file to filter the mappings by. If set to -1, no filtering is applied.
470
- * @param lineKey - The key to access the line number in the mapping object.
471
- * Can be either 'generatedLine' or 'sourceLine'.
472
- * @param columnKey - The key to access the column number in the mapping object.
473
- * Can be either 'generatedColumn' or 'sourceColumn'.
474
- *
475
- * @returns The closest matching MappingInterface object, or null if no matching mapping is found.
476
- */
477
- private retrieveMapping;
478
- }
File without changes