@remotex-labs/xmap 2.0.3 → 2.1.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.
@@ -9,18 +9,18 @@ import * as ts from 'typescript';
9
9
  /**
10
10
  * An enum containing ANSI escape sequences for various colors.
11
11
  *
12
+ * @remarks
12
13
  * This enum is primarily intended for terminal output and won't work directly in JavaScript for web development.
13
- * It defines color codes for various colors and a reset code to return to
14
- * the default text color.
14
+ * It defines color codes for various colors and a reset code to return to the default text color.
15
15
  *
16
16
  * @example
17
- * ```typescript
18
- * console.log(`${Colors.red}This the text will be red in the terminal.${Colors.reset}`);
17
+ * ```ts
18
+ * console.log(`${ Colors.red }This text will be red in the terminal.${ Colors.reset }`);
19
19
  * ```
20
20
  *
21
- * This functionality is limited to terminal environments.
22
- * Consider alternative methods
23
- * for color highlighting in web development contexts, such as CSS classes.
21
+ * @see Colors.reset
22
+ *
23
+ * @since 1.0.0
24
24
  */
25
25
  export declare const enum Colors {
26
26
  reset = "\u001B[0m",
@@ -40,147 +40,303 @@ export declare const enum Colors {
40
40
  /**
41
41
  * Class responsible for applying semantic highlighting to a source code string based on a given color scheme.
42
42
  *
43
- * @class
43
+ * @remarks
44
+ * Processes TypeScript AST nodes and applies color formatting to different code elements
45
+ * according to the provided color scheme.
44
46
  *
45
- * @param sourceFile - The TypeScript AST node representing the source file.
46
- * @param code - The source code string to be highlighted.
47
- * @param schema - The color scheme used for highlighting different elements in the code.
47
+ * @example
48
+ * ```ts
49
+ * const sourceFile = ts.createSourceFile('example.ts', code, ts.ScriptTarget.Latest);
50
+ * const highlighter = new CodeHighlighter(sourceFile, code, customScheme);
51
+ * highlighter.parseNode(sourceFile);
52
+ * const highlightedCode = highlighter.highlight();
53
+ * console.log(highlightedCode);
54
+ * ```
48
55
  *
49
- * const highlighter = new CodeHighlighter(sourceFile, code, schema);
56
+ * @see HighlightSchemeInterface
57
+ * @see highlightCode
58
+ *
59
+ * @since 1.0.0
50
60
  */
51
61
  export declare class CodeHighlighter {
52
62
  private sourceFile;
53
63
  private code;
54
64
  private schema;
55
65
  /**
56
- * A Map of segments where the key is a combination of start and end positions,
57
- * and the value is an object containing the color and reset code.
66
+ * A Map of segments where the key is a combination of start and end positions.
67
+ *
68
+ * @remarks
58
69
  * This structure ensures unique segments and allows for fast lookups and updates.
59
70
  *
60
- * @example
61
- * this.segments = new Map([
62
- * ['0-10', { start: 1, end: 11, color: '\x1b[31m', reset: '\x1b[0m' }],
63
- * ['11-20', { start: 12, end: 20, color: '\x1b[32m', reset: '\x1b[0m' }]
64
- * ]);
71
+ * @see HighlightNodeSegmentInterface
72
+ * @since 1.0.0
65
73
  */
66
74
  private segments;
67
75
  /**
68
76
  * Creates an instance of the CodeHighlighter class.
69
77
  *
70
- * @param sourceFile - The TypeScript AST node representing the source file.
71
- * @param code - The source code string to be highlighted.
72
- * @param schema - The color scheme used for highlighting different elements in the code.
78
+ * @param sourceFile - The TypeScript AST node representing the source file
79
+ * @param code - The source code string to be highlighted
80
+ * @param schema - The color scheme used for highlighting different elements in the code
81
+ *
82
+ * @since 1.0.0
73
83
  */
74
84
  constructor(sourceFile: ts.Node, code: string, schema: HighlightSchemeInterface);
75
85
  /**
76
86
  * Parses a TypeScript AST node and processes its comments to identify segments that need highlighting.
77
87
  *
78
- * @param node - The TypeScript AST node to be parsed.
88
+ * @param node - The TypeScript AST node to be parsed
89
+ *
90
+ * @since 1.0.0
79
91
  */
80
92
  parseNode(node: ts.Node): void;
81
93
  /**
82
94
  * Generates a string with highlighted code segments based on the provided color scheme.
83
95
  *
96
+ * @returns The highlighted code as a string, with ANSI color codes applied to the segments
97
+ *
98
+ * @remarks
84
99
  * This method processes the stored segments, applies the appropriate colors to each segment,
85
100
  * and returns the resulting highlighted code as a single string.
86
101
  *
87
- * @returns The highlighted code as a string, with ANSI color codes applied to the segments.
102
+ * @since 1.0.0
88
103
  */
89
104
  highlight(): string;
90
105
  /**
91
- * Extracts a substring from the code based on the specified start and end positions.
106
+ * Extracts a text segment from the source code using position indices.
107
+ *
108
+ * @param start - The starting index position in the source text
109
+ * @param end - The ending index position in the source text (optional)
110
+ * @returns The substring of source text between the start and end positions
92
111
  *
93
- * This method is used to retrieve the source code segment that corresponds to the
94
- * given start and end positions. It is primarily used for highlighting specific
95
- * segments of the code.
112
+ * @remarks
113
+ * This utility method provides access to specific portions of the source code
114
+ * based on character positions. When the end parameter is omitted, the extraction
115
+ * will continue to the end of the source text.
116
+ *
117
+ * This method is typically used during the highlighting process to access the
118
+ * actual text content that corresponds to syntax nodes or other text ranges
119
+ * before applying formatting.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * // Extract a variable name
124
+ * const variableName = this.getSegmentSource(10, 15);
96
125
  *
97
- * @param start - The starting index of the segment to be extracted.
98
- * @param end - The ending index of the segment to be extracted.
99
- * @returns The extracted substring from the code.
126
+ * // Extract from a position to the end of source
127
+ * const remaining = this.getSegmentSource(100);
128
+ * ```
129
+ *
130
+ * @see addSegment
131
+ * @see highlight
132
+ *
133
+ * @since 1.0.0
100
134
  */
101
135
  private getSegmentSource;
102
136
  /**
103
- * Adds a new segment to the list of segments to be highlighted.
104
- * The segment is defined by its start and end positions, the color to apply, and an optional reset code.
137
+ * Registers a text segment for syntax highlighting with specified style information.
138
+ *
139
+ * @param start - The starting position of the segment in the source text
140
+ * @param end - The ending position of the segment in the source text
141
+ * @param color - The color code to apply to this segment
142
+ * @param reset - The color reset code to apply after the segment (defaults to the standard reset code)
143
+ *
144
+ * @remarks
145
+ * This method creates a unique key for each segment based on its position and stores the segment information in a map.
146
+ * Each segment contains its position information, styling code,
147
+ * and reset code which will later be used during the highlighting process.
148
+ *
149
+ * If multiple segments are added with the same positions, the later additions will
150
+ * overwrite earlier ones due to the map's key-based storage.
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * // Highlight a variable name in red
155
+ * this.addSegment(10, 15, Colors.red);
105
156
  *
106
- * @param start - The starting index of the segment in the code string.
107
- * @param end - The ending index of the segment in the code string.
108
- * @param color - The color code to apply to the segment.
109
- * @param reset - The color reset code to apply after the segment, Defaults to the reset code defined in `Colors.reset`.
157
+ * // Highlight a keyword with custom color and reset
158
+ * this.addSegment(20, 26, Colors.blue, Colors.customReset);
159
+ * ```
160
+ *
161
+ * @see Colors
162
+ * @see processNode
163
+ *
164
+ * @since 1.0.0
110
165
  */
111
166
  private addSegment;
112
167
  /**
113
- * Processes comments within a TypeScript AST node and adds segments for highlighting.
114
- * Extracts trailing and leading comments from the node and adds them as segments using the color defined in `this.colorSchema.comments`.
168
+ * Processes and highlights comments associated with a TypeScript AST node.
169
+ *
170
+ * @param node - The TypeScript AST node whose comments are to be processed
171
+ *
172
+ * @remarks
173
+ * This method identifies both leading and trailing comments associated with the given node
174
+ * and adds them to the highlighting segments.
175
+ * The comments are extracted from the full source text using TypeScript's utility functions
176
+ * and are highlighted using the color specified
177
+ * in the schema's commentColor property.
178
+ *
179
+ * Leading comments appear before the node, while trailing comments appear after it.
180
+ * Both types are processed with the same highlighting style.
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * // For a node that might have comments like:
185
+ * // This is a leading comment
186
+ * const x = 5; // This is a trailing comment
187
+ *
188
+ * this.processComments(someNode);
189
+ * // Both comments will be added to segments with the comment color
190
+ * ```
115
191
  *
116
- * @param node - The TypeScript AST node whose comments are to be processed.
192
+ * @see addSegment
193
+ * @see ts.getLeadingCommentRanges
194
+ * @see ts.getTrailingCommentRanges
195
+ *
196
+ * @since 1.0.0
117
197
  */
118
198
  private processComments;
119
199
  /**
120
- * Processes the keywords within a TypeScript AST node and adds them as segments for highlighting.
200
+ * Processes TypeScript keywords and primitive type references in an AST node for syntax highlighting.
201
+ *
202
+ * @param node - The TypeScript AST node to be processed for keywords
203
+ *
204
+ * @remarks
205
+ * This method handles two categories of tokens that require special highlighting:
206
+ *
207
+ * 1. Primitive type references: Highlights references to built-in types like `null`,
208
+ * `void`, `string`, `number`, `boolean`, and `undefined` using the type color.
209
+ *
210
+ * 2. TypeScript keywords: Identifies any node whose kind falls within the TypeScript
211
+ * keyword range (between FirstKeyword and LastKeyword) and highlights it using
212
+ * the keyword color.
121
213
  *
122
- * This method identifies potential keyword tokens within the provided node and adds them to the
123
- * list of segments with the color defined in `this.schema.keywordColor`.
124
- * The method considers the current node, its first token, and its last token to determine if they should be highlighted
125
- * as keywords.
214
+ * Each identified token is added to the segments collection with appropriate position
215
+ * and color information.
126
216
  *
127
- * The method checks if the node's kind falls within the range of keyword kinds defined by TypeScript.
128
- * If the node or any of its tokens are identified as keywords, a segment is added to `this.segments`
129
- * with the start and end positions of the node and the specified color for keywords.
217
+ * @example
218
+ * ```ts
219
+ * // Inside syntax highlighting process
220
+ * this.processKeywords(someNode);
221
+ * // If the node represents a keyword like 'const' or a primitive type like 'string',
222
+ * // it will be added to the segments with the appropriate color
223
+ * ```
224
+ *
225
+ * @see addSegment
226
+ * @see ts.SyntaxKind
130
227
  *
131
- * @param node - The TypeScript AST node to be processed for keywords.
228
+ * @since 1.0.0
132
229
  */
133
230
  private processKeywords;
134
231
  /**
135
- * Processes identifiers within a TypeScript AST node and adds them as segments for highlighting
136
- * based on the node's parent type.
232
+ * Processes identifier nodes and applies appropriate syntax highlighting based on their context.
233
+ *
234
+ * @param node - The TypeScript AST node representing the identifier to be processed
235
+ *
236
+ * @remarks
237
+ * This method determines the appropriate color for an identifier by examining its parent node's kind.
238
+ * Different colors are applied based on the identifier's role in the code:
239
+ * - Enum members use enumColor
240
+ * - Interface names use interfaceColor
241
+ * - Class names use classColor
242
+ * - Function and method names use functionColor
243
+ * - Parameters use parameterColor
244
+ * - Variables and properties use variableColor
245
+ * - Types use typeColor
246
+ * - And more specialized cases for other syntax kinds
247
+ *
248
+ * Special handling is applied to property access expressions to differentiate between
249
+ * the object being accessed and the property being accessed.
250
+ *
251
+ * @example
252
+ * ```ts
253
+ * // Inside the CodeHighlighter class
254
+ * const identifierNode = getIdentifierNode(); // Get some identifier node
255
+ * this.processIdentifier(identifierNode);
256
+ * // The identifier is now added to segments with appropriate color based on its context
257
+ * ```
137
258
  *
138
- * This method determines the appropriate color for an identifier based on its parent node's kind.
139
- * If the parent node matches one of the specified kinds, the identifier is highlighted with a cyan color.
140
- * Supported parent kinds include various declarations, expressions, and signatures.
259
+ * @see addSegment
260
+ * @see HighlightSchemeInterface
141
261
  *
142
- * @param node - The TypeScript AST node representing the identifier to be processed.
262
+ * @since 1.0.0
143
263
  */
144
264
  private processIdentifier;
145
265
  /**
146
- * Processes a TypeScript template expression and adds segments for highlighting its literal parts.
266
+ * Processes a TypeScript template expression and adds highlighting segments for its literal parts.
147
267
  *
148
- * This method adds a segment for the head of the template expression with the color specified in `this.schema.stringColor`.
149
- * It also processes each template span within the expression, adding
150
- * segments for each span's literal part.
268
+ * @param templateExpression - The TypeScript template expression to be processed
151
269
  *
152
- * @param templateExpression - The TypeScript template expression to be processed.
270
+ * @remarks
271
+ * This method adds color segments for both the template head and each template span's literal part.
272
+ * All template string components are highlighted using the color defined in the schema's stringColor.
273
+ *
274
+ * @example
275
+ * ```ts
276
+ * // Given a template expression like: `Hello ${name}`
277
+ * this.processTemplateExpression(templateNode);
278
+ * // Both "Hello " and the closing part after the expression will be highlighted
279
+ * ```
280
+ *
281
+ * @see addSegment
282
+ *
283
+ * @since 1.0.0
153
284
  */
154
285
  private processTemplateExpression;
155
286
  /**
156
- * Processes a TypeScript AST node and adds segments for highlighting based on the node's kind.
287
+ * Processes a TypeScript AST node and adds highlighting segments based on the node's kind.
157
288
  *
158
- * This method identifies the kind of the node and determines the appropriate color for highlighting.
159
- * It handles various node kinds including string literals, regular expressions, template expressions, and identifiers.
160
- * Specific methods are invoked for more complex node kinds, such as template expressions and identifiers.
289
+ * @param node - The TypeScript AST node to be processed
161
290
  *
162
- * @param node - The TypeScript AST node to be processed.
291
+ * @remarks
292
+ * This method identifies the node's kind and applies the appropriate color for highlighting.
293
+ * It handles various syntax kinds including literals (string, numeric, regular expressions),
294
+ * template expressions, identifiers, and type references.
295
+ * For complex node types like template expressions and identifiers, it delegates to specialized processing methods.
296
+ *
297
+ * @throws Error - When casting to TypeParameterDeclaration fails for non-compatible node kinds
298
+ *
299
+ * @example
300
+ * ```ts
301
+ * // Inside the CodeHighlighter class
302
+ * const node = sourceFile.getChildAt(0);
303
+ * this.processNode(node);
304
+ * // Node is now added to the segments map with appropriate colors
305
+ * ```
306
+ *
307
+ * @see processTemplateExpression
308
+ * @see processIdentifier
309
+ *
310
+ * @since 1.0.0
163
311
  */
164
312
  private processNode;
165
313
  }
166
314
  /**
167
315
  * Applies semantic highlighting to the provided code string using the specified color scheme.
168
316
  *
169
- * @param code - The source code to be highlighted.
170
- * @param schema - An optional partial schema defining the color styles for various code elements.
171
- * Defaults to an empty object, which means no specific highlighting will be applied.
317
+ * @param code - The source code to be highlighted
318
+ * @param schema - An optional partial schema defining the color styles for various code elements
319
+ *
320
+ * @returns A string with the code elements wrapped in the appropriate color styles
172
321
  *
173
- * @returns A string with the code elements wrapped in the appropriate color styles as specified by the schema.
322
+ * @remarks
323
+ * If no schema is provided, the default schema will be used. The function creates a TypeScript
324
+ * source file from the provided code and walks through its AST to apply syntax highlighting.
174
325
  *
175
326
  * @example
327
+ * ```ts
176
328
  * const code = 'const x: number = 42;';
177
329
  * const schema = {
178
330
  * keywordColor: '\x1b[34m', // Blue
179
- * stringColor: '\x1b[32m', // Green
180
331
  * numberColor: '\x1b[31m', // Red
181
- * reset: '\x1b[0m' // Reset
182
332
  * };
183
333
  * const highlightedCode = highlightCode(code, schema);
184
334
  * console.log(highlightedCode);
335
+ * ```
336
+ *
337
+ * @see CodeHighlighter
338
+ * @see HighlightSchemeInterface
339
+ *
340
+ * @since 1.0.0
185
341
  */
186
342
  export declare function highlightCode(code: string, schema?: Partial<HighlightSchemeInterface>): string;
@@ -1,7 +1,37 @@
1
1
  /**
2
- * HighlightSchemeInterface defines the structure for a color style schema
3
- * used in semantic highlighting.
4
- * This interface ensures that the highlighting styles are consistent and easily configurable.
2
+ * Defines the color scheme for syntax highlighting different code elements.
3
+ *
4
+ * @interface HighlightSchemeInterface
5
+ *
6
+ * @property enumColor - Color code for enum declarations and references
7
+ * @property typeColor - Color code for type annotations and primitive types
8
+ * @property classColor - Color code for class declarations and references
9
+ * @property stringColor - Color code for string literals and template strings
10
+ * @property keywordColor - Color code for language keywords
11
+ * @property commentColor - Color code for comments (single-line and multi-line)
12
+ * @property functionColor - Color code for function declarations and calls
13
+ * @property variableColor - Color code for variable declarations and references
14
+ * @property interfaceColor - Color code for interface declarations and references
15
+ * @property parameterColor - Color code for function and method parameters
16
+ * @property getAccessorColor - Color code for getter accessor methods
17
+ * @property numericLiteralColor - Color code for numeric literals
18
+ * @property methodSignatureColor - Color code for method signatures in interfaces
19
+ * @property regularExpressionColor - Color code for regular expression literals
20
+ * @property propertyAssignmentColor - Color code for property assignments in object literals
21
+ * @property propertyAccessExpressionColor - Color code for property access expressions
22
+ * @property expressionWithTypeArgumentsColor - Color code for type arguments in expressions
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const darkTheme: HighlightSchemeInterface = {
27
+ * enumColor: Colors.cyan,
28
+ * typeColor: Colors.blue,
29
+ * classColor: Colors.yellow,
30
+ * // ...other color definitions
31
+ * };
32
+ * ```
33
+ *
34
+ * @since 1.0.0
5
35
  */
6
36
  export interface HighlightSchemeInterface {
7
37
  enumColor: string;
@@ -23,22 +53,38 @@ export interface HighlightSchemeInterface {
23
53
  expressionWithTypeArgumentsColor: string;
24
54
  }
25
55
  /**
26
- * Represents a segment of a code string that needs to be highlighted.
56
+ * Represents a segment of source code to be highlighted with specific styling.
57
+ *
58
+ * @interface HighlightNodeSegmentInterface
59
+ *
60
+ * @property start - The starting character position of the segment in the source text
61
+ * @property end - The ending character position of the segment in the source text
62
+ * @property color - The color or style code to apply to this segment
63
+ * @property reset - The reset code that returns formatting to normal after this segment
27
64
  *
28
- * @interface
65
+ * @remarks
66
+ * Segments are the fundamental units of the highlighting system.
67
+ * Each segment represents a portion of text that should receive specific styling.
68
+ * When the source code is processed for display,
69
+ * these segments are used to insert the appropriate color/style codes at the correct positions.
29
70
  *
30
- * @property start - The starting index of the segment in the code string.
31
- * @property end - The ending index of the segment in the code string.
32
- * @property color - The color code to apply to the segment.
33
- * @property reset - The color reset code to apply after the segment.
71
+ * The highlighter maintains a collection of these segments and applies them
72
+ * in position order to create the complete highlighted output.
34
73
  *
35
74
  * @example
36
- * const segment: HighlightNodeSegment = {
75
+ * ```ts
76
+ * const keywordSegment: HighlightNodeSegmentInterface = {
37
77
  * start: 0,
38
- * end: 10,
39
- * color: '\x1b[31m', // Red
40
- * reset: '\x1b[0m' // Reset
78
+ * end: 6,
79
+ * color: '\x1b[34m', // Blue for the keyword "import"
80
+ * reset: '\x1b[0m' // Reset to default
41
81
  * };
82
+ * ```
83
+ *
84
+ * @see HighlightSchemeInterface
85
+ * @see addSegment
86
+ *
87
+ * @since 1.0.0
42
88
  */
43
89
  export interface HighlightNodeSegmentInterface {
44
90
  end: number;
@@ -1,31 +1,32 @@
1
1
  /**
2
- * Represents an entry in the stack trace.
2
+ * Represents a parsed stack frame from an error stack trace
3
+ *
4
+ * @see ParsedStackTrace
5
+ * @since 2.1.0
3
6
  */
4
- export interface StackInterface {
5
- /**
6
- * The function or method where the error occurred.
7
- */
8
- at: string;
9
- /**
10
- * The file path where the error occurred.
11
- */
12
- file: string;
13
- /**
14
- * The line number where the error occurred.
15
- */
16
- line: number;
17
- /**
18
- * The column number where the error occurred.
19
- */
20
- column: number;
7
+ export interface StackFrame {
8
+ source: string;
9
+ fileName: string | null;
10
+ lineNumber: number | null;
11
+ columnNumber: number | null;
12
+ functionName: string | null;
13
+ isEval: boolean;
14
+ evalOrigin?: {
15
+ fileName: string | null;
16
+ lineNumber: number | null;
17
+ columnNumber: number | null;
18
+ functionName: string | null;
19
+ };
21
20
  }
22
21
  /**
23
- * Represents a detailed entry in the stack trace, which may include an executor stack entry.
22
+ * Represents a fully parsed error stack trace with structured information
23
+ *
24
+ * @see StackFrame
25
+ * @since 2.1.0
24
26
  */
25
- export interface StackEntryInterface extends StackInterface {
26
- /**
27
- * The executor information if the error occurred within an eval function.
28
- * This will be `null` if the error did not occur within an eval function.
29
- */
30
- executor?: StackInterface | null;
27
+ export interface ParsedStackTrace {
28
+ name: string;
29
+ message: string;
30
+ stack: StackFrame[];
31
+ rawStack: string;
31
32
  }