@remotex-labs/xmap 1.1.0 → 2.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.
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
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Represents a mapping segment that corresponds to a position in the source map.
3
+ *
4
+ * @property line - The original line number in the source file.
5
+ * @property column - The original column number in the source file.
6
+ * @property nameIndex - The index of the symbol name in the source map, or `null` if there is no associated name.
7
+ * @property sourceIndex - The index of the source file in the source map.
8
+ * @property generatedLine - The line number in the generated code.
9
+ * @property generatedColumn - The column number in the generated code.
10
+ */
11
+ export interface SegmentInterface {
12
+ line: number;
13
+ column: number;
14
+ nameIndex: number | null;
15
+ sourceIndex: number;
16
+ generatedLine: number;
17
+ generatedColumn: number;
18
+ }
19
+ /**
20
+ * Extends the `SegmentInterface` to represent an offset segment used during mapping calculations.
21
+ * The main difference is that `nameIndex` is always a number.
22
+ *
23
+ * @augments { SegmentInterface }
24
+ * @property nameIndex - The index of the symbol name in the source map (cannot be null in this context).
25
+ */
26
+ export interface SegmentOffsetInterface extends SegmentInterface {
27
+ nameIndex: number;
28
+ }
29
+ /**
30
+ * Represents the bias used when searching for segments in the source map.
31
+ * This enum is useful for determining the preferred matching behavior
32
+ * when the exact line and column cannot be found.
33
+ *
34
+ * @property BOUND - No preference for column matching; returns the first match found.
35
+ * @property LOWER_BOUND - Prefer the closest mapping with a lower column value.
36
+ * @property UPPER_BOUND - Prefer the closest mapping with a higher column value.
37
+ */
38
+ export declare const enum Bias {
39
+ BOUND = 0,
40
+ LOWER_BOUND = 1,
41
+ UPPER_BOUND = 2
42
+ }
43
+ /**
44
+ * A type alias for a frame in the source map, representing an array of segments.
45
+ * Each frame consists of multiple mapping segments for a given line in the generated code.
46
+ */
47
+ export type FrameType = Array<SegmentInterface>;
48
+ /**
49
+ * A type alias for the source map, where each entry represents a frame of mappings.
50
+ * A frame can either be an array of segments (frame) or `null` if the line has no mappings (represented by a semicolon in the mapping string).
51
+ */
52
+ export type MapType = Array<null | FrameType>;
@@ -0,0 +1,229 @@
1
+ /**
2
+ * Import will remove at compile time
3
+ */
4
+ import { Bias, type MapType, type SegmentInterface } from './interfaces/mapping.interface';
5
+ /**
6
+ * The `MappingProvider` class provides methods to encode and decode mappings
7
+ * from a source map or mapping string to an internal structured representation.
8
+ */
9
+ export declare class MappingProvider {
10
+ /**
11
+ * The internal mapping representation, where each index represents a frame of segments.
12
+ */
13
+ private mapping;
14
+ /**
15
+ * Constructor to initialize the `MappingProvider` with a mapping.
16
+ * Can be initialized with either a mapping string or a structured mapping array.
17
+ *
18
+ * @param mapping - The mapping data, either as a string or structured array.
19
+ * @param namesOffset - Optional offset for the names index.
20
+ * @param sourceOffset - Optional offset for the sources index.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const provider = new MappingProvider(";;;AAiBO,SAAS,OAAO;AACnB,UAAQ,IAAI,MAAM;AACtB;;;ACjBA,QAAQ,IAAI,GAAG;AACf,KAAK;", 0, 0);
25
+ * const provider2 = new MappingProvider([
26
+ * null,
27
+ * [
28
+ * {
29
+ * line: 1,
30
+ * column: 1,
31
+ * nameIndex: null,
32
+ * sourceIndex: 0,
33
+ * generatedLine: 2,
34
+ * generatedColumn: 1
35
+ * }
36
+ * ],
37
+ * null
38
+ * ], 0, 0);
39
+ * ```
40
+ */
41
+ constructor(mapping: string, namesOffset?: number, sourceOffset?: number);
42
+ constructor(mapping: MapType, namesOffset?: number, sourceOffset?: number);
43
+ constructor(mapping: MappingProvider, namesOffset?: number, sourceOffset?: number);
44
+ /**
45
+ * Encodes the internal mapping array back into a mapping string.
46
+ *
47
+ * @returns {string} - The encoded mapping string.
48
+ * @example
49
+ * ```ts
50
+ * const encoded = provider.encode();
51
+ * console.log(encoded); // Outputs encoded mapping string
52
+ * ```
53
+ */
54
+ encode(): string;
55
+ /**
56
+ * Decodes a mapping from either a string or structured array into the internal mapping.
57
+ *
58
+ * @param mapping - The mapping data to decode.
59
+ * @param namesOffset - Offset for the names index.
60
+ * @param sourcesOffset - Offset for the sources index.
61
+ * @example
62
+ * ```ts
63
+ * provider.decode(";;;AAiBO,SAAS,OAAO;AACnB,UAAQ,IAAI,MAAM;AACtB;;;ACjBA,QAAQ,IAAI,GAAG;AACf,KAAK;", 0, 0);
64
+ * provider.decode([
65
+ * null,
66
+ * [
67
+ * {
68
+ * line: 1,
69
+ * column: 1,
70
+ * nameIndex: null,
71
+ * sourceIndex: 0,
72
+ * generatedLine: 2,
73
+ * generatedColumn: 1
74
+ * }
75
+ * ],
76
+ * null
77
+ * ], 0, 0);
78
+ * ```
79
+ */
80
+ decode(mapping: MappingProvider | MapType | string, namesOffset?: number, sourcesOffset?: number): void;
81
+ /**
82
+ * Retrieves a segment based on the provided generated line and column,
83
+ * applying the specified bias when the exact match is not found.
84
+ *
85
+ * This method performs a binary search on the segments of the specified
86
+ * generated line to efficiently locate the segment corresponding to
87
+ * the provided generated column. If an exact match is not found,
88
+ * the method returns the closest segment based on the specified bias:
89
+ * - `Bias.BOUND`: No preference for column matching (returns the closest segment).
90
+ * - `Bias.LOWER_BOUND`: Prefers the closest mapping with a lower column value.
91
+ * - `Bias.UPPER_BOUND`: Prefers the closest mapping with a higher column value.
92
+ *
93
+ * @param generatedLine - The line number of the generated code (1-based index).
94
+ * @param generatedColumn - The column number of the generated code (0-based index).
95
+ * @param bias - The bias to use when the line matches, can be one of:
96
+ * - `Bias.BOUND` (default): No preference for column matching.
97
+ * - `Bias.LOWER_BOUND`: Prefer the closest mapping with a lower column value.
98
+ * - `Bias.UPPER_BOUND`: Prefer the closest mapping with a higher column value.
99
+ * @returns The matching segment if found;
100
+ * returns null if no segments exist for the specified generated line
101
+ * or if the generated line is out of bounds.
102
+ *
103
+ * @throws { Error } - Throws an error if the generated line is invalid
104
+ * (out of bounds).
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * const segment = sourceMap.getSegment(5, 10, Bias.UPPER_BOUND);
109
+ * if (segment) {
110
+ * console.log(`Found segment: line ${segment.line}, column ${segment.column}`);
111
+ * } else {
112
+ * console.log('No matching segment found.');
113
+ * }
114
+ * ```
115
+ */
116
+ getSegment(generatedLine: number, generatedColumn: number, bias?: Bias): SegmentInterface | null;
117
+ /**
118
+ * Retrieves the original segment based on the provided line, column, and source index.
119
+ *
120
+ * This method searches for the original segment that corresponds to the specified
121
+ * line, column, and source index. It uses binary search to find the closest segment
122
+ * based on the provided bias.
123
+ *
124
+ * @param line - The line number of the original code (1-based index).
125
+ * @param column - The column number of the original code (0-based index).
126
+ * @param sourceIndex - The index of the source file in the source map.
127
+ * @param bias - The bias to apply when multiple segments match; defaults to `Bias.BOUND`.
128
+ * @returns {SegmentInterface | null} - The matching original segment if found;
129
+ * returns null if no segments exist for the specified line and source index.
130
+ *
131
+ * @example
132
+ * ```ts
133
+ * const originalSegment = sourceMap.getOriginalSegment(3, 5, 0, Bias.LOWER_BOUND);
134
+ * if (originalSegment) {
135
+ * console.log(`Found original segment: line ${originalSegment.line}, column ${originalSegment.column}`);
136
+ * } else {
137
+ * console.log('No matching original segment found.');
138
+ * }
139
+ * ```
140
+ */
141
+ getOriginalSegment(line: number, column: number, sourceIndex: number, bias?: Bias): SegmentInterface | null;
142
+ /**
143
+ * Initializes the segment offsets used to track the current decoding position.
144
+ *
145
+ * @param namesOffset - The offset for the names index.
146
+ * @param sourceIndex - The offset for the source index.
147
+ * @returns { SegmentOffsetInterface } - The initialized segment offset.
148
+ */
149
+ private initPositionOffsets;
150
+ /**
151
+ * Validates the format of an encoded mapping string.
152
+ *
153
+ * @param encodedSourceMap - The encoded source map string to validate.
154
+ * @returns Returns `true` if the format is valid, otherwise `false`.
155
+ */
156
+ private validateMappingString;
157
+ /**
158
+ * Validates the properties of a segment to ensure they conform to expected types.
159
+ *
160
+ * This method checks that the segment's properties are finite numbers and that
161
+ * the nameIndex, if provided, is either a finite number or null.
162
+ * An error is thrown if any of the properties do not meet the specified criteria.
163
+ *
164
+ * @param segment - The segment object to validate, which must conform to the
165
+ * SegmentInterface structure, including:
166
+ * - line: number (finite)
167
+ * - column: number (finite)
168
+ * - nameIndex: number | null (if not null, must be finite)
169
+ * - sourceIndex: number (finite)
170
+ * - generatedLine: number (finite)
171
+ * - generatedColumn: number (finite)
172
+ *
173
+ * @throws {Error} - Throws an error if any property of the segment is invalid.
174
+ * The error message will specify which property is invalid
175
+ * and the value that was received.
176
+ */
177
+ private validateSegment;
178
+ /**
179
+ * Encodes a segment into a VLQ-encoded string based on the segment offsets.
180
+ *
181
+ * @param segmentOffset - The current segment offset.
182
+ * @param segmentObject - The segment to encode.
183
+ * @returns The encoded segment string.
184
+ */
185
+ private encodeSegment;
186
+ /**
187
+ * Encodes the entire mapping array into a VLQ-encoded mapping string.
188
+ *
189
+ * @param map - The mapping array to encode.
190
+ * @returns The encoded mapping string.
191
+ */
192
+ private encodeMappings;
193
+ /**
194
+ * Decodes a VLQ-encoded segment into a segment object based on the current offset.
195
+ *
196
+ * @param segmentOffset - The current segment offset.
197
+ * @param decodedSegment - The decoded VLQ segment values.
198
+ * @returns The decoded segment object.
199
+ */
200
+ private decodedSegment;
201
+ /**
202
+ * Decodes a VLQ-encoded mapping string into the internal mapping representation.
203
+ *
204
+ * @param encodedMap - The VLQ-encoded mapping string.
205
+ * @param namesOffset - Offset for the names index.
206
+ * @param sourceOffset - Offset for the sources index.
207
+ * @throws { Error } - Throws an error if the mapping string is invalid.
208
+ */
209
+ private decodeMappingString;
210
+ /**
211
+ * Decodes a mapping array into the internal mapping representation, adjusting for offsets.
212
+ *
213
+ * This method processes each frame in the provided structured mapping array,
214
+ * validating each segment within the frame and adjusting the indices based on the
215
+ * specified offsets for names and sources. If a frame is invalid or not an array,
216
+ * an error will be thrown.
217
+ *
218
+ * @param encodedMap - The structured mapping array, which should be an array of frames,
219
+ * where each frame is an array of segments. Each segment must conform
220
+ * to the SegmentInterface.
221
+ * @param namesOffset - Offset for the names index, which will be added to each segment's nameIndex.
222
+ * @param sourceOffset - Offset for the sources index, which will be added to each segment's sourceIndex.
223
+ * @throws { Error } - Throws an error if:
224
+ * - The mapping array is invalid (not an array).
225
+ * - Any frame is not an array.
226
+ * - Any segment does not conform to the SegmentInterface.
227
+ */
228
+ private decodeMappingArray;
229
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Represents a source map structure used for mapping code within a file
3
+ * to its original source. Source maps are essential for debugging minified
4
+ * or transpiled code, as they allow developers to trace back to the
5
+ * original source files and understand the context of the code execution.
6
+ *
7
+ * @interface SourceMapInterface
8
+ * @property file - The generated file's name that the source map is associated with.
9
+ * This property is optional and may be null if not specified.
10
+ * @property names - An array of variable/function names that are present in the original source.
11
+ * These names help in mapping the original code context during debugging.
12
+ * @property version - The version of the source map specification.
13
+ * This should be set to `3` for the standard source map version.
14
+ * @property sources - An array of URLs or paths to the original source files.
15
+ * These sources are used to locate the original code that corresponds to the generated code.
16
+ * @property mappings - A VLQ (Variable-Length Quantity) encoded string that describes how to map the
17
+ * generated code back to the original source code. This property is crucial for the correct functioning of the source map.
18
+ * @property sourceRoot - An optional root URL for the sources.
19
+ * It can be used to specify a base path for resolving the sources, which may be null if not applicable.
20
+ * @property sourcesContent - An optional array containing the content of the original source files.
21
+ * This property can be useful when the original files are not available at runtime.
22
+ */
23
+ export interface SourceMapInterface {
24
+ file?: string | null;
25
+ names: Array<string>;
26
+ version: number;
27
+ sources: Array<string>;
28
+ mappings: string;
29
+ sourceRoot?: string | null;
30
+ sourcesContent?: Array<string>;
31
+ }
32
+ export interface PositionInterface {
33
+ name: string | null;
34
+ line: number;
35
+ column: number;
36
+ source: string;
37
+ sourceRoot: string | null;
38
+ sourceIndex: number;
39
+ generatedLine: number;
40
+ generatedColumn: number;
41
+ }
42
+ export interface PositionWithContentInterface extends PositionInterface {
43
+ sourcesContent: string;
44
+ }
45
+ export interface PositionWithCodeInterface extends PositionInterface {
46
+ code: string;
47
+ endLine: number;
48
+ startLine: number;
49
+ }
50
+ export interface SourceOptionsInterface {
51
+ linesAfter?: number;
52
+ linesBefore?: number;
53
+ }
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Import will remove at compile time
3
+ */
4
+ import type { PositionInterface, SourceMapInterface, SourceOptionsInterface, PositionWithCodeInterface, PositionWithContentInterface } from "./interfaces/source.interface";
5
+ /**
6
+ * Imports
7
+ */
8
+ import { MappingProvider } from "../providers/mapping.provider";
9
+ import { Bias } from "../providers/interfaces/mapping.interface";
10
+ /**
11
+ * A service for validating and processing source maps.
12
+ * This class allows parsing and manipulation of source maps, providing functionality such as
13
+ * retrieving position mappings between original and generated code, concatenating source maps,
14
+ * and getting code snippets based on mappings.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const sourceMapJSON = '{"version": 3, "file": "bundle.js", "sources": ["foo.ts"], "names": [], "mappings": "AAAA"}';
19
+ * const sourceService = new SourceService(sourceMapJSON);
20
+ *
21
+ * console.log(sourceService.file); // Outputs: 'bundle.js'
22
+ * ```
23
+ */
24
+ export declare class SourceService {
25
+ /**
26
+ * The name of the generated file (bundle) that this source map applies to.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * console.log(sourceService.file); // 'bundle.js'
31
+ * ```
32
+ */
33
+ readonly file: string | null;
34
+ /**
35
+ * A MappingProvider instance of base64 VLQ-encoded mappings.
36
+ */
37
+ readonly mappings: MappingProvider;
38
+ /**
39
+ * The root URL for the sources, if present in the source map.
40
+ */
41
+ readonly sourceRoot: string | null;
42
+ /**
43
+ * A list of symbol names used by the “mappings” entry.
44
+ */
45
+ readonly names: Array<string>;
46
+ /**
47
+ * An array of source file paths.
48
+ */
49
+ readonly sources: Array<string>;
50
+ /**
51
+ * An array of source files contents.
52
+ */
53
+ readonly sourcesContent: Array<string>;
54
+ /**
55
+ * Creates a new instance of the `SourceService` class.
56
+ *
57
+ * This constructor initializes the class using either a `SourceMapInterface` object,
58
+ * a JSON string representing the source map, or an existing `SourceService` instance.
59
+ * It validates the source map and populates its properties such as `file`, `sources`, and `mappings`.
60
+ *
61
+ * @param source - Can be one of the following:
62
+ * - An object conforming to the `SourceMapInterface`.
63
+ * - A JSON string representing the source map.
64
+ * - A `SourceService` instance to copy the properties.
65
+ * @param file - (Optional) A string representing the file name of the generated bundle.
66
+ * Defaults to `null`. It will overwrite any existing `file` property in the source map.
67
+ * @throws {Error} - If the source map does not contain required properties or has an invalid format.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * const sourceMapJSON = '{"version": 3, "file": "bundle.js", "sources": ["foo.ts"], "names": [], "mappings": "AAAA"}';
72
+ * const sourceService = new SourceService(sourceMapJSON);
73
+ * ```
74
+ */
75
+ constructor(source: SourceService);
76
+ constructor(source: SourceMapInterface | string, file?: string | null);
77
+ /**
78
+ * Converts the current source map data into a plain object format.
79
+ *
80
+ * @returns The source map json object.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * const mapObject = sourceService.getMapObject();
85
+ * console.log(mapObject.file); // 'bundle.js'
86
+ * ```
87
+ */
88
+ getMapObject(): SourceMapInterface;
89
+ /**
90
+ * Concatenates one or more source maps to the current source map.
91
+ *
92
+ * This method merges additional source maps into the current source map,
93
+ * updating the `mappings`, `names`, `sources`, and `sourcesContent` arrays.
94
+ *
95
+ * @param maps - An array of `SourceMapInterface` or `SourceService` instances to be concatenated.
96
+ * @throws { Error } If no source maps are provided for concatenation.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * sourceService.concat(anotherSourceMap);
101
+ * console.log(sourceService.sources); // Updated source paths
102
+ * ```
103
+ */
104
+ concat(...maps: Array<SourceMapInterface | SourceService>): void;
105
+ /**
106
+ * Creates a new instance of `SourceService` with concatenated source maps.
107
+ *
108
+ * @param maps - An array of `SourceMapInterface` or `SourceService` instances to be concatenated.
109
+ * @returns { SourceService } A new `SourceService` instance with the concatenated maps.
110
+ * @throws { Error } If no source maps are provided.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * const newService = sourceService.concatNewMap(anotherSourceMap);
115
+ * console.log(newService.file); // The file from the new source map
116
+ * ```
117
+ */
118
+ concatNewMap(...maps: Array<SourceMapInterface | SourceService>): SourceService;
119
+ /**
120
+ * Retrieves the position information based on the original source line and column.
121
+ *
122
+ * @param line - The line number in the generated code.
123
+ * @param column - The column number in the generated code.
124
+ * @param sourceIndex - The index or file path of the original source.
125
+ * @param bias - The bias to use when matching positions (`Bias.LOWER_BOUND`, `Bias.UPPER_BOUND`, or `Bias.BOUND`).
126
+ * @returns { PositionInterface | null } The corresponding position in the original source, or `null` if not found.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * const position = sourceService.getPositionByOriginal(1, 10, 'foo.ts');
131
+ * console.log(position?.line); // The line number in the original source
132
+ * ```
133
+ */
134
+ getPositionByOriginal(line: number, column: number, sourceIndex: number | string, bias?: Bias): PositionInterface | null;
135
+ /**
136
+ * Retrieves the position in the original source code based on a given line and column
137
+ * in the generated code.
138
+ *
139
+ * @param line - Line number in the generated code.
140
+ * @param column - Column number in the generated code.
141
+ * @param bias - The bias to use for matching positions. Defaults to `Bias.BOUND`.
142
+ * @returns {PositionInterface | null} The position in the original source, or null if not found.
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * const position = sourceService.getPosition(2, 15);
147
+ * console.log(position?.source); // The original source file
148
+ * ```
149
+ */
150
+ getPosition(line: number, column: number, bias?: Bias): PositionInterface | null;
151
+ /**
152
+ * Retrieves the position and original source content for a given position in the generated code.
153
+ *
154
+ * @param line - Line number in the generated code.
155
+ * @param column - Column number in the generated code.
156
+ * @param bias - Bias used for position matching.
157
+ * @returns { PositionWithContentInterface | null } The position and its associated content, or `null` if not found.
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * const positionWithContent = sourceService.getPositionWithContent(3, 5);
162
+ * console.log(positionWithContent?.sourcesContent); // The source code content
163
+ * ```
164
+ */
165
+ getPositionWithContent(line: number, column: number, bias?: Bias): PositionWithContentInterface | null;
166
+ /**
167
+ * Retrieves the position and a code snippet from the original source based on the given
168
+ * generated code position, with additional lines of code around the matching line.
169
+ *
170
+ * @param line - Line number in the generated code.
171
+ * @param column - Column number in the generated code.
172
+ * @param bias - Bias used for position matching.
173
+ * @param options - (Optional) Extra options for the amount of surrounding lines to include.
174
+ * @returns { PositionWithCodeInterface | null } The position and code snippet.
175
+ *
176
+ * @example
177
+ * ```ts
178
+ * const positionWithCode = sourceService.getPositionWithCode(4, 8, Bias.BOUND, { linesBefore: 2, linesAfter: 2 });
179
+ * console.log(positionWithCode?.code); // The code snippet from the original source
180
+ * ```
181
+ */
182
+ getPositionWithCode(line: number, column: number, bias?: Bias, options?: SourceOptionsInterface): PositionWithCodeInterface | null;
183
+ /**
184
+ * Converts the current source map object to a JSON string.
185
+ *
186
+ * @returns A stringified version of the source map object.
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * console.log(sourceService.toString()); // JSON string of the source map
191
+ * ```
192
+ */
193
+ toString(): string;
194
+ /**
195
+ * Validates the provided source map object.
196
+ *
197
+ * This method checks whether all required keys are present in the source map object.
198
+ * It throws an error if any required keys are missing.
199
+ *
200
+ * @private
201
+ * @param input - The source map object to be validated.
202
+ * @throws Error If any required key is missing from the source map.
203
+ *
204
+ * @example
205
+ * ```ts
206
+ * const sourceMap = {
207
+ * version: 3,
208
+ * file: 'example.js',
209
+ * names: ['src', 'maps', 'example', 'function', 'line', 'column'],
210
+ * sources: ['source1.js', 'source2.js'],
211
+ * mappings: 'AAAA,SAASA,CAAC,CAAC,CAAC;AAAA,CAAC,CAAC;AAAC,CAAC',
212
+ * };
213
+ * sourceService['validateSource'](sourceMap); // Throws if invalid
214
+ * ```
215
+ */
216
+ private validateSourceMap;
217
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Encodes a given number using Variable-Length Quantity (VLQ) encoding.
3
+ * Negative numbers are encoded by converting to a non-negative representation.
4
+ * The continuation bit is used to indicate if more bytes follow.
5
+ *
6
+ * @param value - The number to be encoded.
7
+ * @returns The VLQ encoded string.
8
+ */
9
+ export declare function encodeVLQ(value: number): string;
10
+ /**
11
+ * Encodes an array of numbers using VLQ encoding.
12
+ * Each number in the array is individually encoded and the results are concatenated.
13
+ *
14
+ * @param values - The array of numbers to be encoded.
15
+ * @returns The concatenated VLQ encoded string.
16
+ */
17
+ export declare function encodeArrayVLQ(values: number[]): string;
18
+ /**
19
+ * Decodes a VLQ encoded string back into an array of numbers.
20
+ * Each character is decoded using the Base64 map and continuation bits are processed.
21
+ *
22
+ * @param data - The VLQ encoded string.
23
+ * @returns The array of decoded numbers.
24
+ * @throws Error If the string contains invalid Base64 characters.
25
+ */
26
+ export declare function decodeVLQ(data: string): number[];
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Import will remove at compile time
3
+ */
4
+ import type { PositionWithCodeInterface } from "../services/interfaces/source.interface";
5
+ import type { AnsiOptionInterface, FormatCodeInterface } from "./interfaces/formatter.interface";
6
+ /**
7
+ * Formats a code snippet with optional line padding and custom actions.
8
+ *
9
+ * This function takes a code string and an options object to format the code snippet.
10
+ * It applies padding to line numbers and can trigger custom actions for specific lines.
11
+ *
12
+ * @param code - The source code | stack to be formatted.
13
+ * @param options - Configuration options for formatting the code.
14
+ * - `padding` (number, optional): Number of characters for line number padding. Defaults to 10.
15
+ * - `startLine` (number, optional): The starting line number for formatting. Defaults to 1.
16
+ * - `action` (object, optional): Custom actions to apply to specific lines.
17
+ * - `triggerLine` (number): The line number where the action should be triggered.
18
+ * - `callback` (function): A callback function to format the line string when `triggerLine` is matched.
19
+ * The callback receives the formatted line string, the padding value, and the current line number as arguments.
20
+ *
21
+ * @returns A formatted string of the code snippet with applied padding and custom actions.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const formattedCode = formatCode(code, {
26
+ * padding: 8,
27
+ * startLine: 5,
28
+ * action: {
29
+ * triggerLine: 7,
30
+ * callback: (lineString, padding, lineNumber) => {
31
+ * return `Custom formatting for line ${lineNumber}: ${lineString}`;
32
+ * }
33
+ * }
34
+ * });
35
+ * console.log(formattedCode);
36
+ * ```
37
+ */
38
+ export declare function formatCode(code: string, options?: FormatCodeInterface): string;
39
+ /**
40
+ * Formats a code snippet around an error location with special highlighting.
41
+ *
42
+ * This function takes a `sourcePosition` object containing information about the source code
43
+ * and error location, then uses `formatCode` to format and highlight the relevant code snippet.
44
+ *
45
+ * @param sourcePosition - An object containing information about the source code and error location.
46
+ * - `code` (string): The entire source code content.
47
+ * - `line` (number): The line number where the error occurred (1-based indexing).
48
+ * - `column` (number): The column number within the line where the error occurred (1-based indexing).
49
+ * - `startLine` (number, optional): The starting line number of the code snippet (defaults to 1).
50
+ * @param ansiOption - Optional configuration for ANSI color codes.
51
+ * - `color` (string): The ANSI escape sequence to colorize the error marker and prefix (e.g., `'\x1b[38;5;160m'`).
52
+ * - `reset` (string): The ANSI escape sequence to reset the color (e.g., `'\x1b[0m'`).
53
+ *
54
+ * @throws Error - If the provided `sourcePosition` object has invalid line or column numbers,
55
+ * or if the error line is outside the boundaries of the provided code content.
56
+ *
57
+ * @returns A formatted string representing the relevant code snippet around the error location,
58
+ * including special highlighting for the error line and column.
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const formattedErrorCode = formatErrorCode(sourcePosition);
63
+ * console.log(formattedErrorCode);
64
+ * ```
65
+ */
66
+ export declare function formatErrorCode(sourcePosition: PositionWithCodeInterface, ansiOption?: AnsiOptionInterface): string;