juxscript 1.1.46 → 1.1.47

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,CAAmC;IAExD;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAqB,GAAG,UAAU,EAAE;IAYzE,OAAO,CAAC,MAAM,CAAC,UAAU;CA2E5B;AACD;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM,CAUhD;AAED;;GAEG;AACH,wBAAgB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M,CAUhD;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAS/C;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAiCnE;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO/C;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAmB9C;AAID;;GAEG;AACH,eAAO,MAAM,UAAU;;;;;;;;CAQtB,CAAC;AAEF,eAAe,UAAU,CAAC"}
@@ -52,10 +52,25 @@ export class CodeParser {
52
52
  static _parseCode(code, language) {
53
53
  const lines = [];
54
54
  const codeLines = code.split('\n');
55
+ // ✅ Skip parsing for non-JS languages
56
+ if (!['javascript', 'typescript', 'js', 'ts', 'jsx', 'tsx'].includes(language.toLowerCase())) {
57
+ codeLines.forEach((lineText, index) => {
58
+ lines.push({
59
+ lineNumber: index + 1,
60
+ tokens: [],
61
+ raw: lineText
62
+ });
63
+ });
64
+ return lines;
65
+ }
66
+ // ✅ Strip TypeScript types for parsing
67
+ const cleanedCode = language.toLowerCase().includes('typescript') || language.toLowerCase().includes('ts')
68
+ ? this._stripTypeScriptTypes(code)
69
+ : code;
55
70
  try {
56
71
  // Parse with Acorn (supports ES2020+)
57
72
  const tokens = [];
58
- acorn.parse(code, {
73
+ acorn.parse(cleanedCode, {
59
74
  ecmaVersion: 2022,
60
75
  sourceType: 'module',
61
76
  locations: true,
@@ -79,7 +94,7 @@ export class CodeParser {
79
94
  const line = token.loc.start.line;
80
95
  const parsedToken = {
81
96
  type: token.type.label || token.type,
82
- value: token.value !== undefined ? String(token.value) : code.substring(token.start, token.end),
97
+ value: token.value !== undefined ? String(token.value) : cleanedCode.substring(token.start, token.end),
83
98
  start: token.start,
84
99
  end: token.end,
85
100
  line: token.loc.start.line,
@@ -91,14 +106,14 @@ export class CodeParser {
91
106
  }
92
107
  tokensByLine.get(line).push(parsedToken);
93
108
  });
94
- // Build lines with tokens
109
+ // Build lines with tokens (use ORIGINAL code, not cleaned)
95
110
  codeLines.forEach((lineText, index) => {
96
111
  const lineNumber = index + 1;
97
112
  const lineTokens = tokensByLine.get(lineNumber) || [];
98
113
  lines.push({
99
114
  lineNumber,
100
115
  tokens: lineTokens,
101
- raw: lineText
116
+ raw: lineText // Use original line with types
102
117
  });
103
118
  });
104
119
  }
@@ -115,6 +130,29 @@ export class CodeParser {
115
130
  }
116
131
  return lines;
117
132
  }
133
+ /**
134
+ * ✅ NEW: Strip TypeScript type annotations for parsing
135
+ * This is a simple regex-based approach - not perfect but works for most cases
136
+ */
137
+ static _stripTypeScriptTypes(code) {
138
+ return code
139
+ // Remove type annotations from parameters: (name: string) -> (name)
140
+ .replace(/(\w+)\s*:\s*[\w<>\[\]|&]+/g, '$1')
141
+ // Remove return type annotations: ): string -> )
142
+ .replace(/\)\s*:\s*[\w<>\[\]|&]+/g, ')')
143
+ // Remove interface/type declarations (keep as comments to preserve line numbers)
144
+ .replace(/^(\s*)(interface|type)\s+\w+.*$/gm, '$1// $2 declaration')
145
+ // Remove as type assertions: value as string -> value
146
+ .replace(/\s+as\s+[\w<>\[\]|&]+/g, '')
147
+ // Remove angle bracket generics: Array<string> -> Array
148
+ .replace(/<[\w<>\[\]|&,\s]+>/g, '');
149
+ }
150
+ /**
151
+ * Clear cache (useful for development)
152
+ */
153
+ static clearCache() {
154
+ this._cache.clear();
155
+ }
118
156
  }
119
157
  CodeParser._cache = new Map();
120
158
  /**
@@ -80,11 +80,28 @@ export class CodeParser {
80
80
  const lines: ParsedLine[] = [];
81
81
  const codeLines = code.split('\n');
82
82
 
83
+ // ✅ Skip parsing for non-JS languages
84
+ if (!['javascript', 'typescript', 'js', 'ts', 'jsx', 'tsx'].includes(language.toLowerCase())) {
85
+ codeLines.forEach((lineText, index) => {
86
+ lines.push({
87
+ lineNumber: index + 1,
88
+ tokens: [],
89
+ raw: lineText
90
+ });
91
+ });
92
+ return lines;
93
+ }
94
+
95
+ // ✅ Strip TypeScript types for parsing
96
+ const cleanedCode = language.toLowerCase().includes('typescript') || language.toLowerCase().includes('ts')
97
+ ? this._stripTypeScriptTypes(code)
98
+ : code;
99
+
83
100
  try {
84
101
  // Parse with Acorn (supports ES2020+)
85
102
  const tokens: any[] = [];
86
103
 
87
- acorn.parse(code, {
104
+ acorn.parse(cleanedCode, {
88
105
  ecmaVersion: 2022,
89
106
  sourceType: 'module',
90
107
  locations: true,
@@ -111,7 +128,7 @@ export class CodeParser {
111
128
  const line = token.loc.start.line;
112
129
  const parsedToken: ParsedToken = {
113
130
  type: token.type.label || token.type,
114
- value: token.value !== undefined ? String(token.value) : code.substring(token.start, token.end),
131
+ value: token.value !== undefined ? String(token.value) : cleanedCode.substring(token.start, token.end),
115
132
  start: token.start,
116
133
  end: token.end,
117
134
  line: token.loc.start.line,
@@ -125,7 +142,7 @@ export class CodeParser {
125
142
  tokensByLine.get(line)!.push(parsedToken);
126
143
  });
127
144
 
128
- // Build lines with tokens
145
+ // Build lines with tokens (use ORIGINAL code, not cleaned)
129
146
  codeLines.forEach((lineText, index) => {
130
147
  const lineNumber = index + 1;
131
148
  const lineTokens = tokensByLine.get(lineNumber) || [];
@@ -133,7 +150,7 @@ export class CodeParser {
133
150
  lines.push({
134
151
  lineNumber,
135
152
  tokens: lineTokens,
136
- raw: lineText
153
+ raw: lineText // Use original line with types
137
154
  });
138
155
  });
139
156
 
@@ -151,7 +168,33 @@ export class CodeParser {
151
168
 
152
169
  return lines;
153
170
  }
171
+
172
+ /**
173
+ * ✅ NEW: Strip TypeScript type annotations for parsing
174
+ * This is a simple regex-based approach - not perfect but works for most cases
175
+ */
176
+ private static _stripTypeScriptTypes(code: string): string {
177
+ return code
178
+ // Remove type annotations from parameters: (name: string) -> (name)
179
+ .replace(/(\w+)\s*:\s*[\w<>\[\]|&]+/g, '$1')
180
+ // Remove return type annotations: ): string -> )
181
+ .replace(/\)\s*:\s*[\w<>\[\]|&]+/g, ')')
182
+ // Remove interface/type declarations (keep as comments to preserve line numbers)
183
+ .replace(/^(\s*)(interface|type)\s+\w+.*$/gm, '$1// $2 declaration')
184
+ // Remove as type assertions: value as string -> value
185
+ .replace(/\s+as\s+[\w<>\[\]|&]+/g, '')
186
+ // Remove angle bracket generics: Array<string> -> Array
187
+ .replace(/<[\w<>\[\]|&,\s]+>/g, '');
188
+ }
189
+
190
+ /**
191
+ * Clear cache (useful for development)
192
+ */
193
+ static clearCache(): void {
194
+ this._cache.clear();
195
+ }
154
196
  }
197
+
155
198
  /**
156
199
  * Get CSS class for a token
157
200
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "juxscript",
3
- "version": "1.1.46",
3
+ "version": "1.1.47",
4
4
  "type": "module",
5
5
  "description": "A JavaScript UX authorship platform",
6
6
  "main": "index.js",