@remotex-labs/xmap 3.0.2 → 3.0.4
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/dist/cjs/formatter.component.js.map +1 -1
- package/dist/cjs/highlighter.component.js.map +1 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/parser.component.js +2 -2
- package/dist/cjs/parser.component.js.map +4 -4
- package/dist/esm/formatter.component.js.map +1 -1
- package/dist/esm/highlighter.component.js.map +1 -1
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/parser.component.js +2 -2
- package/dist/esm/parser.component.js.map +4 -4
- package/dist/formatter.component.d.ts +1511 -95
- package/dist/highlighter.component.d.ts +1255 -4
- package/dist/index.d.ts +814 -6
- package/dist/parser.component.d.ts +1418 -10
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type * as ts from "typescript";
|
|
1
2
|
/**
|
|
2
3
|
* Represents a source map structure used for mapping code within a file to its original source
|
|
3
4
|
* @since 1.0.0
|
|
@@ -162,7 +163,7 @@ export interface SourceOptionsInterface {
|
|
|
162
163
|
*
|
|
163
164
|
* @since 1.0.0
|
|
164
165
|
*/
|
|
165
|
-
export function encodeVLQ(value: number): string;
|
|
166
|
+
export declare function encodeVLQ(value: number): string;
|
|
166
167
|
/**
|
|
167
168
|
* Encodes an array of numbers using VLQ encoding by individually encoding each number and concatenating the results.
|
|
168
169
|
*
|
|
@@ -179,7 +180,7 @@ export function encodeVLQ(value: number): string;
|
|
|
179
180
|
*
|
|
180
181
|
* @since 1.0.0
|
|
181
182
|
*/
|
|
182
|
-
export function encodeArrayVLQ(values: number[]): string;
|
|
183
|
+
export declare function encodeArrayVLQ(values: number[]): string;
|
|
183
184
|
/**
|
|
184
185
|
* Decodes a VLQ encoded string back into an array of numbers by processing Base64 characters and continuation bits.
|
|
185
186
|
*
|
|
@@ -206,7 +207,7 @@ export function encodeArrayVLQ(values: number[]): string;
|
|
|
206
207
|
*
|
|
207
208
|
* @since 1.0.0
|
|
208
209
|
*/
|
|
209
|
-
export function decodeVLQ(data: string): number[];
|
|
210
|
+
export declare function decodeVLQ(data: string): number[];
|
|
210
211
|
/**
|
|
211
212
|
* Represents a source map mapping segment that links positions between original and generated code.
|
|
212
213
|
*
|
|
@@ -296,7 +297,7 @@ export interface SegmentOffsetInterface extends SegmentInterface {
|
|
|
296
297
|
*
|
|
297
298
|
* @since 1.0.0
|
|
298
299
|
*/
|
|
299
|
-
export const enum Bias {
|
|
300
|
+
export declare const enum Bias {
|
|
300
301
|
BOUND = 0,
|
|
301
302
|
LOWER_BOUND = 1,
|
|
302
303
|
UPPER_BOUND = 2
|
|
@@ -373,7 +374,7 @@ export type MapType = Array<null | FrameType>;
|
|
|
373
374
|
*
|
|
374
375
|
* @since 1.0.0
|
|
375
376
|
*/
|
|
376
|
-
export class MappingProvider {
|
|
377
|
+
export declare class MappingProvider {
|
|
377
378
|
private mapping;
|
|
378
379
|
/**
|
|
379
380
|
* Creates a new MappingProvider instance.
|
|
@@ -674,7 +675,7 @@ export class MappingProvider {
|
|
|
674
675
|
*
|
|
675
676
|
* @since 1.0.0
|
|
676
677
|
*/
|
|
677
|
-
export class SourceService {
|
|
678
|
+
export declare class SourceService {
|
|
678
679
|
/**
|
|
679
680
|
* The name of the generated file this source map applies to.
|
|
680
681
|
*
|
|
@@ -864,3 +865,810 @@ export class SourceService {
|
|
|
864
865
|
*/
|
|
865
866
|
private validateSourceMap;
|
|
866
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;
|