juxscript 1.1.46 → 1.1.48
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.
|
@@ -23,6 +23,15 @@ export declare class CodeParser {
|
|
|
23
23
|
*/
|
|
24
24
|
static parse(code: string, language?: string): ParsedLine[];
|
|
25
25
|
private static _parseCode;
|
|
26
|
+
/**
|
|
27
|
+
* ✅ NEW: Strip TypeScript type annotations for parsing
|
|
28
|
+
* This is a simple regex-based approach - not perfect but works for most cases
|
|
29
|
+
*/
|
|
30
|
+
private static _stripTypeScriptTypes;
|
|
31
|
+
/**
|
|
32
|
+
* Clear cache (useful for development)
|
|
33
|
+
*/
|
|
34
|
+
static clearCache(): void;
|
|
26
35
|
}
|
|
27
36
|
/**
|
|
28
37
|
* Get CSS class for a token
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codeparser.d.ts","sourceRoot":"","sources":["codeparser.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAqClD,CAAC;AAEF,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,UAAU;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,WAAW,EAAE,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;CACf;AAED,qBAAa,UAAU;IACnB,OAAO,CAAC,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"codeparser.d.ts","sourceRoot":"","sources":["codeparser.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,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
|
42
42
|
*/
|
|
43
43
|
static parse(code, language = 'javascript') {
|
|
44
|
+
// ✅ Guard against undefined cache
|
|
45
|
+
if (!this._cache) {
|
|
46
|
+
this._cache = new Map();
|
|
47
|
+
}
|
|
44
48
|
const cacheKey = `${language}:${code}`;
|
|
45
49
|
if (this._cache.has(cacheKey)) {
|
|
46
50
|
return this._cache.get(cacheKey);
|
|
@@ -52,10 +56,25 @@ export class CodeParser {
|
|
|
52
56
|
static _parseCode(code, language) {
|
|
53
57
|
const lines = [];
|
|
54
58
|
const codeLines = code.split('\n');
|
|
59
|
+
// ✅ Skip parsing for non-JS languages
|
|
60
|
+
if (!['javascript', 'typescript', 'js', 'ts', 'jsx', 'tsx'].includes(language.toLowerCase())) {
|
|
61
|
+
codeLines.forEach((lineText, index) => {
|
|
62
|
+
lines.push({
|
|
63
|
+
lineNumber: index + 1,
|
|
64
|
+
tokens: [],
|
|
65
|
+
raw: lineText
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
return lines;
|
|
69
|
+
}
|
|
70
|
+
// ✅ Strip TypeScript types for parsing
|
|
71
|
+
const cleanedCode = language.toLowerCase().includes('typescript') || language.toLowerCase().includes('ts')
|
|
72
|
+
? this._stripTypeScriptTypes(code)
|
|
73
|
+
: code;
|
|
55
74
|
try {
|
|
56
75
|
// Parse with Acorn (supports ES2020+)
|
|
57
76
|
const tokens = [];
|
|
58
|
-
acorn.parse(
|
|
77
|
+
acorn.parse(cleanedCode, {
|
|
59
78
|
ecmaVersion: 2022,
|
|
60
79
|
sourceType: 'module',
|
|
61
80
|
locations: true,
|
|
@@ -79,7 +98,7 @@ export class CodeParser {
|
|
|
79
98
|
const line = token.loc.start.line;
|
|
80
99
|
const parsedToken = {
|
|
81
100
|
type: token.type.label || token.type,
|
|
82
|
-
value: token.value !== undefined ? String(token.value) :
|
|
101
|
+
value: token.value !== undefined ? String(token.value) : cleanedCode.substring(token.start, token.end),
|
|
83
102
|
start: token.start,
|
|
84
103
|
end: token.end,
|
|
85
104
|
line: token.loc.start.line,
|
|
@@ -91,14 +110,14 @@ export class CodeParser {
|
|
|
91
110
|
}
|
|
92
111
|
tokensByLine.get(line).push(parsedToken);
|
|
93
112
|
});
|
|
94
|
-
// Build lines with tokens
|
|
113
|
+
// ✅ Build lines with tokens (use ORIGINAL code, not cleaned)
|
|
95
114
|
codeLines.forEach((lineText, index) => {
|
|
96
115
|
const lineNumber = index + 1;
|
|
97
116
|
const lineTokens = tokensByLine.get(lineNumber) || [];
|
|
98
117
|
lines.push({
|
|
99
118
|
lineNumber,
|
|
100
119
|
tokens: lineTokens,
|
|
101
|
-
raw: lineText
|
|
120
|
+
raw: lineText // Use original line with types
|
|
102
121
|
});
|
|
103
122
|
});
|
|
104
123
|
}
|
|
@@ -115,8 +134,33 @@ export class CodeParser {
|
|
|
115
134
|
}
|
|
116
135
|
return lines;
|
|
117
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* ✅ NEW: Strip TypeScript type annotations for parsing
|
|
139
|
+
* This is a simple regex-based approach - not perfect but works for most cases
|
|
140
|
+
*/
|
|
141
|
+
static _stripTypeScriptTypes(code) {
|
|
142
|
+
return code
|
|
143
|
+
// Remove type annotations from parameters: (name: string) -> (name)
|
|
144
|
+
.replace(/(\w+)\s*:\s*[\w<>\[\]|&]+/g, '$1')
|
|
145
|
+
// Remove return type annotations: ): string -> )
|
|
146
|
+
.replace(/\)\s*:\s*[\w<>\[\]|&]+/g, ')')
|
|
147
|
+
// Remove interface/type declarations (keep as comments to preserve line numbers)
|
|
148
|
+
.replace(/^(\s*)(interface|type)\s+\w+.*$/gm, '$1// $2 declaration')
|
|
149
|
+
// Remove as type assertions: value as string -> value
|
|
150
|
+
.replace(/\s+as\s+[\w<>\[\]|&]+/g, '')
|
|
151
|
+
// Remove angle bracket generics: Array<string> -> Array
|
|
152
|
+
.replace(/<[\w<>\[\]|&,\s]+>/g, '');
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Clear cache (useful for development)
|
|
156
|
+
*/
|
|
157
|
+
static clearCache() {
|
|
158
|
+
if (this._cache) {
|
|
159
|
+
this._cache.clear();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
118
162
|
}
|
|
119
|
-
CodeParser._cache = new Map();
|
|
163
|
+
CodeParser._cache = new Map(); // ✅ Initialize inline
|
|
120
164
|
/**
|
|
121
165
|
* Get CSS class for a token
|
|
122
166
|
*/
|
package/lib/utils/codeparser.ts
CHANGED
|
@@ -59,12 +59,17 @@ export interface ParsedLine {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
export class CodeParser {
|
|
62
|
-
private static _cache
|
|
62
|
+
private static _cache: Map<string, ParsedLine[]> = new Map(); // ✅ Initialize inline
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
65
|
* Parse code with caching
|
|
66
66
|
*/
|
|
67
67
|
static parse(code: string, language: string = 'javascript'): ParsedLine[] {
|
|
68
|
+
// ✅ Guard against undefined cache
|
|
69
|
+
if (!this._cache) {
|
|
70
|
+
this._cache = new Map();
|
|
71
|
+
}
|
|
72
|
+
|
|
68
73
|
const cacheKey = `${language}:${code}`;
|
|
69
74
|
|
|
70
75
|
if (this._cache.has(cacheKey)) {
|
|
@@ -80,11 +85,28 @@ export class CodeParser {
|
|
|
80
85
|
const lines: ParsedLine[] = [];
|
|
81
86
|
const codeLines = code.split('\n');
|
|
82
87
|
|
|
88
|
+
// ✅ Skip parsing for non-JS languages
|
|
89
|
+
if (!['javascript', 'typescript', 'js', 'ts', 'jsx', 'tsx'].includes(language.toLowerCase())) {
|
|
90
|
+
codeLines.forEach((lineText, index) => {
|
|
91
|
+
lines.push({
|
|
92
|
+
lineNumber: index + 1,
|
|
93
|
+
tokens: [],
|
|
94
|
+
raw: lineText
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
return lines;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ✅ Strip TypeScript types for parsing
|
|
101
|
+
const cleanedCode = language.toLowerCase().includes('typescript') || language.toLowerCase().includes('ts')
|
|
102
|
+
? this._stripTypeScriptTypes(code)
|
|
103
|
+
: code;
|
|
104
|
+
|
|
83
105
|
try {
|
|
84
106
|
// Parse with Acorn (supports ES2020+)
|
|
85
107
|
const tokens: any[] = [];
|
|
86
108
|
|
|
87
|
-
acorn.parse(
|
|
109
|
+
acorn.parse(cleanedCode, {
|
|
88
110
|
ecmaVersion: 2022,
|
|
89
111
|
sourceType: 'module',
|
|
90
112
|
locations: true,
|
|
@@ -111,7 +133,7 @@ export class CodeParser {
|
|
|
111
133
|
const line = token.loc.start.line;
|
|
112
134
|
const parsedToken: ParsedToken = {
|
|
113
135
|
type: token.type.label || token.type,
|
|
114
|
-
value: token.value !== undefined ? String(token.value) :
|
|
136
|
+
value: token.value !== undefined ? String(token.value) : cleanedCode.substring(token.start, token.end),
|
|
115
137
|
start: token.start,
|
|
116
138
|
end: token.end,
|
|
117
139
|
line: token.loc.start.line,
|
|
@@ -125,7 +147,7 @@ export class CodeParser {
|
|
|
125
147
|
tokensByLine.get(line)!.push(parsedToken);
|
|
126
148
|
});
|
|
127
149
|
|
|
128
|
-
// Build lines with tokens
|
|
150
|
+
// ✅ Build lines with tokens (use ORIGINAL code, not cleaned)
|
|
129
151
|
codeLines.forEach((lineText, index) => {
|
|
130
152
|
const lineNumber = index + 1;
|
|
131
153
|
const lineTokens = tokensByLine.get(lineNumber) || [];
|
|
@@ -133,7 +155,7 @@ export class CodeParser {
|
|
|
133
155
|
lines.push({
|
|
134
156
|
lineNumber,
|
|
135
157
|
tokens: lineTokens,
|
|
136
|
-
raw: lineText
|
|
158
|
+
raw: lineText // Use original line with types
|
|
137
159
|
});
|
|
138
160
|
});
|
|
139
161
|
|
|
@@ -151,7 +173,35 @@ export class CodeParser {
|
|
|
151
173
|
|
|
152
174
|
return lines;
|
|
153
175
|
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* ✅ NEW: Strip TypeScript type annotations for parsing
|
|
179
|
+
* This is a simple regex-based approach - not perfect but works for most cases
|
|
180
|
+
*/
|
|
181
|
+
private static _stripTypeScriptTypes(code: string): string {
|
|
182
|
+
return code
|
|
183
|
+
// Remove type annotations from parameters: (name: string) -> (name)
|
|
184
|
+
.replace(/(\w+)\s*:\s*[\w<>\[\]|&]+/g, '$1')
|
|
185
|
+
// Remove return type annotations: ): string -> )
|
|
186
|
+
.replace(/\)\s*:\s*[\w<>\[\]|&]+/g, ')')
|
|
187
|
+
// Remove interface/type declarations (keep as comments to preserve line numbers)
|
|
188
|
+
.replace(/^(\s*)(interface|type)\s+\w+.*$/gm, '$1// $2 declaration')
|
|
189
|
+
// Remove as type assertions: value as string -> value
|
|
190
|
+
.replace(/\s+as\s+[\w<>\[\]|&]+/g, '')
|
|
191
|
+
// Remove angle bracket generics: Array<string> -> Array
|
|
192
|
+
.replace(/<[\w<>\[\]|&,\s]+>/g, '');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Clear cache (useful for development)
|
|
197
|
+
*/
|
|
198
|
+
static clearCache(): void {
|
|
199
|
+
if (this._cache) {
|
|
200
|
+
this._cache.clear();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
154
203
|
}
|
|
204
|
+
|
|
155
205
|
/**
|
|
156
206
|
* Get CSS class for a token
|
|
157
207
|
*/
|