@remotex-labs/xmap 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,141 +0,0 @@
1
- /**
2
- * Represents a source map mapping segment that links positions between original and generated code.
3
- *
4
- * @interface SegmentInterface
5
- *
6
- * @property line - Original line number in the source file
7
- * @property column - Original column number in the source file
8
- * @property nameIndex - Index of the symbol name in the source map's names array (null if no associated name)
9
- * @property sourceIndex - Index of the source file in the source map's sources array
10
- * @property generatedLine - Line number in the generated output code
11
- * @property generatedColumn - Column number in the generated output code
12
- *
13
- * @remarks
14
- * These segments form the core data structure of source maps, providing the necessary
15
- * information to trace locations between original source code and generated output code.
16
- * Each segment represents a single mapping point in the transformation process.
17
- *
18
- * @example
19
- * ```ts
20
- * const segment: SegmentInterface = {
21
- * line: 42,
22
- * column: 10,
23
- * nameIndex: 5,
24
- * sourceIndex: 0,
25
- * generatedLine: 100,
26
- * generatedColumn: 15
27
- * };
28
- * ```
29
- *
30
- * @since 1.0.0
31
- */
32
- export interface SegmentInterface {
33
- line: number;
34
- column: number;
35
- nameIndex: number | null;
36
- sourceIndex: number;
37
- generatedLine: number;
38
- generatedColumn: number;
39
- }
40
- /**
41
- * A specialized segment interface used during source map offset calculations.
42
- *
43
- * @property nameIndex - Index of the symbol name in the source map's names array (always numeric)
44
- *
45
- * @remarks
46
- * Unlike the base SegmentInterface where nameIndex can be null,
47
- * in offset calculations this value must always be a concrete numeric index.
48
- * This specialized interface ensures type safety during mapping offset operations.
49
- *
50
- * @example
51
- * ```ts
52
- * const offsetSegment: SegmentOffsetInterface = {
53
- * line: 42,
54
- * column: 10,
55
- * nameIndex: 5, // Must be a number, not null
56
- * sourceIndex: 0,
57
- * generatedLine: 100,
58
- * generatedColumn: 15
59
- * };
60
- * ```
61
- *
62
- * @see SegmentInterface
63
- *
64
- * @since 1.0.0
65
- */
66
- export interface SegmentOffsetInterface extends SegmentInterface {
67
- nameIndex: number;
68
- }
69
- /**
70
- * Determines the matching behavior when searching for segments in a source map.
71
- *
72
- * @property BOUND - No directional preference; returns the first matching segment found
73
- * @property LOWER_BOUND - Prefers segments with column values lower than or equal to the target
74
- * @property UPPER_BOUND - Prefers segments with column values greater than or equal to the target
75
- *
76
- * @remarks
77
- * The bias affects how segment lookups behave when an exact position match cannot be found.
78
- * This provides flexibility in determining which nearby mapping should be preferred when
79
- * working with source map data that might not have exact matches for every position.
80
- *
81
- * @example
82
- * ```ts
83
- * // When searching for a position not exactly in the map:
84
- * findSegmentForPosition(line, column, Bias.LOWER_BOUND); // Prefers the position just before
85
- * findSegmentForPosition(line, column, Bias.UPPER_BOUND); // Prefers the position just after
86
- * ```
87
- *
88
- * @since 1.0.0
89
- */
90
- export declare const enum Bias {
91
- BOUND = 0,
92
- LOWER_BOUND = 1,
93
- UPPER_BOUND = 2
94
- }
95
- /**
96
- * Represents a collection of mapping segments for a single line in generated code.
97
- *
98
- * @remarks
99
- * A frame contains all the segments that map to a particular line of generated code.
100
- * Each segment within the frame provides mapping information for a specific range
101
- * of columns within that line.
102
- *
103
- * @example
104
- * ```ts
105
- * const lineFrame: FrameType = [
106
- * { line: 10, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 5, generatedColumn: 0 },
107
- * { line: 10, column: 5, nameIndex: 3, sourceIndex: 0, generatedLine: 5, generatedColumn: 10 }
108
- * ];
109
- * ```
110
- *
111
- * @see SegmentInterface
112
- *
113
- * @since 1.0.0
114
- */
115
- export type FrameType = Array<SegmentInterface>;
116
- /**
117
- * Represents the complete mapping structure of a source map.
118
- *
119
- * @remarks
120
- * A source map contains an array where each index corresponds to a line in the generated code.
121
- * Each entry is either:
122
- * - A frame (array of segments) containing mappings for that line
123
- * - Null, indicating the line has no mappings (represented by a semicolon in the source map)
124
- *
125
- * The array index corresponds directly to the line number in generated code (0-based).
126
- *
127
- * @example
128
- * ```ts
129
- * const sourceMap: MapType = [
130
- * [{ line: 1, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 0, generatedColumn: 0 }], // Line 0
131
- * null, // Line 1 has no mappings
132
- * [{ line: 2, column: 0, nameIndex: 1, sourceIndex: 0, generatedLine: 2, generatedColumn: 0 }] // Line 2
133
- * ];
134
- * ```
135
- *
136
- * @see FrameType
137
- * @see SegmentInterface
138
- *
139
- * @since 1.0.0
140
- */
141
- export type MapType = Array<null | FrameType>;
@@ -1,317 +0,0 @@
1
- /**
2
- * Import will remove at compile time
3
- */
4
- import type { MapType, SegmentInterface } from './interfaces/mapping-provider.interface';
5
- /**
6
- * Imports
7
- */
8
- import { Bias } from './interfaces/mapping-provider.interface';
9
- /**
10
- * Provides functionality for encoding and decoding source map mappings.
11
- *
12
- * The MappingProvider class handles the conversion between various mapping representations:
13
- * - String format (VLQ-encoded mappings)
14
- * - Structured array format (MapType)
15
- * - Internal structured representation
16
- *
17
- * It also provides methods to query and retrieve source map segments based on
18
- * generated or original source positions.
19
- *
20
- * @example
21
- * ```ts
22
- * // Create from VLQ-encoded mapping string
23
- * const provider = new MappingProvider(mappingString);
24
- *
25
- * // Get a segment by generated position
26
- * const segment = provider.getSegment(10, 15);
27
- *
28
- * // Convert back to mapping string
29
- * const encoded = provider.encode();
30
- * ```
31
- *
32
- * @since 1.0.0
33
- */
34
- export declare class MappingProvider {
35
- private mapping;
36
- /**
37
- * Creates a new MappingProvider instance.
38
- *
39
- * @param mapping - Source map mapping data in one of three formats:
40
- * - VLQ-encoded string
41
- * - Structured array (MapType)
42
- * - Another MappingProvider instance (copy constructor)
43
- * @param namesOffset - Optional offset to apply to name indices (default: 0)
44
- * @param sourceOffset - Optional offset to apply to source indices (default: 0)
45
- *
46
- * @remarks
47
- * The constructor automatically detects the mapping format and decodes it accordingly.
48
- * When providing offsets, these values will be added to the corresponding indices
49
- * in the decoded mapping data, which is useful when concatenating multiple source maps.
50
- *
51
- * @since 1.0.0
52
- */
53
- constructor(mapping: string, namesOffset?: number, sourceOffset?: number);
54
- constructor(mapping: MapType, namesOffset?: number, sourceOffset?: number);
55
- constructor(mapping: MappingProvider, namesOffset?: number, sourceOffset?: number);
56
- /**
57
- * Encodes the internal mapping representation to a VLQ-encoded mapping string.
58
- *
59
- * @returns VLQ-encoded mapping string compatible with the source map format specification
60
- *
61
- * @remarks
62
- * This method converts the internal structured mapping representation into a compact
63
- * string format using Variable Length Quantity (VLQ) encoding.
64
- * The resulting string follows the source map v3 format for the 'mappings' field.
65
- *
66
- * @see https://sourcemaps.info/spec.html
67
- *
68
- * @since 1.0.0
69
- */
70
- encode(): string;
71
- /**
72
- * Decodes mapping data into the internal representation.
73
- *
74
- * @param mapping - Mapping data to decode in one of three formats:
75
- * - VLQ-encoded string
76
- * - Structured array (MapType)
77
- * - Another MappingProvider instance
78
- * @param namesOffset - Optional offset for name indices (default: 0)
79
- * @param sourcesOffset - Optional offset for source indices (default: 0)
80
- *
81
- * @remarks
82
- * This method replaces the current internal mapping data with the newly decoded mapping.
83
- * The format of the input mapping is automatically detected and processed accordingly.
84
- *
85
- * @see MapType
86
- * @see MappingProvider
87
- *
88
- * @since 1.0.0
89
- */
90
- decode(mapping: MappingProvider | MapType | string, namesOffset?: number, sourcesOffset?: number): void;
91
- /**
92
- * Retrieves a segment based on a position in the generated code.
93
- *
94
- * @param generatedLine - Line number in generated code (1-based)
95
- * @param generatedColumn - Column number in generated code (0-based)
96
- * @param bias - Controls matching behavior when exact position not found:
97
- * - BOUND: No preference (default)
98
- * - LOWER_BOUND: Prefer segment with lower column
99
- * - UPPER_BOUND: Prefer segment with higher column
100
- * @returns Matching segment or null if not found
101
- *
102
- * @remarks
103
- * Uses binary search to efficiently locate matching segments.
104
- * When no exact match is found, the bias parameter determines which nearby segment to return.
105
- *
106
- * @since 1.0.0
107
- */
108
- getSegment(generatedLine: number, generatedColumn: number, bias?: Bias): SegmentInterface | null;
109
- /**
110
- * Retrieves a segment based on a position in the original source code.
111
- *
112
- * @param line - Line number in original source (1-based)
113
- * @param column - Column number in original source (0-based)
114
- * @param sourceIndex - Index of source file in the sources array
115
- * @param bias - Controls matching behavior when exact position not found:
116
- * - BOUND: No preference (default)
117
- * - LOWER_BOUND: Prefer segment with lower column
118
- * - UPPER_BOUND: Prefer segment with higher column
119
- * @returns Matching segment or null if not found
120
- *
121
- * @remarks
122
- * Searches across all mapping segments to find those matching the specified original source position.
123
- * When multiple matches are possible, the bias
124
- * parameter determines which segment to return.
125
- *
126
- * This operation is more expensive than getSegment as it must potentially
127
- * scan the entire mapping structure.
128
- *
129
- * @since 1.0.0
130
- */
131
- getOriginalSegment(line: number, column: number, sourceIndex: number, bias?: Bias): SegmentInterface | null;
132
- /**
133
- * Initializes a new segment offset object with default values.
134
- *
135
- * @param namesOffset - Initial name index offset value (default: 0)
136
- * @param sourceIndex - Initial source index offset value (default: 0)
137
- * @returns A new segment offset object with initialized position tracking values
138
- *
139
- * @remarks
140
- * This method creates an object that tracks position data during mapping operations.
141
- * All position values (line, column, generatedLine, generatedColumn) are initialized to 0,
142
- * while the nameIndex and sourceIndex can be initialized with custom offsets.
143
- *
144
- * @since 1.0.0
145
- */
146
- private initPositionOffsets;
147
- /**
148
- * Validates the format of an encoded mapping string.
149
- *
150
- * @param encodedSourceMap - The encoded source map string to validate
151
- * @returns `true` if the string contains only valid VLQ mapping characters, otherwise `false`
152
- *
153
- * @remarks
154
- * Checks if the string contains only characters valid in source map mappings:
155
- * - Base64 characters (a-z, A-Z, 0-9, +, /)
156
- * - Separators (commas for segments, semicolons for lines)
157
- *
158
- * This is a basic format validation and doesn't verify the semantic correctness
159
- * of the VLQ encoding itself.
160
- *
161
- * @since 1.0.0
162
- */
163
- private validateMappingString;
164
- /**
165
- * Validates that a segment's properties conform to expected types.
166
- *
167
- * @param segment - The segment object to validate
168
- *
169
- * @remarks
170
- * Performs the following validations on the segment properties:
171
- * - line: Must be a finite number
172
- * - column: Must be a finite number
173
- * - nameIndex: Must be either null or a finite number
174
- * - sourceIndex: Must be a finite number
175
- * - generatedLine: Must be a finite number
176
- * - generatedColumn: Must be a finite number
177
- *
178
- * This validation ensures that segments can be safely used in mapping operations
179
- * and prevents potential issues with non-numeric or infinite values.
180
- *
181
- * @throws Error - When any property of the segment is invalid, with a message
182
- * indicating which property failed validation and its value
183
- *
184
- * @since 1.0.0
185
- */
186
- private validateSegment;
187
- /**
188
- * Encodes a segment into a VLQ-encoded string based on relative offsets.
189
- *
190
- * @param segmentOffset - The current segment offset tracking state
191
- * @param segmentObject - The segment to encode
192
- * @returns A VLQ-encoded string representation of the segment
193
- *
194
- * @remarks
195
- * The encoding process:
196
- * 1. Adjusts line and column values (subtracts 1 to convert from 1-based to 0-based)
197
- * 2. Calculates relative differences between current values and previous offsets
198
- * 3. Creates an array with the following components:
199
- * - generatedColumn difference
200
- * - sourceIndex difference (0 if unchanged)
201
- * - line difference
202
- * - column difference
203
- * - nameIndex difference (only if nameIndex is present)
204
- * 4. Updates the segment offset state for the next encoding
205
- * 5. Returns the array as a VLQ-encoded string
206
- *
207
- * This method implements the source map V3 specification's delta encoding scheme
208
- * where values are stored as differences from previous positions.
209
- *
210
- * @since 1.0.0
211
- */
212
- private encodeSegment;
213
- /**
214
- * Encodes a mapping array into a VLQ-encoded mapping string following the source map V3 spec.
215
- *
216
- * @param map - The mapping array to encode, organized by generated lines and segments
217
- * @returns A complete VLQ-encoded mapping string with line and segment separators
218
- *
219
- * @remarks
220
- * The encoding process:
221
- * 1. Initializes position offsets to track state across the entire mapping
222
- * 2. Processes each frame (line) in the mapping array:
223
- * - Resets generated column offset to 0 at the start of each line
224
- * - Encodes each segment within the line using relative VLQ encoding
225
- * - Joins segments with commas (,)
226
- * 3. Joins lines with semicolons (;)
227
- *
228
- * Empty frames are preserved as empty strings in the output to maintain
229
- * the correct line numbering in the resulting source map.
230
- *
231
- * @since 1.0.0
232
- */
233
- private encodeMappings;
234
- /**
235
- * Converts a VLQ-decoded segment array into a structured segment object.
236
- *
237
- * @param segmentOffset - The current positional state tracking offsets
238
- * @param decodedSegment - Array of VLQ-decoded values representing relative offsets
239
- * @returns A complete segment object with absolute positions
240
- *
241
- * @remarks
242
- * The decoding process:
243
- * 1. Extracts position values from the decoded array:
244
- * - [0]: generatedColumn delta
245
- * - [1]: sourceIndex delta
246
- * - [2]: sourceLine delta
247
- * - [3]: sourceColumn delta
248
- * - [4]: nameIndex delta (optional)
249
- * 2. Updates the segmentOffset state by adding each delta
250
- * 3. Constructs a segment object with absolute positions (adding 1 to convert
251
- * from 0-based to 1-based coordinates)
252
- * 4. Handles nameIndex appropriately (null if not present in the input)
253
- *
254
- * This method implements the inverse operation of the delta encoding scheme
255
- * defined in the source map V3 specification.
256
- *
257
- * @since 1.0.0
258
- */
259
- private decodedSegment;
260
- /**
261
- * Decodes a VLQ-encoded mapping string into the internal mapping data structure.
262
- *
263
- * @param encodedMap - The VLQ-encoded mapping string from a source map
264
- * @param namesOffset - Base offset for name indices in the global names array
265
- * @param sourceOffset - Base offset for source indices in the global sources array
266
- *
267
- * @remarks
268
- * The decoding process:
269
- * 1. Validates the mapping string format before processing
270
- * 2. Splits the string into frames using semicolons (;) as line separators
271
- * 3. Initializes position offsets with the provided name and source offsets
272
- * 4. For each frame (line):
273
- * - Adds `null` to the mapping array if the frame is empty
274
- * - Resets the generated column offset to 0 for each new line
275
- * - Sets the generated line index using the offset + current index
276
- * - Splits segments using commas (,) and decodes each segment
277
- * - Transforms each decoded segment into a segment object
278
- * 5. Updates the internal mapping array with the decoded data
279
- *
280
- * Error handling includes validation checks and descriptive error messages
281
- * indicating which frame caused a decoding failure.
282
- *
283
- * @throws Error - When the mapping string format is invalid or decoding fails
284
- *
285
- * @since 1.0.0
286
- */
287
- private decodeMappingString;
288
- /**
289
- * Decodes a structured mapping array into the internal mapping representation.
290
- *
291
- * @param encodedMap - The structured mapping array (array of frames, with each frame being an array of segments)
292
- * @param namesOffset - Offset to add to each segment's nameIndex (for merging multiple source maps)
293
- * @param sourceOffset - Offset to add to each segment's sourceIndex (for merging multiple source maps)
294
- *
295
- * @remarks
296
- * The decoding process:
297
- * 1. Validates that the input is a properly structured array
298
- * 2. Tracks the current line offset based on the existing mapping length
299
- * 3. For each frame (line) in the mapping:
300
- * - Preserves null frames as-is (representing empty lines)
301
- * - Validates that each frame is an array
302
- * - For each segment in a frame:
303
- * - Validates the segment structure
304
- * - Applies the name and source offsets
305
- * - Adjusts the generated line index by the line offset
306
- * - Adds the processed frame to the internal mapping array
307
- *
308
- * This method is primarily used when combining multiple source maps or
309
- * importing mapping data from pre-structured arrays rather than VLQ strings.
310
- * The offsets enable proper indexing when concatenating multiple mappings.
311
- *
312
- * @throws Error - When the input format is invalid or segments don't conform to requirements
313
- *
314
- * @since 1.0.0
315
- */
316
- private decodeMappingArray;
317
- }
@@ -1,139 +0,0 @@
1
- /**
2
- * Represents a source map structure used for mapping code within a file to its original source
3
- * @since 1.0.0
4
- */
5
- export interface SourceMapInterface {
6
- /**
7
- * The generated file's name that the source map is associated with
8
- * @since 1.0.0
9
- */
10
- file?: string | null;
11
- /**
12
- * An array of variable/function names present in the original source
13
- * @since 1.0.0
14
- */
15
- names: Array<string>;
16
- /**
17
- * The version of the source map specification (standard is 3)
18
- * @since 1.0.0
19
- */
20
- version: number;
21
- /**
22
- * An array of URLs or paths to the original source files
23
- * @since 1.0.0
24
- */
25
- sources: Array<string>;
26
- /**
27
- * VLQ encoded string that maps generated code back to original source code
28
- * @since 1.0.0
29
- */
30
- mappings: string;
31
- /**
32
- * Root URL for resolving the sources
33
- * @since 1.0.0
34
- */
35
- sourceRoot?: string | null;
36
- /**
37
- * Array containing the content of the original source files
38
- * @since 1.0.0
39
- */
40
- sourcesContent?: Array<string>;
41
- }
42
- /**
43
- * Represents a position in source code with mapping information
44
- * @since 1.0.0
45
- */
46
- export interface PositionInterface {
47
- /**
48
- * Name of the identifier at this position
49
- * @since 1.0.0
50
- */
51
- name: string | null;
52
- /**
53
- * Line number in the original source
54
- * @since 1.0.0
55
- */
56
- line: number;
57
- /**
58
- * Column number in the original source
59
- * @since 1.0.0
60
- */
61
- column: number;
62
- /**
63
- * Path or URL to the original source file
64
- * @since 1.0.0
65
- */
66
- source: string;
67
- /**
68
- * Root URL for resolving the source
69
- * @since 1.0.0
70
- */
71
- sourceRoot: string | null;
72
- /**
73
- * Index of the source in the sources array
74
- * @since 1.0.0
75
- */
76
- sourceIndex: number;
77
- /**
78
- * Line number in the generated code
79
- * @since 1.0.0
80
- */
81
- generatedLine: number;
82
- /**
83
- * Column number in the generated code
84
- * @since 1.0.0
85
- */
86
- generatedColumn: number;
87
- }
88
- /**
89
- * Position in source code including the original source content
90
- *
91
- * @see PositionInterface
92
- * @since 1.0.0
93
- */
94
- export interface PositionWithContentInterface extends PositionInterface {
95
- /**
96
- * Content of the original source file
97
- * @since 1.0.0
98
- */
99
- sourcesContent: string;
100
- }
101
- /**
102
- * Position in source code including code fragment information
103
- *
104
- * @see PositionInterface
105
- * @since 1.0.0
106
- */
107
- export interface PositionWithCodeInterface extends PositionInterface {
108
- /**
109
- * Code fragment from the original source
110
- * @since 1.0.0
111
- */
112
- code: string;
113
- /**
114
- * Ending line number of the code fragment
115
- * @since 1.0.0
116
- */
117
- endLine: number;
118
- /**
119
- * Starting line number of the code fragment
120
- * @since 1.0.0
121
- */
122
- startLine: number;
123
- }
124
- /**
125
- * Options for retrieving source code context
126
- * @since 1.0.0
127
- */
128
- export interface SourceOptionsInterface {
129
- /**
130
- * Number of lines to include after the target line
131
- * @since 1.0.0
132
- */
133
- linesAfter?: number;
134
- /**
135
- * Number of lines to include before the target line
136
- * @since 1.0.0
137
- */
138
- linesBefore?: number;
139
- }