@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
|
@@ -1,9 +1,79 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This file was automatically generated by xBuild.
|
|
4
|
+
* DO NOT EDIT MANUALLY.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Import will remove at compile time
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Export interfaces
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Formats a code snippet with optional line padding and custom actions
|
|
14
|
+
*
|
|
15
|
+
* @param code - The source code | stack to be formatted
|
|
16
|
+
* @param options - Configuration options for formatting the code
|
|
17
|
+
* @returns A formatted string of the code snippet with applied padding and custom actions
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* This function takes a code string and an options object to format the code snippet.
|
|
21
|
+
* It applies padding to line numbers and can trigger custom actions for specific lines.
|
|
22
|
+
* Options include padding (default 10), startLine (default 0), and custom actions for specific lines.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const formattedCode = formatCode(code, {
|
|
27
|
+
* padding: 8,
|
|
28
|
+
* startLine: 5,
|
|
29
|
+
* action: {
|
|
30
|
+
* triggerLine: 7,
|
|
31
|
+
* callback: (lineString, padding, lineNumber) => {
|
|
32
|
+
* return `Custom formatting for line ${lineNumber}: ${lineString}`;
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @since 1.0.0
|
|
39
|
+
*/
|
|
40
|
+
export declare function formatCode(code: string, options?: FormatCodeInterface): string;
|
|
41
|
+
/**
|
|
42
|
+
* Formats a code snippet around an error location with special highlighting
|
|
43
|
+
*
|
|
44
|
+
* @param sourcePosition - An object containing information about the source code and error location
|
|
45
|
+
* @param ansiOption - Optional configuration for ANSI color codes
|
|
46
|
+
* @returns A formatted string representing the relevant code snippet with error highlighting
|
|
47
|
+
*
|
|
48
|
+
* @throws Error - If the provided sourcePosition object has invalid line or column numbers
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* This function takes a sourcePosition object with code content and error location information,
|
|
52
|
+
* then uses formatCode to format and highlight the relevant code snippet around the error.
|
|
53
|
+
* The sourcePosition object should contain code (string), line (number), column (number),
|
|
54
|
+
* and optional startLine (number, defaults to 1).
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* const formattedErrorCode = formatErrorCode({
|
|
59
|
+
* code: "const x = 1;\nconst y = x.undefined;\n",
|
|
60
|
+
* line: 2,
|
|
61
|
+
* column: 15,
|
|
62
|
+
* startLine: 1
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @see formatCode - The underlying function used for basic code formatting
|
|
67
|
+
*
|
|
68
|
+
* @since 1.0.0
|
|
69
|
+
*/
|
|
70
|
+
export declare function formatErrorCode(sourcePosition: PositionWithCodeInterface, ansiOption?: AnsiOptionInterface): string;
|
|
71
|
+
|
|
2
72
|
/**
|
|
3
73
|
* Represents a source map structure used for mapping code within a file to its original source
|
|
4
74
|
* @since 1.0.0
|
|
5
75
|
*/
|
|
6
|
-
|
|
76
|
+
interface SourceMapInterface {
|
|
7
77
|
/**
|
|
8
78
|
* The generated file's name that the source map is associated with
|
|
9
79
|
* @since 1.0.0
|
|
@@ -44,7 +114,7 @@ export interface SourceMapInterface {
|
|
|
44
114
|
* Represents a position in source code with mapping information
|
|
45
115
|
* @since 1.0.0
|
|
46
116
|
*/
|
|
47
|
-
|
|
117
|
+
interface PositionInterface {
|
|
48
118
|
/**
|
|
49
119
|
* Name of the identifier at this position
|
|
50
120
|
* @since 1.0.0
|
|
@@ -92,7 +162,7 @@ export interface PositionInterface {
|
|
|
92
162
|
* @see PositionInterface
|
|
93
163
|
* @since 1.0.0
|
|
94
164
|
*/
|
|
95
|
-
|
|
165
|
+
interface PositionWithContentInterface extends PositionInterface {
|
|
96
166
|
/**
|
|
97
167
|
* Content of the original source file
|
|
98
168
|
* @since 1.0.0
|
|
@@ -105,7 +175,7 @@ export interface PositionWithContentInterface extends PositionInterface {
|
|
|
105
175
|
* @see PositionInterface
|
|
106
176
|
* @since 1.0.0
|
|
107
177
|
*/
|
|
108
|
-
|
|
178
|
+
interface PositionWithCodeInterface extends PositionInterface {
|
|
109
179
|
/**
|
|
110
180
|
* Code fragment from the original source
|
|
111
181
|
* @since 1.0.0
|
|
@@ -126,7 +196,7 @@ export interface PositionWithCodeInterface extends PositionInterface {
|
|
|
126
196
|
* Options for retrieving source code context
|
|
127
197
|
* @since 1.0.0
|
|
128
198
|
*/
|
|
129
|
-
|
|
199
|
+
interface SourceOptionsInterface {
|
|
130
200
|
/**
|
|
131
201
|
* Number of lines to include after the target line
|
|
132
202
|
* @since 1.0.0
|
|
@@ -138,1537 +208,212 @@ export interface SourceOptionsInterface {
|
|
|
138
208
|
*/
|
|
139
209
|
linesBefore?: number;
|
|
140
210
|
}
|
|
211
|
+
|
|
141
212
|
/**
|
|
142
|
-
*
|
|
143
|
-
* converting to a non-negative representation and the continuation bit is used to indicate if more bytes follow.
|
|
144
|
-
*
|
|
145
|
-
* @param value - The number to be encoded
|
|
146
|
-
* @returns The VLQ encoded string represented as Base64 characters
|
|
147
|
-
*
|
|
148
|
-
* @throws Error - If the value cannot be properly encoded
|
|
149
|
-
*
|
|
150
|
-
* @remarks The encoding process shifts the value left by 1 bit to accommodate a sign bit in the least significant position.
|
|
151
|
-
* Negative values have their sign bit set to 1, while positive values have it set to 0.
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* ```ts
|
|
155
|
-
* // Encoding a positive number
|
|
156
|
-
* const encoded = encodeVLQ(25); // Returns "Y"
|
|
157
|
-
*
|
|
158
|
-
* // Encoding a negative number
|
|
159
|
-
* const encodedNegative = encodeVLQ(-25); // Returns "Z"
|
|
160
|
-
* ```
|
|
161
|
-
*
|
|
162
|
-
* @see decodeVLQ - For the corresponding decode function
|
|
163
|
-
*
|
|
164
|
-
* @since 1.0.0
|
|
165
|
-
*/
|
|
166
|
-
export declare function encodeVLQ(value: number): string;
|
|
167
|
-
/**
|
|
168
|
-
* Encodes an array of numbers using VLQ encoding by individually encoding each number and concatenating the results.
|
|
169
|
-
*
|
|
170
|
-
* @param values - The array of numbers to be encoded
|
|
171
|
-
* @returns The concatenated VLQ encoded string
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* ```ts
|
|
175
|
-
* // Encoding multiple values
|
|
176
|
-
* const encoded = encodeArrayVLQ([1, 0, -5]); // Returns "CAAK"
|
|
177
|
-
* ```
|
|
178
|
-
*
|
|
179
|
-
* @see encodeVLQ - The underlying function used to encode each number
|
|
180
|
-
*
|
|
181
|
-
* @since 1.0.0
|
|
182
|
-
*/
|
|
183
|
-
export declare function encodeArrayVLQ(values: number[]): string;
|
|
184
|
-
/**
|
|
185
|
-
* Decodes a VLQ encoded string back into an array of numbers by processing Base64 characters and continuation bits.
|
|
186
|
-
*
|
|
187
|
-
* @param data - The VLQ encoded string
|
|
188
|
-
* @returns The array of decoded numbers
|
|
189
|
-
*
|
|
190
|
-
* @throws Error - If the string contains invalid Base64 characters
|
|
191
|
-
*
|
|
192
|
-
* @remarks The decoding process examines each Base64 character,
|
|
193
|
-
* checking for continuation bits and progressively building up numeric values.
|
|
194
|
-
* The sign bit is extracted from the least significant position
|
|
195
|
-
* to determine if the original number was negative.
|
|
196
|
-
*
|
|
197
|
-
* @example
|
|
198
|
-
* ```ts
|
|
199
|
-
* // Decoding a simple VLQ string
|
|
200
|
-
* const decoded = decodeVLQ("Y"); // Returns [25]
|
|
201
|
-
*
|
|
202
|
-
* // Decoding multiple values
|
|
203
|
-
* const multiDecoded = decodeVLQ("CAAK"); // Returns [1, 0, -5]
|
|
204
|
-
* ```
|
|
205
|
-
*
|
|
206
|
-
* @see encodeVLQ - For the corresponding encode function
|
|
207
|
-
*
|
|
208
|
-
* @since 1.0.0
|
|
213
|
+
* Import will remove at compile time
|
|
209
214
|
*/
|
|
210
|
-
export declare function decodeVLQ(data: string): number[];
|
|
211
215
|
/**
|
|
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.
|
|
216
|
+
* A callback function for formatting code lines
|
|
227
217
|
*
|
|
228
|
-
* @
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
* column: 10,
|
|
233
|
-
* nameIndex: 5,
|
|
234
|
-
* sourceIndex: 0,
|
|
235
|
-
* generatedLine: 100,
|
|
236
|
-
* generatedColumn: 15
|
|
237
|
-
* };
|
|
238
|
-
* ```
|
|
218
|
+
* @param lineString - The content of the line to be formatted
|
|
219
|
+
* @param padding - The amount of padding to be applied to the line
|
|
220
|
+
* @param line - The line number of the line to be formatted
|
|
221
|
+
* @returns Formatted line string
|
|
239
222
|
*
|
|
240
223
|
* @since 1.0.0
|
|
241
224
|
*/
|
|
242
|
-
export
|
|
243
|
-
line: number;
|
|
244
|
-
column: number;
|
|
245
|
-
nameIndex: number | null;
|
|
246
|
-
sourceIndex: number;
|
|
247
|
-
generatedLine: number;
|
|
248
|
-
generatedColumn: number;
|
|
249
|
-
}
|
|
225
|
+
export type FormatCodeCallbackType = (lineString: string, padding: number, line: number) => string;
|
|
250
226
|
/**
|
|
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
|
|
227
|
+
* Configuration options for formatting code
|
|
273
228
|
*
|
|
274
229
|
* @since 1.0.0
|
|
275
230
|
*/
|
|
276
|
-
export interface
|
|
277
|
-
|
|
231
|
+
export interface FormatCodeInterface {
|
|
232
|
+
/**
|
|
233
|
+
* The amount of padding to be applied to each line
|
|
234
|
+
* @since 1.0.0
|
|
235
|
+
*/
|
|
236
|
+
padding?: number;
|
|
237
|
+
/**
|
|
238
|
+
* The starting line number for formatting
|
|
239
|
+
* @since 1.0.0
|
|
240
|
+
*/
|
|
241
|
+
startLine?: number;
|
|
242
|
+
/**
|
|
243
|
+
* An optional action object specifying a line where a callback function should be triggered.
|
|
244
|
+
* @since 1.0.0
|
|
245
|
+
*/
|
|
246
|
+
action?: {
|
|
247
|
+
/**
|
|
248
|
+
* The line number at which the callback function should be triggered.
|
|
249
|
+
* @since 1.0.0
|
|
250
|
+
*/
|
|
251
|
+
triggerLine: number;
|
|
252
|
+
/**
|
|
253
|
+
* The callback function to be executed when the trigger line is encountered.
|
|
254
|
+
* @since 1.0.0
|
|
255
|
+
*/
|
|
256
|
+
callback: FormatCodeCallbackType;
|
|
257
|
+
};
|
|
278
258
|
}
|
|
279
259
|
/**
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
* @property BOUND - No directional preference; returns the first matching segment found
|
|
283
|
-
* @property LOWER_BOUND - Prefers segments with column values lower than or equal to the target
|
|
284
|
-
* @property UPPER_BOUND - Prefers segments with column values greater than or equal to the target
|
|
285
|
-
*
|
|
286
|
-
* @remarks
|
|
287
|
-
* The bias affects how segment lookups behave when an exact position match cannot be found.
|
|
288
|
-
* This provides flexibility in determining which nearby mapping should be preferred when
|
|
289
|
-
* working with source map data that might not have exact matches for every position.
|
|
290
|
-
*
|
|
291
|
-
* @example
|
|
292
|
-
* ```ts
|
|
293
|
-
* // When searching for a position not exactly in the map:
|
|
294
|
-
* findSegmentForPosition(line, column, Bias.LOWER_BOUND); // Prefers the position just before
|
|
295
|
-
* findSegmentForPosition(line, column, Bias.UPPER_BOUND); // Prefers the position just after
|
|
296
|
-
* ```
|
|
297
|
-
*
|
|
260
|
+
* Configuration for ANSI color styling of error pointers
|
|
298
261
|
* @since 1.0.0
|
|
299
262
|
*/
|
|
300
|
-
export
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
263
|
+
export interface AnsiOptionInterface {
|
|
264
|
+
/**
|
|
265
|
+
* ANSI color code to apply to the error pointer
|
|
266
|
+
* @since 1.0.0
|
|
267
|
+
*/
|
|
268
|
+
color: ColorFunctionType;
|
|
304
269
|
}
|
|
270
|
+
|
|
305
271
|
/**
|
|
306
|
-
* Represents a
|
|
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
|
-
* ```
|
|
272
|
+
* Represents a function that applies coloring or formatting to strings.
|
|
320
273
|
*
|
|
321
|
-
* @
|
|
274
|
+
* @param args - Strings to be formatted
|
|
275
|
+
* @returns The formatted string
|
|
322
276
|
*
|
|
323
277
|
* @since 1.0.0
|
|
324
278
|
*/
|
|
325
|
-
|
|
279
|
+
type ColorFunctionType = (...args: Array<string>) => string;
|
|
326
280
|
/**
|
|
327
|
-
*
|
|
281
|
+
* Defines a color scheme for syntax highlighting various code elements.
|
|
328
282
|
*
|
|
329
283
|
* @remarks
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
* - A frame (array of segments) containing mappings for that line
|
|
333
|
-
* - Null, indicating the line has no mappings (represented by a semicolon in the source map)
|
|
334
|
-
*
|
|
335
|
-
* The array index corresponds directly to the line number in generated code (0-based).
|
|
336
|
-
*
|
|
337
|
-
* @example
|
|
338
|
-
* ```ts
|
|
339
|
-
* const sourceMap: MapType = [
|
|
340
|
-
* [{ line: 1, column: 0, nameIndex: null, sourceIndex: 0, generatedLine: 0, generatedColumn: 0 }], // Line 0
|
|
341
|
-
* null, // Line 1 has no mappings
|
|
342
|
-
* [{ line: 2, column: 0, nameIndex: 1, sourceIndex: 0, generatedLine: 2, generatedColumn: 0 }] // Line 2
|
|
343
|
-
* ];
|
|
344
|
-
* ```
|
|
345
|
-
*
|
|
346
|
-
* @see FrameType
|
|
347
|
-
* @see SegmentInterface
|
|
348
|
-
*
|
|
349
|
-
* @since 1.0.0
|
|
350
|
-
*/
|
|
351
|
-
export type MapType = Array<null | FrameType>;
|
|
352
|
-
/**
|
|
353
|
-
* Provides functionality for encoding and decoding source map mappings.
|
|
354
|
-
*
|
|
355
|
-
* The MappingProvider class handles the conversion between various mapping representations:
|
|
356
|
-
* - String format (VLQ-encoded mappings)
|
|
357
|
-
* - Structured array format (MapType)
|
|
358
|
-
* - Internal structured representation
|
|
359
|
-
*
|
|
360
|
-
* It also provides methods to query and retrieve source map segments based on
|
|
361
|
-
* generated or original source positions.
|
|
362
|
-
*
|
|
363
|
-
* @example
|
|
364
|
-
* ```ts
|
|
365
|
-
* // Create from VLQ-encoded mapping string
|
|
366
|
-
* const provider = new MappingProvider(mappingString);
|
|
367
|
-
*
|
|
368
|
-
* // Get a segment by generated position
|
|
369
|
-
* const segment = provider.getSegment(10, 15);
|
|
370
|
-
*
|
|
371
|
-
* // Convert back to mapping string
|
|
372
|
-
* const encoded = provider.encode();
|
|
373
|
-
* ```
|
|
284
|
+
* Each property is a {@link ColorFunctionType} that formats a specific type of syntax element,
|
|
285
|
+
* such as enums, classes, keywords, or literals.
|
|
374
286
|
*
|
|
287
|
+
* @see ColorFunctionType
|
|
375
288
|
* @since 1.0.0
|
|
376
289
|
*/
|
|
377
|
-
|
|
378
|
-
private mapping;
|
|
379
|
-
/**
|
|
380
|
-
* Creates a new MappingProvider instance.
|
|
381
|
-
*
|
|
382
|
-
* @param mapping - Source map mapping data in one of three formats:
|
|
383
|
-
* - VLQ-encoded string
|
|
384
|
-
* - Structured array (MapType)
|
|
385
|
-
* - Another MappingProvider instance (copy constructor)
|
|
386
|
-
* @param namesOffset - Optional offset to apply to name indices (default: 0)
|
|
387
|
-
* @param sourceOffset - Optional offset to apply to source indices (default: 0)
|
|
388
|
-
*
|
|
389
|
-
* @remarks
|
|
390
|
-
* The constructor automatically detects the mapping format and decodes it accordingly.
|
|
391
|
-
* When providing offsets, these values will be added to the corresponding indices
|
|
392
|
-
* in the decoded mapping data, which is useful when concatenating multiple source maps.
|
|
393
|
-
*
|
|
394
|
-
* @since 1.0.0
|
|
395
|
-
*/
|
|
396
|
-
constructor(mapping: string, namesOffset?: number, sourceOffset?: number);
|
|
397
|
-
constructor(mapping: MapType, namesOffset?: number, sourceOffset?: number);
|
|
398
|
-
constructor(mapping: MappingProvider, namesOffset?: number, sourceOffset?: number);
|
|
399
|
-
/**
|
|
400
|
-
* Encodes the internal mapping representation to a VLQ-encoded mapping string.
|
|
401
|
-
*
|
|
402
|
-
* @returns VLQ-encoded mapping string compatible with the source map format specification
|
|
403
|
-
*
|
|
404
|
-
* @remarks
|
|
405
|
-
* This method converts the internal structured mapping representation into a compact
|
|
406
|
-
* string format using Variable Length Quantity (VLQ) encoding.
|
|
407
|
-
* The resulting string follows the source map v3 format for the 'mappings' field.
|
|
408
|
-
*
|
|
409
|
-
* @see https://sourcemaps.info/spec.html
|
|
410
|
-
*
|
|
411
|
-
* @since 1.0.0
|
|
412
|
-
*/
|
|
413
|
-
encode(): string;
|
|
414
|
-
/**
|
|
415
|
-
* Decodes mapping data into the internal representation.
|
|
416
|
-
*
|
|
417
|
-
* @param mapping - Mapping data to decode in one of three formats:
|
|
418
|
-
* - VLQ-encoded string
|
|
419
|
-
* - Structured array (MapType)
|
|
420
|
-
* - Another MappingProvider instance
|
|
421
|
-
* @param namesOffset - Optional offset for name indices (default: 0)
|
|
422
|
-
* @param sourcesOffset - Optional offset for source indices (default: 0)
|
|
423
|
-
*
|
|
424
|
-
* @remarks
|
|
425
|
-
* This method replaces the current internal mapping data with the newly decoded mapping.
|
|
426
|
-
* The format of the input mapping is automatically detected and processed accordingly.
|
|
427
|
-
*
|
|
428
|
-
* @see MapType
|
|
429
|
-
* @see MappingProvider
|
|
430
|
-
*
|
|
431
|
-
* @since 1.0.0
|
|
432
|
-
*/
|
|
433
|
-
decode(mapping: MappingProvider | MapType | string, namesOffset?: number, sourcesOffset?: number): void;
|
|
434
|
-
/**
|
|
435
|
-
* Retrieves a segment based on a position in the generated code.
|
|
436
|
-
*
|
|
437
|
-
* @param generatedLine - Line number in generated code (1-based)
|
|
438
|
-
* @param generatedColumn - Column number in generated code (0-based)
|
|
439
|
-
* @param bias - Controls matching behavior when exact position not found:
|
|
440
|
-
* - BOUND: No preference (default)
|
|
441
|
-
* - LOWER_BOUND: Prefer segment with lower column
|
|
442
|
-
* - UPPER_BOUND: Prefer segment with higher column
|
|
443
|
-
* @returns Matching segment or null if not found
|
|
444
|
-
*
|
|
445
|
-
* @remarks
|
|
446
|
-
* Uses binary search to efficiently locate matching segments.
|
|
447
|
-
* When no exact match is found, the bias parameter determines which nearby segment to return.
|
|
448
|
-
*
|
|
449
|
-
* @since 1.0.0
|
|
450
|
-
*/
|
|
451
|
-
getSegment(generatedLine: number, generatedColumn: number, bias?: Bias): SegmentInterface | null;
|
|
452
|
-
/**
|
|
453
|
-
* Retrieves a segment based on a position in the original source code.
|
|
454
|
-
*
|
|
455
|
-
* @param line - Line number in original source (1-based)
|
|
456
|
-
* @param column - Column number in original source (0-based)
|
|
457
|
-
* @param sourceIndex - Index of source file in the sources array
|
|
458
|
-
* @param bias - Controls matching behavior when exact position not found:
|
|
459
|
-
* - BOUND: No preference (default)
|
|
460
|
-
* - LOWER_BOUND: Prefer segment with lower column
|
|
461
|
-
* - UPPER_BOUND: Prefer segment with higher column
|
|
462
|
-
* @returns Matching segment or null if not found
|
|
463
|
-
*
|
|
464
|
-
* @remarks
|
|
465
|
-
* Searches across all mapping segments to find those matching the specified original source position.
|
|
466
|
-
* When multiple matches are possible, the bias
|
|
467
|
-
* parameter determines which segment to return.
|
|
468
|
-
*
|
|
469
|
-
* This operation is more expensive than getSegment as it must potentially
|
|
470
|
-
* scan the entire mapping structure.
|
|
471
|
-
*
|
|
472
|
-
* @since 1.0.0
|
|
473
|
-
*/
|
|
474
|
-
getOriginalSegment(line: number, column: number, sourceIndex: number, bias?: Bias): SegmentInterface | null;
|
|
290
|
+
interface HighlightSchemeInterface {
|
|
475
291
|
/**
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
* @param namesOffset - Initial name index offset value (default: 0)
|
|
479
|
-
* @param sourceIndex - Initial source index offset value (default: 0)
|
|
480
|
-
* @returns A new segment offset object with initialized position tracking values
|
|
481
|
-
*
|
|
482
|
-
* @remarks
|
|
483
|
-
* This method creates an object that tracks position data during mapping operations.
|
|
484
|
-
* All position values (line, column, generatedLine, generatedColumn) are initialized to 0,
|
|
485
|
-
* while the nameIndex and sourceIndex can be initialized with custom offsets.
|
|
486
|
-
*
|
|
292
|
+
* Color function for enum names.
|
|
487
293
|
* @since 1.0.0
|
|
488
294
|
*/
|
|
489
|
-
|
|
295
|
+
enumColor: ColorFunctionType;
|
|
490
296
|
/**
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
* @param encodedSourceMap - The encoded source map string to validate
|
|
494
|
-
* @returns `true` if the string contains only valid VLQ mapping characters, otherwise `false`
|
|
495
|
-
*
|
|
496
|
-
* @remarks
|
|
497
|
-
* Checks if the string contains only characters valid in source map mappings:
|
|
498
|
-
* - Base64 characters (a-z, A-Z, 0-9, +, /)
|
|
499
|
-
* - Separators (commas for segments, semicolons for lines)
|
|
500
|
-
*
|
|
501
|
-
* This is a basic format validation and doesn't verify the semantic correctness
|
|
502
|
-
* of the VLQ encoding itself.
|
|
503
|
-
*
|
|
297
|
+
* Color function for type names.
|
|
504
298
|
* @since 1.0.0
|
|
505
299
|
*/
|
|
506
|
-
|
|
300
|
+
typeColor: ColorFunctionType;
|
|
507
301
|
/**
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
* @param segment - The segment object to validate
|
|
511
|
-
*
|
|
512
|
-
* @remarks
|
|
513
|
-
* Performs the following validations on the segment properties:
|
|
514
|
-
* - line: Must be a finite number
|
|
515
|
-
* - column: Must be a finite number
|
|
516
|
-
* - nameIndex: Must be either null or a finite number
|
|
517
|
-
* - sourceIndex: Must be a finite number
|
|
518
|
-
* - generatedLine: Must be a finite number
|
|
519
|
-
* - generatedColumn: Must be a finite number
|
|
520
|
-
*
|
|
521
|
-
* This validation ensures that segments can be safely used in mapping operations
|
|
522
|
-
* and prevents potential issues with non-numeric or infinite values.
|
|
523
|
-
*
|
|
524
|
-
* @throws Error - When any property of the segment is invalid, with a message
|
|
525
|
-
* indicating which property failed validation and its value
|
|
526
|
-
*
|
|
302
|
+
* Color function for class names.
|
|
527
303
|
* @since 1.0.0
|
|
528
304
|
*/
|
|
529
|
-
|
|
305
|
+
classColor: ColorFunctionType;
|
|
530
306
|
/**
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
* @param segmentOffset - The current segment offset tracking state
|
|
534
|
-
* @param segmentObject - The segment to encode
|
|
535
|
-
* @returns A VLQ-encoded string representation of the segment
|
|
536
|
-
*
|
|
537
|
-
* @remarks
|
|
538
|
-
* The encoding process:
|
|
539
|
-
* 1. Adjusts line and column values (subtracts 1 to convert from 1-based to 0-based)
|
|
540
|
-
* 2. Calculates relative differences between current values and previous offsets
|
|
541
|
-
* 3. Creates an array with the following components:
|
|
542
|
-
* - generatedColumn difference
|
|
543
|
-
* - sourceIndex difference (0 if unchanged)
|
|
544
|
-
* - line difference
|
|
545
|
-
* - column difference
|
|
546
|
-
* - nameIndex difference (only if nameIndex is present)
|
|
547
|
-
* 4. Updates the segment offset state for the next encoding
|
|
548
|
-
* 5. Returns the array as a VLQ-encoded string
|
|
549
|
-
*
|
|
550
|
-
* This method implements the source map V3 specification's delta encoding scheme
|
|
551
|
-
* where values are stored as differences from previous positions.
|
|
552
|
-
*
|
|
307
|
+
* Color function for string literals.
|
|
553
308
|
* @since 1.0.0
|
|
554
309
|
*/
|
|
555
|
-
|
|
310
|
+
stringColor: ColorFunctionType;
|
|
556
311
|
/**
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
* @param map - The mapping array to encode, organized by generated lines and segments
|
|
560
|
-
* @returns A complete VLQ-encoded mapping string with line and segment separators
|
|
561
|
-
*
|
|
562
|
-
* @remarks
|
|
563
|
-
* The encoding process:
|
|
564
|
-
* 1. Initializes position offsets to track state across the entire mapping
|
|
565
|
-
* 2. Processes each frame (line) in the mapping array:
|
|
566
|
-
* - Resets generated column offset to 0 at the start of each line
|
|
567
|
-
* - Encodes each segment within the line using relative VLQ encoding
|
|
568
|
-
* - Joins segments with commas (,)
|
|
569
|
-
* 3. Joins lines with semicolons (;)
|
|
570
|
-
*
|
|
571
|
-
* Empty frames are preserved as empty strings in the output to maintain
|
|
572
|
-
* the correct line numbering in the resulting source map.
|
|
573
|
-
*
|
|
312
|
+
* Color function for language keywords.
|
|
574
313
|
* @since 1.0.0
|
|
575
314
|
*/
|
|
576
|
-
|
|
577
|
-
/**
|
|
578
|
-
* Converts a VLQ-decoded segment array into a structured segment object.
|
|
579
|
-
*
|
|
580
|
-
* @param segmentOffset - The current positional state tracking offsets
|
|
581
|
-
* @param decodedSegment - Array of VLQ-decoded values representing relative offsets
|
|
582
|
-
* @returns A complete segment object with absolute positions
|
|
583
|
-
*
|
|
584
|
-
* @remarks
|
|
585
|
-
* The decoding process:
|
|
586
|
-
* 1. Extracts position values from the decoded array:
|
|
587
|
-
* - [0]: generatedColumn delta
|
|
588
|
-
* - [1]: sourceIndex delta
|
|
589
|
-
* - [2]: sourceLine delta
|
|
590
|
-
* - [3]: sourceColumn delta
|
|
591
|
-
* - [4]: nameIndex delta (optional)
|
|
592
|
-
* 2. Updates the segmentOffset state by adding each delta
|
|
593
|
-
* 3. Constructs a segment object with absolute positions (adding 1 to convert
|
|
594
|
-
* from 0-based to 1-based coordinates)
|
|
595
|
-
* 4. Handles nameIndex appropriately (null if not present in the input)
|
|
596
|
-
*
|
|
597
|
-
* This method implements the inverse operation of the delta encoding scheme
|
|
598
|
-
* defined in the source map V3 specification.
|
|
599
|
-
*
|
|
600
|
-
* @since 1.0.0
|
|
601
|
-
*/
|
|
602
|
-
private decodedSegment;
|
|
603
|
-
/**
|
|
604
|
-
* Decodes a VLQ-encoded mapping string into the internal mapping data structure.
|
|
605
|
-
*
|
|
606
|
-
* @param encodedMap - The VLQ-encoded mapping string from a source map
|
|
607
|
-
* @param namesOffset - Base offset for name indices in the global names array
|
|
608
|
-
* @param sourceOffset - Base offset for source indices in the global sources array
|
|
609
|
-
*
|
|
610
|
-
* @remarks
|
|
611
|
-
* The decoding process:
|
|
612
|
-
* 1. Validates the mapping string format before processing
|
|
613
|
-
* 2. Splits the string into frames using semicolons (;) as line separators
|
|
614
|
-
* 3. Initializes position offsets with the provided name and source offsets
|
|
615
|
-
* 4. For each frame (line):
|
|
616
|
-
* - Adds `null` to the mapping array if the frame is empty
|
|
617
|
-
* - Resets the generated column offset to 0 for each new line
|
|
618
|
-
* - Sets the generated line index using the offset + current index
|
|
619
|
-
* - Splits segments using commas (,) and decodes each segment
|
|
620
|
-
* - Transforms each decoded segment into a segment object
|
|
621
|
-
* 5. Updates the internal mapping array with the decoded data
|
|
622
|
-
*
|
|
623
|
-
* Error handling includes validation checks and descriptive error messages
|
|
624
|
-
* indicating which frame caused a decoding failure.
|
|
625
|
-
*
|
|
626
|
-
* @throws Error - When the mapping string format is invalid or decoding fails
|
|
627
|
-
*
|
|
628
|
-
* @since 1.0.0
|
|
629
|
-
*/
|
|
630
|
-
private decodeMappingString;
|
|
631
|
-
/**
|
|
632
|
-
* Decodes a structured mapping array into the internal mapping representation.
|
|
633
|
-
*
|
|
634
|
-
* @param encodedMap - The structured mapping array (array of frames, with each frame being an array of segments)
|
|
635
|
-
* @param namesOffset - Offset to add to each segment's nameIndex (for merging multiple source maps)
|
|
636
|
-
* @param sourceOffset - Offset to add to each segment's sourceIndex (for merging multiple source maps)
|
|
637
|
-
*
|
|
638
|
-
* @remarks
|
|
639
|
-
* The decoding process:
|
|
640
|
-
* 1. Validates that the input is a properly structured array
|
|
641
|
-
* 2. Tracks the current line offset based on the existing mapping length
|
|
642
|
-
* 3. For each frame (line) in the mapping:
|
|
643
|
-
* - Preserves null frames as-is (representing empty lines)
|
|
644
|
-
* - Validates that each frame is an array
|
|
645
|
-
* - For each segment in a frame:
|
|
646
|
-
* - Validates the segment structure
|
|
647
|
-
* - Applies the name and source offsets
|
|
648
|
-
* - Adjusts the generated line index by the line offset
|
|
649
|
-
* - Adds the processed frame to the internal mapping array
|
|
650
|
-
*
|
|
651
|
-
* This method is primarily used when combining multiple source maps or
|
|
652
|
-
* importing mapping data from pre-structured arrays rather than VLQ strings.
|
|
653
|
-
* The offsets enable proper indexing when concatenating multiple mappings.
|
|
654
|
-
*
|
|
655
|
-
* @throws Error - When the input format is invalid or segments don't conform to requirements
|
|
656
|
-
*
|
|
657
|
-
* @since 1.0.0
|
|
658
|
-
*/
|
|
659
|
-
private decodeMappingArray;
|
|
660
|
-
}
|
|
661
|
-
/**
|
|
662
|
-
* A service for validating and processing source maps that provides functionality for parsing,
|
|
663
|
-
* position mapping, concatenation, and code snippet extraction.
|
|
664
|
-
*
|
|
665
|
-
* @param source - Source map data (SourceService, SourceMapInterface object, or JSON string)
|
|
666
|
-
* @param file - Optional file name for the generated bundle
|
|
667
|
-
* @returns A new SourceService instance
|
|
668
|
-
*
|
|
669
|
-
* @example
|
|
670
|
-
* ```ts
|
|
671
|
-
* const sourceMapJSON = '{"version": 3, "file": "bundle.js", "sources": ["foo.ts"], "names": [], "mappings": "AAAA"}';
|
|
672
|
-
* const sourceService = new SourceService(sourceMapJSON);
|
|
673
|
-
* console.log(sourceService.file); // 'bundle.js'
|
|
674
|
-
* ```
|
|
675
|
-
*
|
|
676
|
-
* @since 1.0.0
|
|
677
|
-
*/
|
|
678
|
-
export declare class SourceService {
|
|
315
|
+
keywordColor: ColorFunctionType;
|
|
679
316
|
/**
|
|
680
|
-
*
|
|
681
|
-
*
|
|
317
|
+
* Color function for comments.
|
|
682
318
|
* @since 1.0.0
|
|
683
319
|
*/
|
|
684
|
-
|
|
320
|
+
commentColor: ColorFunctionType;
|
|
685
321
|
/**
|
|
686
|
-
*
|
|
687
|
-
*
|
|
322
|
+
* Color function for function names.
|
|
688
323
|
* @since 1.0.0
|
|
689
324
|
*/
|
|
690
|
-
|
|
325
|
+
functionColor: ColorFunctionType;
|
|
691
326
|
/**
|
|
692
|
-
*
|
|
327
|
+
* Color function for variable names.
|
|
693
328
|
* @since 1.0.0
|
|
694
329
|
*/
|
|
695
|
-
|
|
330
|
+
variableColor: ColorFunctionType;
|
|
696
331
|
/**
|
|
697
|
-
*
|
|
332
|
+
* Color function for interface names.
|
|
698
333
|
* @since 1.0.0
|
|
699
334
|
*/
|
|
700
|
-
|
|
335
|
+
interfaceColor: ColorFunctionType;
|
|
701
336
|
/**
|
|
702
|
-
*
|
|
337
|
+
* Color function for function/method parameters.
|
|
703
338
|
* @since 1.0.0
|
|
704
339
|
*/
|
|
705
|
-
|
|
340
|
+
parameterColor: ColorFunctionType;
|
|
706
341
|
/**
|
|
707
|
-
*
|
|
342
|
+
* Color function for getter accessor names.
|
|
708
343
|
* @since 1.0.0
|
|
709
344
|
*/
|
|
710
|
-
|
|
345
|
+
getAccessorColor: ColorFunctionType;
|
|
711
346
|
/**
|
|
712
|
-
*
|
|
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
|
-
*
|
|
347
|
+
* Color function for numeric literals.
|
|
719
348
|
* @since 1.0.0
|
|
720
349
|
*/
|
|
721
|
-
|
|
722
|
-
constructor(source: SourceMapInterface | string, file?: string | null);
|
|
350
|
+
numericLiteralColor: ColorFunctionType;
|
|
723
351
|
/**
|
|
724
|
-
*
|
|
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
|
-
*
|
|
352
|
+
* Color function for method signatures.
|
|
734
353
|
* @since 1.0.0
|
|
735
354
|
*/
|
|
736
|
-
|
|
355
|
+
methodSignatureColor: ColorFunctionType;
|
|
737
356
|
/**
|
|
738
|
-
*
|
|
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
|
-
*
|
|
357
|
+
* Color function for regular expressions.
|
|
750
358
|
* @since 1.0.0
|
|
751
359
|
*/
|
|
752
|
-
|
|
360
|
+
regularExpressionColor: ColorFunctionType;
|
|
753
361
|
/**
|
|
754
|
-
*
|
|
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
|
-
*
|
|
362
|
+
* Color function for property assignments.
|
|
767
363
|
* @since 1.0.0
|
|
768
364
|
*/
|
|
769
|
-
|
|
365
|
+
propertyAssignmentColor: ColorFunctionType;
|
|
770
366
|
/**
|
|
771
|
-
*
|
|
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
|
-
*
|
|
367
|
+
* Color function for property access expressions.
|
|
785
368
|
* @since 1.0.0
|
|
786
369
|
*/
|
|
787
|
-
|
|
370
|
+
propertyAccessExpressionColor: ColorFunctionType;
|
|
788
371
|
/**
|
|
789
|
-
*
|
|
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
|
-
*
|
|
372
|
+
* Color function for expressions with type arguments.
|
|
802
373
|
* @since 1.0.0
|
|
803
374
|
*/
|
|
804
|
-
|
|
375
|
+
expressionWithTypeArgumentsColor: ColorFunctionType;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Represents a segment of source code to be highlighted with specific styling.
|
|
379
|
+
*
|
|
380
|
+
* @remarks
|
|
381
|
+
* Segments are the fundamental units of the highlighting system.
|
|
382
|
+
* Each segment represents a portion of text that should receive specific styling.
|
|
383
|
+
* When the source code is processed for display,
|
|
384
|
+
* these segments are used to insert the appropriate color/style codes at the correct positions.
|
|
385
|
+
*
|
|
386
|
+
* The highlighter maintains a collection of these segments and applies them
|
|
387
|
+
* in position order to create the complete highlighted output.
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* ```ts
|
|
391
|
+
* const keywordSegment: HighlightNodeSegmentInterface = {
|
|
392
|
+
* start: 0,
|
|
393
|
+
* end: 6,
|
|
394
|
+
* color: xterm.red
|
|
395
|
+
* };
|
|
396
|
+
* ```
|
|
397
|
+
*
|
|
398
|
+
* @see addSegment
|
|
399
|
+
* @see HighlightSchemeInterface
|
|
400
|
+
*
|
|
401
|
+
* @since 1.0.0
|
|
402
|
+
*/
|
|
403
|
+
interface HighlightNodeSegmentInterface {
|
|
805
404
|
/**
|
|
806
|
-
*
|
|
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
|
-
*
|
|
405
|
+
* The starting character position of the segment in the source text.
|
|
819
406
|
* @since 1.0.0
|
|
820
407
|
*/
|
|
821
|
-
|
|
408
|
+
start: number;
|
|
822
409
|
/**
|
|
823
|
-
*
|
|
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
|
-
*
|
|
410
|
+
* The ending character position of the segment in the source text.
|
|
840
411
|
* @since 1.0.0
|
|
841
412
|
*/
|
|
842
|
-
|
|
413
|
+
end: number;
|
|
843
414
|
/**
|
|
844
|
-
*
|
|
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
|
-
*
|
|
415
|
+
* The color or style code to apply to this segment.
|
|
854
416
|
* @since 1.0.0
|
|
855
417
|
*/
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
* Validates a source map object has all required properties.
|
|
859
|
-
*
|
|
860
|
-
* @param input - Source map object to validate
|
|
861
|
-
*
|
|
862
|
-
* @throws Error - When required properties are missing
|
|
863
|
-
*
|
|
864
|
-
* @since 1.0.0
|
|
865
|
-
*/
|
|
866
|
-
private validateSourceMap;
|
|
867
|
-
}
|
|
868
|
-
/**
|
|
869
|
-
* A callback function for formatting code lines
|
|
870
|
-
*
|
|
871
|
-
* @param lineString - The content of the line to be formatted
|
|
872
|
-
* @param padding - The amount of padding to be applied to the line
|
|
873
|
-
* @param line - The line number of the line to be formatted
|
|
874
|
-
* @returns Formatted line string
|
|
875
|
-
*
|
|
876
|
-
* @since 1.0.0
|
|
877
|
-
*/
|
|
878
|
-
export type FormatCodeCallbackType = (lineString: string, padding: number, line: number) => string;
|
|
879
|
-
/**
|
|
880
|
-
* Configuration options for formatting code
|
|
881
|
-
*
|
|
882
|
-
* @since 1.0.0
|
|
883
|
-
*/
|
|
884
|
-
export interface FormatCodeInterface {
|
|
885
|
-
/**
|
|
886
|
-
* The amount of padding to be applied to each line
|
|
887
|
-
* @since 1.0.0
|
|
888
|
-
*/
|
|
889
|
-
padding?: number;
|
|
890
|
-
/**
|
|
891
|
-
* The starting line number for formatting
|
|
892
|
-
* @since 1.0.0
|
|
893
|
-
*/
|
|
894
|
-
startLine?: number;
|
|
895
|
-
/**
|
|
896
|
-
* An optional action object specifying a line where a callback function should be triggered.
|
|
897
|
-
* @since 1.0.0
|
|
898
|
-
*/
|
|
899
|
-
action?: {
|
|
900
|
-
/**
|
|
901
|
-
* The line number at which the callback function should be triggered.
|
|
902
|
-
* @since 1.0.0
|
|
903
|
-
*/
|
|
904
|
-
triggerLine: number;
|
|
905
|
-
/**
|
|
906
|
-
* The callback function to be executed when the trigger line is encountered.
|
|
907
|
-
* @since 1.0.0
|
|
908
|
-
*/
|
|
909
|
-
callback: FormatCodeCallbackType;
|
|
910
|
-
};
|
|
911
|
-
}
|
|
912
|
-
/**
|
|
913
|
-
* Configuration for ANSI color styling of error pointers
|
|
914
|
-
* @since 1.0.0
|
|
915
|
-
*/
|
|
916
|
-
export interface AnsiOptionInterface {
|
|
917
|
-
/**
|
|
918
|
-
* ANSI color code to apply to the error pointer
|
|
919
|
-
* @since 1.0.0
|
|
920
|
-
*/
|
|
921
|
-
color: string;
|
|
922
|
-
/**
|
|
923
|
-
* ANSI reset code to restore default styling after the error pointer
|
|
924
|
-
* @since 1.0.0
|
|
925
|
-
*/
|
|
926
|
-
reset: string;
|
|
927
|
-
}
|
|
928
|
-
/**
|
|
929
|
-
* Formats a code snippet with optional line padding and custom actions
|
|
930
|
-
*
|
|
931
|
-
* @param code - The source code | stack to be formatted
|
|
932
|
-
* @param options - Configuration options for formatting the code
|
|
933
|
-
* @returns A formatted string of the code snippet with applied padding and custom actions
|
|
934
|
-
*
|
|
935
|
-
* @remarks
|
|
936
|
-
* This function takes a code string and an options object to format the code snippet.
|
|
937
|
-
* It applies padding to line numbers and can trigger custom actions for specific lines.
|
|
938
|
-
* Options include padding (default 10), startLine (default 0), and custom actions for specific lines.
|
|
939
|
-
*
|
|
940
|
-
* @example
|
|
941
|
-
* ```ts
|
|
942
|
-
* const formattedCode = formatCode(code, {
|
|
943
|
-
* padding: 8,
|
|
944
|
-
* startLine: 5,
|
|
945
|
-
* action: {
|
|
946
|
-
* triggerLine: 7,
|
|
947
|
-
* callback: (lineString, padding, lineNumber) => {
|
|
948
|
-
* return `Custom formatting for line ${lineNumber}: ${lineString}`;
|
|
949
|
-
* }
|
|
950
|
-
* }
|
|
951
|
-
* });
|
|
952
|
-
* ```
|
|
953
|
-
*
|
|
954
|
-
* @since 1.0.0
|
|
955
|
-
*/
|
|
956
|
-
export declare function formatCode(code: string, options?: FormatCodeInterface): string;
|
|
957
|
-
/**
|
|
958
|
-
* Formats a code snippet around an error location with special highlighting
|
|
959
|
-
*
|
|
960
|
-
* @param sourcePosition - An object containing information about the source code and error location
|
|
961
|
-
* @param ansiOption - Optional configuration for ANSI color codes
|
|
962
|
-
* @returns A formatted string representing the relevant code snippet with error highlighting
|
|
963
|
-
*
|
|
964
|
-
* @throws Error - If the provided sourcePosition object has invalid line or column numbers
|
|
965
|
-
*
|
|
966
|
-
* @remarks
|
|
967
|
-
* This function takes a sourcePosition object with code content and error location information,
|
|
968
|
-
* then uses formatCode to format and highlight the relevant code snippet around the error.
|
|
969
|
-
* The sourcePosition object should contain code (string), line (number), column (number),
|
|
970
|
-
* and optional startLine (number, defaults to 1).
|
|
971
|
-
*
|
|
972
|
-
* @example
|
|
973
|
-
* ```ts
|
|
974
|
-
* const formattedErrorCode = formatErrorCode({
|
|
975
|
-
* code: "const x = 1;\nconst y = x.undefined;\n",
|
|
976
|
-
* line: 2,
|
|
977
|
-
* column: 15,
|
|
978
|
-
* startLine: 1
|
|
979
|
-
* });
|
|
980
|
-
* ```
|
|
981
|
-
*
|
|
982
|
-
* @see formatCode - The underlying function used for basic code formatting
|
|
983
|
-
*
|
|
984
|
-
* @since 1.0.0
|
|
985
|
-
*/
|
|
986
|
-
export declare function formatErrorCode(sourcePosition: PositionWithCodeInterface, ansiOption?: AnsiOptionInterface): string;
|
|
987
|
-
/**
|
|
988
|
-
* Defines the color scheme for syntax highlighting different code elements.
|
|
989
|
-
*
|
|
990
|
-
* @interface HighlightSchemeInterface
|
|
991
|
-
*
|
|
992
|
-
* @property enumColor - Color code for enum declarations and references
|
|
993
|
-
* @property typeColor - Color code for type annotations and primitive types
|
|
994
|
-
* @property classColor - Color code for class declarations and references
|
|
995
|
-
* @property stringColor - Color code for string literals and template strings
|
|
996
|
-
* @property keywordColor - Color code for language keywords
|
|
997
|
-
* @property commentColor - Color code for comments (single-line and multi-line)
|
|
998
|
-
* @property functionColor - Color code for function declarations and calls
|
|
999
|
-
* @property variableColor - Color code for variable declarations and references
|
|
1000
|
-
* @property interfaceColor - Color code for interface declarations and references
|
|
1001
|
-
* @property parameterColor - Color code for function and method parameters
|
|
1002
|
-
* @property getAccessorColor - Color code for getter accessor methods
|
|
1003
|
-
* @property numericLiteralColor - Color code for numeric literals
|
|
1004
|
-
* @property methodSignatureColor - Color code for method signatures in interfaces
|
|
1005
|
-
* @property regularExpressionColor - Color code for regular expression literals
|
|
1006
|
-
* @property propertyAssignmentColor - Color code for property assignments in object literals
|
|
1007
|
-
* @property propertyAccessExpressionColor - Color code for property access expressions
|
|
1008
|
-
* @property expressionWithTypeArgumentsColor - Color code for type arguments in expressions
|
|
1009
|
-
*
|
|
1010
|
-
* @example
|
|
1011
|
-
* ```ts
|
|
1012
|
-
* const darkTheme: HighlightSchemeInterface = {
|
|
1013
|
-
* enumColor: Colors.cyan,
|
|
1014
|
-
* typeColor: Colors.blue,
|
|
1015
|
-
* classColor: Colors.yellow,
|
|
1016
|
-
* // ...other color definitions
|
|
1017
|
-
* };
|
|
1018
|
-
* ```
|
|
1019
|
-
*
|
|
1020
|
-
* @since 1.0.0
|
|
1021
|
-
*/
|
|
1022
|
-
export interface HighlightSchemeInterface {
|
|
1023
|
-
enumColor: string;
|
|
1024
|
-
typeColor: string;
|
|
1025
|
-
classColor: string;
|
|
1026
|
-
stringColor: string;
|
|
1027
|
-
keywordColor: string;
|
|
1028
|
-
commentColor: string;
|
|
1029
|
-
functionColor: string;
|
|
1030
|
-
variableColor: string;
|
|
1031
|
-
interfaceColor: string;
|
|
1032
|
-
parameterColor: string;
|
|
1033
|
-
getAccessorColor: string;
|
|
1034
|
-
numericLiteralColor: string;
|
|
1035
|
-
methodSignatureColor: string;
|
|
1036
|
-
regularExpressionColor: string;
|
|
1037
|
-
propertyAssignmentColor: string;
|
|
1038
|
-
propertyAccessExpressionColor: string;
|
|
1039
|
-
expressionWithTypeArgumentsColor: string;
|
|
1040
|
-
}
|
|
1041
|
-
/**
|
|
1042
|
-
* Represents a segment of source code to be highlighted with specific styling.
|
|
1043
|
-
*
|
|
1044
|
-
* @interface HighlightNodeSegmentInterface
|
|
1045
|
-
*
|
|
1046
|
-
* @property start - The starting character position of the segment in the source text
|
|
1047
|
-
* @property end - The ending character position of the segment in the source text
|
|
1048
|
-
* @property color - The color or style code to apply to this segment
|
|
1049
|
-
* @property reset - The reset code that returns formatting to normal after this segment
|
|
1050
|
-
*
|
|
1051
|
-
* @remarks
|
|
1052
|
-
* Segments are the fundamental units of the highlighting system.
|
|
1053
|
-
* Each segment represents a portion of text that should receive specific styling.
|
|
1054
|
-
* When the source code is processed for display,
|
|
1055
|
-
* these segments are used to insert the appropriate color/style codes at the correct positions.
|
|
1056
|
-
*
|
|
1057
|
-
* The highlighter maintains a collection of these segments and applies them
|
|
1058
|
-
* in position order to create the complete highlighted output.
|
|
1059
|
-
*
|
|
1060
|
-
* @example
|
|
1061
|
-
* ```ts
|
|
1062
|
-
* const keywordSegment: HighlightNodeSegmentInterface = {
|
|
1063
|
-
* start: 0,
|
|
1064
|
-
* end: 6,
|
|
1065
|
-
* color: '\x1b[34m', // Blue for the keyword "import"
|
|
1066
|
-
* reset: '\x1b[0m' // Reset to default
|
|
1067
|
-
* };
|
|
1068
|
-
* ```
|
|
1069
|
-
*
|
|
1070
|
-
* @see HighlightSchemeInterface
|
|
1071
|
-
* @see addSegment
|
|
1072
|
-
*
|
|
1073
|
-
* @since 1.0.0
|
|
1074
|
-
*/
|
|
1075
|
-
export interface HighlightNodeSegmentInterface {
|
|
1076
|
-
end: number;
|
|
1077
|
-
start: number;
|
|
1078
|
-
color: string;
|
|
1079
|
-
reset: string;
|
|
1080
|
-
}
|
|
1081
|
-
/**
|
|
1082
|
-
* An enum containing ANSI escape sequences for various colors
|
|
1083
|
-
*
|
|
1084
|
-
* @remarks
|
|
1085
|
-
* This enum is primarily intended for terminal output and won't work directly in JavaScript for web development.
|
|
1086
|
-
* It defines color codes for various colors and a reset code to return to the default text color.
|
|
1087
|
-
*
|
|
1088
|
-
* @example
|
|
1089
|
-
* ```ts
|
|
1090
|
-
* console.log(`${Colors.red}This text will be red in the terminal.${Colors.reset}`);
|
|
1091
|
-
* ```
|
|
1092
|
-
*
|
|
1093
|
-
* @since 1.0.0
|
|
1094
|
-
*/
|
|
1095
|
-
export declare const enum Colors {
|
|
1096
|
-
reset = "\u001B[0m",
|
|
1097
|
-
gray = "\u001B[38;5;243m",
|
|
1098
|
-
darkGray = "\u001B[38;5;238m",
|
|
1099
|
-
lightCoral = "\u001B[38;5;203m",
|
|
1100
|
-
lightOrange = "\u001B[38;5;215m",
|
|
1101
|
-
oliveGreen = "\u001B[38;5;149m",
|
|
1102
|
-
burntOrange = "\u001B[38;5;208m",
|
|
1103
|
-
lightGoldenrodYellow = "\u001B[38;5;221m",
|
|
1104
|
-
lightYellow = "\u001B[38;5;230m",
|
|
1105
|
-
canaryYellow = "\u001B[38;5;227m",
|
|
1106
|
-
deepOrange = "\u001B[38;5;166m",
|
|
1107
|
-
lightGray = "\u001B[38;5;252m",
|
|
1108
|
-
brightPink = "\u001B[38;5;197m"
|
|
1109
|
-
}
|
|
1110
|
-
/**
|
|
1111
|
-
* Class responsible for applying semantic highlighting to a source code string based on a given color scheme
|
|
1112
|
-
*
|
|
1113
|
-
* @remarks
|
|
1114
|
-
* Processes TypeScript AST nodes and applies color formatting to different code elements
|
|
1115
|
-
* according to the provided color scheme.
|
|
1116
|
-
*
|
|
1117
|
-
* @example
|
|
1118
|
-
* ```ts
|
|
1119
|
-
* const sourceFile = ts.createSourceFile('example.ts', code, ts.ScriptTarget.Latest);
|
|
1120
|
-
* const highlighter = new CodeHighlighter(sourceFile, code, customScheme);
|
|
1121
|
-
* highlighter.parseNode(sourceFile);
|
|
1122
|
-
* const highlightedCode = highlighter.highlight();
|
|
1123
|
-
* ```
|
|
1124
|
-
*
|
|
1125
|
-
* @since 1.0.0
|
|
1126
|
-
*/
|
|
1127
|
-
export declare class CodeHighlighter {
|
|
1128
|
-
private sourceFile;
|
|
1129
|
-
private code;
|
|
1130
|
-
private schema;
|
|
1131
|
-
/**
|
|
1132
|
-
* A Map of segments where the key is a combination of start and end positions.
|
|
1133
|
-
*
|
|
1134
|
-
* @remarks
|
|
1135
|
-
* This structure ensures unique segments and allows for fast lookups and updates.
|
|
1136
|
-
*
|
|
1137
|
-
* @see HighlightNodeSegmentInterface
|
|
1138
|
-
* @since 1.0.0
|
|
1139
|
-
*/
|
|
1140
|
-
private segments;
|
|
1141
|
-
/**
|
|
1142
|
-
* Creates an instance of the CodeHighlighter class.
|
|
1143
|
-
*
|
|
1144
|
-
* @param sourceFile - The TypeScript AST node representing the source file
|
|
1145
|
-
* @param code - The source code string to be highlighted
|
|
1146
|
-
* @param schema - The color scheme used for highlighting different elements in the code
|
|
1147
|
-
*
|
|
1148
|
-
* @since 1.0.0
|
|
1149
|
-
*/
|
|
1150
|
-
constructor(sourceFile: ts.Node, code: string, schema: HighlightSchemeInterface);
|
|
1151
|
-
/**
|
|
1152
|
-
* Parses a TypeScript AST node and processes its comments to identify segments that need highlighting.
|
|
1153
|
-
*
|
|
1154
|
-
* @param node - The TypeScript AST node to be parsed
|
|
1155
|
-
*
|
|
1156
|
-
* @since 1.0.0
|
|
1157
|
-
*/
|
|
1158
|
-
parseNode(node: ts.Node): void;
|
|
1159
|
-
/**
|
|
1160
|
-
* Generates a string with highlighted code segments based on the provided color scheme.
|
|
1161
|
-
*
|
|
1162
|
-
* @returns The highlighted code as a string, with ANSI color codes applied to the segments
|
|
1163
|
-
*
|
|
1164
|
-
* @remarks
|
|
1165
|
-
* This method processes the stored segments, applies the appropriate colors to each segment,
|
|
1166
|
-
* and returns the resulting highlighted code as a single string.
|
|
1167
|
-
*
|
|
1168
|
-
* @since 1.0.0
|
|
1169
|
-
*/
|
|
1170
|
-
highlight(): string;
|
|
1171
|
-
/**
|
|
1172
|
-
* Extracts a text segment from the source code using position indices.
|
|
1173
|
-
*
|
|
1174
|
-
* @param start - The starting index position in the source text
|
|
1175
|
-
* @param end - The ending index position in the source text (optional)
|
|
1176
|
-
* @returns The substring of source text between the start and end positions
|
|
1177
|
-
*
|
|
1178
|
-
* @remarks
|
|
1179
|
-
* This utility method provides access to specific portions of the source code
|
|
1180
|
-
* based on character positions. When the end parameter is omitted, the extraction
|
|
1181
|
-
* will continue to the end of the source text.
|
|
1182
|
-
*
|
|
1183
|
-
* This method is typically used during the highlighting process to access the
|
|
1184
|
-
* actual text content that corresponds to syntax nodes or other text ranges
|
|
1185
|
-
* before applying formatting.
|
|
1186
|
-
*
|
|
1187
|
-
* @example
|
|
1188
|
-
* ```ts
|
|
1189
|
-
* // Extract a variable name
|
|
1190
|
-
* const variableName = this.getSegmentSource(10, 15);
|
|
1191
|
-
*
|
|
1192
|
-
* // Extract from a position to the end of source
|
|
1193
|
-
* const remaining = this.getSegmentSource(100);
|
|
1194
|
-
* ```
|
|
1195
|
-
*
|
|
1196
|
-
* @see addSegment
|
|
1197
|
-
* @see highlight
|
|
1198
|
-
*
|
|
1199
|
-
* @since 1.0.0
|
|
1200
|
-
*/
|
|
1201
|
-
private getSegmentSource;
|
|
1202
|
-
/**
|
|
1203
|
-
* Registers a text segment for syntax highlighting with specified style information.
|
|
1204
|
-
*
|
|
1205
|
-
* @param start - The starting position of the segment in the source text
|
|
1206
|
-
* @param end - The ending position of the segment in the source text
|
|
1207
|
-
* @param color - The color code to apply to this segment
|
|
1208
|
-
* @param reset - The color reset code to apply after the segment (defaults to the standard reset code)
|
|
1209
|
-
*
|
|
1210
|
-
* @remarks
|
|
1211
|
-
* This method creates a unique key for each segment based on its position and stores the segment information in a map.
|
|
1212
|
-
* Each segment contains its position information, styling code,
|
|
1213
|
-
* and reset code which will later be used during the highlighting process.
|
|
1214
|
-
*
|
|
1215
|
-
* If multiple segments are added with the same positions, the later additions will
|
|
1216
|
-
* overwrite earlier ones due to the map's key-based storage.
|
|
1217
|
-
*
|
|
1218
|
-
* @example
|
|
1219
|
-
* ```ts
|
|
1220
|
-
* // Highlight a variable name in red
|
|
1221
|
-
* this.addSegment(10, 15, Colors.red);
|
|
1222
|
-
*
|
|
1223
|
-
* // Highlight a keyword with custom color and reset
|
|
1224
|
-
* this.addSegment(20, 26, Colors.blue, Colors.customReset);
|
|
1225
|
-
* ```
|
|
1226
|
-
*
|
|
1227
|
-
* @see Colors
|
|
1228
|
-
* @see processNode
|
|
1229
|
-
*
|
|
1230
|
-
* @since 1.0.0
|
|
1231
|
-
*/
|
|
1232
|
-
private addSegment;
|
|
1233
|
-
/**
|
|
1234
|
-
* Processes and highlights comments associated with a TypeScript AST node.
|
|
1235
|
-
*
|
|
1236
|
-
* @param node - The TypeScript AST node whose comments are to be processed
|
|
1237
|
-
*
|
|
1238
|
-
* @remarks
|
|
1239
|
-
* This method identifies both leading and trailing comments associated with the given node
|
|
1240
|
-
* and adds them to the highlighting segments.
|
|
1241
|
-
* The comments are extracted from the full source text using TypeScript's utility functions
|
|
1242
|
-
* and are highlighted using the color specified
|
|
1243
|
-
* in the schema's commentColor property.
|
|
1244
|
-
*
|
|
1245
|
-
* Leading comments appear before the node, while trailing comments appear after it.
|
|
1246
|
-
* Both types are processed with the same highlighting style.
|
|
1247
|
-
*
|
|
1248
|
-
* @example
|
|
1249
|
-
* ```ts
|
|
1250
|
-
* // For a node that might have comments like:
|
|
1251
|
-
* // This is a leading comment
|
|
1252
|
-
* const x = 5; // This is a trailing comment
|
|
1253
|
-
*
|
|
1254
|
-
* this.processComments(someNode);
|
|
1255
|
-
* // Both comments will be added to segments with the comment color
|
|
1256
|
-
* ```
|
|
1257
|
-
*
|
|
1258
|
-
* @see addSegment
|
|
1259
|
-
* @see ts.getLeadingCommentRanges
|
|
1260
|
-
* @see ts.getTrailingCommentRanges
|
|
1261
|
-
*
|
|
1262
|
-
* @since 1.0.0
|
|
1263
|
-
*/
|
|
1264
|
-
private processComments;
|
|
1265
|
-
/**
|
|
1266
|
-
* Processes TypeScript keywords and primitive type references in an AST node for syntax highlighting.
|
|
1267
|
-
*
|
|
1268
|
-
* @param node - The TypeScript AST node to be processed for keywords
|
|
1269
|
-
*
|
|
1270
|
-
* @remarks
|
|
1271
|
-
* This method handles two categories of tokens that require special highlighting:
|
|
1272
|
-
*
|
|
1273
|
-
* 1. Primitive type references: Highlights references to built-in types like `null`,
|
|
1274
|
-
* `void`, `string`, `number`, `boolean`, and `undefined` using the type color.
|
|
1275
|
-
*
|
|
1276
|
-
* 2. TypeScript keywords: Identifies any node whose kind falls within the TypeScript
|
|
1277
|
-
* keyword range (between FirstKeyword and LastKeyword) and highlights it using
|
|
1278
|
-
* the keyword color.
|
|
1279
|
-
*
|
|
1280
|
-
* Each identified token is added to the segments collection with appropriate position
|
|
1281
|
-
* and color information.
|
|
1282
|
-
*
|
|
1283
|
-
* @example
|
|
1284
|
-
* ```ts
|
|
1285
|
-
* // Inside syntax highlighting process
|
|
1286
|
-
* this.processKeywords(someNode);
|
|
1287
|
-
* // If the node represents a keyword like 'const' or a primitive type like 'string',
|
|
1288
|
-
* // it will be added to the segments with the appropriate color
|
|
1289
|
-
* ```
|
|
1290
|
-
*
|
|
1291
|
-
* @see addSegment
|
|
1292
|
-
* @see ts.SyntaxKind
|
|
1293
|
-
*
|
|
1294
|
-
* @since 1.0.0
|
|
1295
|
-
*/
|
|
1296
|
-
private processKeywords;
|
|
1297
|
-
/**
|
|
1298
|
-
* Processes identifier nodes and applies appropriate syntax highlighting based on their context.
|
|
1299
|
-
*
|
|
1300
|
-
* @param node - The TypeScript AST node representing the identifier to be processed
|
|
1301
|
-
*
|
|
1302
|
-
* @remarks
|
|
1303
|
-
* This method determines the appropriate color for an identifier by examining its parent node's kind.
|
|
1304
|
-
* Different colors are applied based on the identifier's role in the code:
|
|
1305
|
-
* - Enum members use enumColor
|
|
1306
|
-
* - Interface names use interfaceColor
|
|
1307
|
-
* - Class names use classColor
|
|
1308
|
-
* - Function and method names use functionColor
|
|
1309
|
-
* - Parameters use parameterColor
|
|
1310
|
-
* - Variables and properties use variableColor
|
|
1311
|
-
* - Types use typeColor
|
|
1312
|
-
* - And more specialized cases for other syntax kinds
|
|
1313
|
-
*
|
|
1314
|
-
* Special handling is applied to property access expressions to differentiate between
|
|
1315
|
-
* the object being accessed and the property being accessed.
|
|
1316
|
-
*
|
|
1317
|
-
* @example
|
|
1318
|
-
* ```ts
|
|
1319
|
-
* // Inside the CodeHighlighter class
|
|
1320
|
-
* const identifierNode = getIdentifierNode(); // Get some identifier node
|
|
1321
|
-
* this.processIdentifier(identifierNode);
|
|
1322
|
-
* // The identifier is now added to segments with appropriate color based on its context
|
|
1323
|
-
* ```
|
|
1324
|
-
*
|
|
1325
|
-
* @see addSegment
|
|
1326
|
-
* @see HighlightSchemeInterface
|
|
1327
|
-
*
|
|
1328
|
-
* @since 1.0.0
|
|
1329
|
-
*/
|
|
1330
|
-
private processIdentifier;
|
|
1331
|
-
/**
|
|
1332
|
-
* Processes a TypeScript template expression and adds highlighting segments for its literal parts.
|
|
1333
|
-
*
|
|
1334
|
-
* @param templateExpression - The TypeScript template expression to be processed
|
|
1335
|
-
*
|
|
1336
|
-
* @remarks
|
|
1337
|
-
* This method adds color segments for both the template head and each template span's literal part.
|
|
1338
|
-
* All template string components are highlighted using the color defined in the schema's stringColor.
|
|
1339
|
-
*
|
|
1340
|
-
* @example
|
|
1341
|
-
* ```ts
|
|
1342
|
-
* // Given a template expression like: `Hello ${name}`
|
|
1343
|
-
* this.processTemplateExpression(templateNode);
|
|
1344
|
-
* // Both "Hello " and the closing part after the expression will be highlighted
|
|
1345
|
-
* ```
|
|
1346
|
-
*
|
|
1347
|
-
* @see addSegment
|
|
1348
|
-
*
|
|
1349
|
-
* @since 1.0.0
|
|
1350
|
-
*/
|
|
1351
|
-
private processTemplateExpression;
|
|
1352
|
-
/**
|
|
1353
|
-
* Processes a TypeScript AST node and adds highlighting segments based on the node's kind.
|
|
1354
|
-
*
|
|
1355
|
-
* @param node - The TypeScript AST node to be processed
|
|
1356
|
-
*
|
|
1357
|
-
* @remarks
|
|
1358
|
-
* This method identifies the node's kind and applies the appropriate color for highlighting.
|
|
1359
|
-
* It handles various syntax kinds including literals (string, numeric, regular expressions),
|
|
1360
|
-
* template expressions, identifiers, and type references.
|
|
1361
|
-
* For complex node types like template expressions and identifiers, it delegates to specialized processing methods.
|
|
1362
|
-
*
|
|
1363
|
-
* @throws Error - When casting to TypeParameterDeclaration fails for non-compatible node kinds
|
|
1364
|
-
*
|
|
1365
|
-
* @example
|
|
1366
|
-
* ```ts
|
|
1367
|
-
* // Inside the CodeHighlighter class
|
|
1368
|
-
* const node = sourceFile.getChildAt(0);
|
|
1369
|
-
* this.processNode(node);
|
|
1370
|
-
* // Node is now added to the segments map with appropriate colors
|
|
1371
|
-
* ```
|
|
1372
|
-
*
|
|
1373
|
-
* @see processTemplateExpression
|
|
1374
|
-
* @see processIdentifier
|
|
1375
|
-
*
|
|
1376
|
-
* @since 1.0.0
|
|
1377
|
-
*/
|
|
1378
|
-
private processNode;
|
|
1379
|
-
}
|
|
1380
|
-
/**
|
|
1381
|
-
* Applies semantic highlighting to the provided code string using the specified color scheme.
|
|
1382
|
-
*
|
|
1383
|
-
* @param code - The source code to be highlighted
|
|
1384
|
-
* @param schema - An optional partial schema defining the color styles for various code elements
|
|
1385
|
-
*
|
|
1386
|
-
* @returns A string with the code elements wrapped in the appropriate color styles
|
|
1387
|
-
*
|
|
1388
|
-
* @remarks
|
|
1389
|
-
* If no schema is provided, the default schema will be used. The function creates a TypeScript
|
|
1390
|
-
* source file from the provided code and walks through its AST to apply syntax highlighting.
|
|
1391
|
-
*
|
|
1392
|
-
* @example
|
|
1393
|
-
* ```ts
|
|
1394
|
-
* const code = 'const x: number = 42;';
|
|
1395
|
-
* const schema = {
|
|
1396
|
-
* keywordColor: '\x1b[34m', // Blue
|
|
1397
|
-
* numberColor: '\x1b[31m', // Red
|
|
1398
|
-
* };
|
|
1399
|
-
* const highlightedCode = highlightCode(code, schema);
|
|
1400
|
-
* console.log(highlightedCode);
|
|
1401
|
-
* ```
|
|
1402
|
-
*
|
|
1403
|
-
* @see CodeHighlighter
|
|
1404
|
-
* @see HighlightSchemeInterface
|
|
1405
|
-
*
|
|
1406
|
-
* @since 1.0.0
|
|
1407
|
-
*/
|
|
1408
|
-
export declare function highlightCode(code: string, schema?: Partial<HighlightSchemeInterface>): string;
|
|
1409
|
-
/**
|
|
1410
|
-
* Interface representing a stack frame in a stack trace.
|
|
1411
|
-
*
|
|
1412
|
-
* This structure provides detailed information about the location
|
|
1413
|
-
* of a specific frame in the stack trace, primarily used in error debugging and stack analysis.
|
|
1414
|
-
* It optionally includes information about the line, column, file, function name, and other details
|
|
1415
|
-
* about the origin of the code.
|
|
1416
|
-
*
|
|
1417
|
-
* Properties:
|
|
1418
|
-
* - source: The source code relevant to the stack frame.
|
|
1419
|
-
* - line: An optional line number in the code associated with this frame.
|
|
1420
|
-
* - column: An optional column number in the line associated with this frame.
|
|
1421
|
-
* - fileName: The name of the file associated with the frame, if available.
|
|
1422
|
-
* - functionName: The name of the function where the stack frame is located, if available.
|
|
1423
|
-
* - eval: Indicates if the stack frame originates from an evaluated script.
|
|
1424
|
-
* - async: Indicates if the stack frame is part of an asynchronous operation.
|
|
1425
|
-
* - native: Indicates if the stack frame is part of native code execution.
|
|
1426
|
-
* - constructor: Indicates if the frame is related to an object constructor invocation.
|
|
1427
|
-
* - evalOrigin: Optional information about the origin of the code if it resulted from an eval execution,
|
|
1428
|
-
* including line number, column number, file name, and function name.
|
|
1429
|
-
*
|
|
1430
|
-
* @since 3.0.0
|
|
1431
|
-
*/
|
|
1432
|
-
export interface StackFrame {
|
|
1433
|
-
source: string;
|
|
1434
|
-
line?: number;
|
|
1435
|
-
column?: number;
|
|
1436
|
-
fileName?: string;
|
|
1437
|
-
functionName?: string;
|
|
1438
|
-
eval: boolean;
|
|
1439
|
-
async: boolean;
|
|
1440
|
-
native: boolean;
|
|
1441
|
-
constructor: boolean;
|
|
1442
|
-
evalOrigin?: {
|
|
1443
|
-
line?: number;
|
|
1444
|
-
column?: number;
|
|
1445
|
-
fileName?: string;
|
|
1446
|
-
functionName?: string;
|
|
1447
|
-
};
|
|
1448
|
-
}
|
|
1449
|
-
/**
|
|
1450
|
-
* Represents a fully parsed error stack trace with structured information
|
|
1451
|
-
*
|
|
1452
|
-
* @see StackFrame
|
|
1453
|
-
* @since 2.1.0
|
|
1454
|
-
*/
|
|
1455
|
-
export interface ParsedStackTrace {
|
|
1456
|
-
name: string;
|
|
1457
|
-
message: string;
|
|
1458
|
-
stack: StackFrame[];
|
|
1459
|
-
rawStack: string;
|
|
1460
|
-
}
|
|
1461
|
-
/**
|
|
1462
|
-
* Enumeration of JavaScript engines that can be detected from stack traces
|
|
1463
|
-
*
|
|
1464
|
-
* @since 2.1.0
|
|
1465
|
-
*/
|
|
1466
|
-
export declare const enum JSEngines {
|
|
1467
|
-
V8 = 0,
|
|
1468
|
-
SPIDERMONKEY = 1,
|
|
1469
|
-
JAVASCRIPT_CORE = 2,
|
|
1470
|
-
UNKNOWN = 3
|
|
1471
|
-
}
|
|
1472
|
-
/**
|
|
1473
|
-
* Detects the JavaScript engine based on the format of a stack trace line
|
|
1474
|
-
*
|
|
1475
|
-
* @param stack - The stack trace to analyze
|
|
1476
|
-
* @returns The identified JavaScript engine type
|
|
1477
|
-
*
|
|
1478
|
-
* @example
|
|
1479
|
-
* ```ts
|
|
1480
|
-
* const engine = detectJSEngine("at functionName (/path/to/file.js:10:15)");
|
|
1481
|
-
* if (engine === JSEngines.V8) {
|
|
1482
|
-
* // Handle V8 specific logic
|
|
1483
|
-
* }
|
|
1484
|
-
* ```
|
|
1485
|
-
*
|
|
1486
|
-
* @since 2.1.0
|
|
1487
|
-
*/
|
|
1488
|
-
export declare function detectJSEngine(stack: string): JSEngines;
|
|
1489
|
-
/**
|
|
1490
|
-
* Normalizes file paths from various formats to a standard format
|
|
1491
|
-
*
|
|
1492
|
-
* @param filePath - The file path to normalize, which may include protocol prefixes
|
|
1493
|
-
* @returns A normalized file path with consistent separators and without protocol prefixes
|
|
1494
|
-
*
|
|
1495
|
-
* @remarks
|
|
1496
|
-
* Handles both Windows and Unix-style paths, as well as file:// protocol URLs.
|
|
1497
|
-
* Converts all backslashes to forward slashes for consistency.
|
|
1498
|
-
*
|
|
1499
|
-
* @example
|
|
1500
|
-
* ```ts
|
|
1501
|
-
* // Windows file URL to a normal path
|
|
1502
|
-
* normalizePath("file:///C:/path/to/file.js"); // "C:/path/to/file.js"
|
|
1503
|
-
*
|
|
1504
|
-
* // Unix file URL to a normal path
|
|
1505
|
-
* normalizePath("file:///path/to/file.js"); // "/path/to/file.js"
|
|
1506
|
-
*
|
|
1507
|
-
* // Windows backslashes to forward slashes
|
|
1508
|
-
* normalizePath("C:\\path\\to\\file.js"); // "C:/path/to/file.js"
|
|
1509
|
-
* ```
|
|
1510
|
-
*
|
|
1511
|
-
* @since 2.1.0
|
|
1512
|
-
*/
|
|
1513
|
-
export declare function normalizePath(filePath: string): string;
|
|
1514
|
-
/**
|
|
1515
|
-
* Creates a default stack frame object with initial values
|
|
1516
|
-
*
|
|
1517
|
-
* @param source - The original source line from the stack trace
|
|
1518
|
-
* @returns A new StackFrame object with default null values
|
|
1519
|
-
*
|
|
1520
|
-
* @see ParsedStackTrace
|
|
1521
|
-
* @see StackFrame
|
|
1522
|
-
*
|
|
1523
|
-
* @since 2.1.0
|
|
1524
|
-
*/
|
|
1525
|
-
export declare function createDefaultFrame(source: string): StackFrame;
|
|
1526
|
-
/**
|
|
1527
|
-
* Safely parses a string value to an integer, handling undefined and null cases
|
|
1528
|
-
*
|
|
1529
|
-
* @param value - The string value to parse
|
|
1530
|
-
* @returns The parsed integer or null if the input is undefined/null
|
|
1531
|
-
*
|
|
1532
|
-
* @example
|
|
1533
|
-
* ```ts
|
|
1534
|
-
* safeParseInt("42"); // 42
|
|
1535
|
-
* safeParseInt(undefined); // null
|
|
1536
|
-
* safeParseInt(null); // null
|
|
1537
|
-
* ```
|
|
1538
|
-
*
|
|
1539
|
-
* @since 2.1.0
|
|
1540
|
-
*/
|
|
1541
|
-
export declare function safeParseInt(value: string | undefined | null): number | undefined;
|
|
1542
|
-
/**
|
|
1543
|
-
* Parses a V8 JavaScript engine stack trace line into a structured StackFrame object
|
|
1544
|
-
*
|
|
1545
|
-
* @param line - The stack trace line to parse
|
|
1546
|
-
* @returns A StackFrame object containing the parsed information
|
|
1547
|
-
*
|
|
1548
|
-
* @remarks
|
|
1549
|
-
* Handles both standard V8 stack frames and eval-generated stack frames which
|
|
1550
|
-
* have a more complex structure with nested origin information.
|
|
1551
|
-
*
|
|
1552
|
-
* @example
|
|
1553
|
-
* ```ts
|
|
1554
|
-
* // Standard frame
|
|
1555
|
-
* parseV8StackLine("at functionName (/path/to/file.js:10:15)");
|
|
1556
|
-
*
|
|
1557
|
-
* // Eval frame
|
|
1558
|
-
* parseV8StackLine("at eval (eval at evalFn (/source.js:5:10), <anonymous>:1:5)");
|
|
1559
|
-
* ```
|
|
1560
|
-
*
|
|
1561
|
-
* @throws Error - If the line format doesn't match any known V8 pattern
|
|
1562
|
-
*
|
|
1563
|
-
* @see StackFrame
|
|
1564
|
-
* @see createDefaultFrame
|
|
1565
|
-
*
|
|
1566
|
-
* @since 2.1.0
|
|
1567
|
-
*/
|
|
1568
|
-
export declare function parseV8StackLine(line: string): StackFrame;
|
|
1569
|
-
/**
|
|
1570
|
-
* Parses a SpiderMonkey JavaScript engine stack trace line into a structured StackFrame object
|
|
1571
|
-
*
|
|
1572
|
-
* @param line - The stack trace line to parse
|
|
1573
|
-
* @returns A StackFrame object containing the parsed information
|
|
1574
|
-
*
|
|
1575
|
-
* @remarks
|
|
1576
|
-
* Handles both standard SpiderMonkey stack frames and eval/Function-generated stack frames
|
|
1577
|
-
* which contain additional evaluation context information.
|
|
1578
|
-
*
|
|
1579
|
-
* @example
|
|
1580
|
-
* ```ts
|
|
1581
|
-
* // Standard frame
|
|
1582
|
-
* parseSpiderMonkeyStackLine("functionName@/path/to/file.js:10:15");
|
|
1583
|
-
*
|
|
1584
|
-
* // Eval frame
|
|
1585
|
-
* parseSpiderMonkeyStackLine("evalFn@/source.js line 5 > eval:1:5");
|
|
1586
|
-
* ```
|
|
1587
|
-
*
|
|
1588
|
-
* @see StackFrame
|
|
1589
|
-
* @see createDefaultFrame
|
|
1590
|
-
*
|
|
1591
|
-
* @since 2.1.0
|
|
1592
|
-
*/
|
|
1593
|
-
export declare function parseSpiderMonkeyStackLine(line: string): StackFrame;
|
|
1594
|
-
/**
|
|
1595
|
-
* Parses a JavaScriptCore engine stack trace line into a structured StackFrame object
|
|
1596
|
-
*
|
|
1597
|
-
* @param line - The stack trace line to parse
|
|
1598
|
-
* @returns A StackFrame object containing the parsed information
|
|
1599
|
-
*
|
|
1600
|
-
* @remarks
|
|
1601
|
-
* Handles both standard JavaScriptCore stack frames and eval-generated stack frames.
|
|
1602
|
-
* Special handling is provided for "global code" references and native code.
|
|
1603
|
-
*
|
|
1604
|
-
* @example
|
|
1605
|
-
* ```ts
|
|
1606
|
-
* // Standard frame
|
|
1607
|
-
* parseJavaScriptCoreStackLine("functionName@/path/to/file.js:10:15");
|
|
1608
|
-
*
|
|
1609
|
-
* // Eval frame
|
|
1610
|
-
* parseJavaScriptCoreStackLine("eval code@");
|
|
1611
|
-
* ```
|
|
1612
|
-
*
|
|
1613
|
-
* @see StackFrame
|
|
1614
|
-
* @see createDefaultFrame
|
|
1615
|
-
*
|
|
1616
|
-
* @since 2.1.0
|
|
1617
|
-
*/
|
|
1618
|
-
export declare function parseJavaScriptCoreStackLine(line: string): StackFrame;
|
|
1619
|
-
/**
|
|
1620
|
-
* Parses a stack trace line based on the detected JavaScript engine
|
|
1621
|
-
*
|
|
1622
|
-
* @param line - The stack trace line to parse
|
|
1623
|
-
* @param engine - The JavaScript engine type that generated the stack trace
|
|
1624
|
-
* @returns A StackFrame object containing the parsed information
|
|
1625
|
-
*
|
|
1626
|
-
* @remarks
|
|
1627
|
-
* Delegates to the appropriate parsing function based on the JavaScript engine.
|
|
1628
|
-
* Defaults to V8 parsing if the engine is unknown.
|
|
1629
|
-
*
|
|
1630
|
-
* @example
|
|
1631
|
-
* ```ts
|
|
1632
|
-
* const engine = detectJSEngine(stackLine);
|
|
1633
|
-
* const frame = parseStackLine(stackLine, engine);
|
|
1634
|
-
* ```
|
|
1635
|
-
*
|
|
1636
|
-
* @see JSEngines
|
|
1637
|
-
* @see parseV8StackLine
|
|
1638
|
-
* @see parseSpiderMonkeyStackLine
|
|
1639
|
-
* @see parseJavaScriptCoreStackLine
|
|
1640
|
-
*
|
|
1641
|
-
* @since 2.1.0
|
|
1642
|
-
*/
|
|
1643
|
-
export declare function parseStackLine(line: string, engine: JSEngines): StackFrame;
|
|
1644
|
-
/**
|
|
1645
|
-
* Parses a complete error stack trace into a structured format
|
|
1646
|
-
*
|
|
1647
|
-
* @param error - Error object or error message string to parse
|
|
1648
|
-
* @returns A ParsedStackTrace object containing structured stack trace information
|
|
1649
|
-
*
|
|
1650
|
-
* @remarks
|
|
1651
|
-
* Automatically detects the JavaScript engine from the stack format.
|
|
1652
|
-
* Filters out redundant information like the error name/message line.
|
|
1653
|
-
* Handles both Error objects and string error messages.
|
|
1654
|
-
*
|
|
1655
|
-
* @example
|
|
1656
|
-
* ```ts
|
|
1657
|
-
* try {
|
|
1658
|
-
* throw new Error ("Something went wrong");
|
|
1659
|
-
* } catch (error) {
|
|
1660
|
-
* const parsedStack = parseErrorStack(error);
|
|
1661
|
-
* console.log(parsedStack.name); // "Error"
|
|
1662
|
-
* console.log(parsedStack.message); // "Something went wrong"
|
|
1663
|
-
* console.log(parsedStack.stack); // Array of StackFrame objects
|
|
1664
|
-
* }
|
|
1665
|
-
* ```
|
|
1666
|
-
*
|
|
1667
|
-
* @see ParsedStackTrace
|
|
1668
|
-
* @see StackFrame
|
|
1669
|
-
* @see parseStackLine
|
|
1670
|
-
* @see detectJSEngine
|
|
1671
|
-
*
|
|
1672
|
-
* @since 2.1.0
|
|
1673
|
-
*/
|
|
1674
|
-
export declare function parseErrorStack(error: Error | string): ParsedStackTrace;
|
|
418
|
+
color: ColorFunctionType;
|
|
419
|
+
}
|