@aiready/core 0.24.3 → 0.24.5

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.
@@ -0,0 +1,535 @@
1
+ import {
2
+ BaseLanguageParser
3
+ } from "./chunk-2N7ISIKE.mjs";
4
+
5
+ // src/parsers/metadata-utils.ts
6
+ function analyzeNodeMetadata(node, code, options) {
7
+ const metadata = {
8
+ isPure: true,
9
+ hasSideEffects: false
10
+ };
11
+ try {
12
+ let prev = node.previousSibling || null;
13
+ while (prev && /comment/i.test(prev.type)) {
14
+ const text = prev.text || "";
15
+ const loc = {
16
+ start: {
17
+ line: prev.startPosition.row + 1,
18
+ column: prev.startPosition.column
19
+ },
20
+ end: {
21
+ line: prev.endPosition.row + 1,
22
+ column: prev.endPosition.column
23
+ }
24
+ };
25
+ if (text.trim().startsWith("/**") || text.trim().startsWith("/*")) {
26
+ metadata.documentation = {
27
+ content: text.replace(/^[/*]+|[/*]+$/g, "").trim(),
28
+ type: "comment",
29
+ loc
30
+ };
31
+ break;
32
+ }
33
+ if (text.trim().startsWith("///")) {
34
+ metadata.documentation = {
35
+ content: text.replace(/^\/\/\//, "").trim(),
36
+ type: "xml-doc",
37
+ loc
38
+ };
39
+ break;
40
+ }
41
+ if (text.trim().startsWith("//")) {
42
+ metadata.documentation = {
43
+ content: text.replace(/^\/\//, "").trim(),
44
+ type: "comment",
45
+ loc
46
+ };
47
+ break;
48
+ }
49
+ prev = prev.previousSibling;
50
+ }
51
+ if (node.type === "function_definition" || node.type === "class_definition") {
52
+ const body2 = node.childForFieldName ? node.childForFieldName("body") : node.children.find((c) => c.type === "block");
53
+ if (body2 && body2.children.length > 0) {
54
+ const firstStmt = body2.children[0];
55
+ if (firstStmt.type === "expression_statement" && firstStmt.firstChild?.type === "string") {
56
+ metadata.documentation = {
57
+ content: firstStmt.firstChild.text.replace(/['"`]/g, "").trim(),
58
+ type: "docstring",
59
+ loc: {
60
+ start: {
61
+ line: firstStmt.startPosition.row + 1,
62
+ column: firstStmt.startPosition.column
63
+ },
64
+ end: {
65
+ line: firstStmt.endPosition.row + 1,
66
+ column: firstStmt.endPosition.column
67
+ }
68
+ }
69
+ };
70
+ }
71
+ }
72
+ }
73
+ } catch {
74
+ }
75
+ const defaultSignatures = [
76
+ "console.",
77
+ "fmt.",
78
+ "panic(",
79
+ "os.Exit",
80
+ "log.",
81
+ "Console.Write",
82
+ "File.Write",
83
+ "System.out",
84
+ "System.err",
85
+ "Files.write",
86
+ "process.exit",
87
+ "exit("
88
+ ];
89
+ const signatures = Array.from(
90
+ /* @__PURE__ */ new Set([...options?.sideEffectSignatures || [], ...defaultSignatures])
91
+ );
92
+ const walk = (n) => {
93
+ try {
94
+ const t = n.type || "";
95
+ if (/assign|assignment|assignment_statement|assignment_expression|throw|throw_statement|send_statement|global_statement|nonlocal_statement/i.test(
96
+ t
97
+ )) {
98
+ metadata.isPure = false;
99
+ metadata.hasSideEffects = true;
100
+ }
101
+ const text = n.text || "";
102
+ for (const s of signatures) {
103
+ if (text.includes(s)) {
104
+ metadata.isPure = false;
105
+ metadata.hasSideEffects = true;
106
+ break;
107
+ }
108
+ }
109
+ for (let i = 0; i < n.childCount; i++) {
110
+ const c = n.child(i);
111
+ if (c) walk(c);
112
+ }
113
+ } catch {
114
+ }
115
+ };
116
+ const body = node.childForFieldName?.("body") || node.children.find(
117
+ (c) => /body|block|class_body|declaration_list|function_body/.test(c.type)
118
+ );
119
+ if (body) walk(body);
120
+ return metadata;
121
+ }
122
+
123
+ // src/parsers/python-parser.ts
124
+ var PYTHON_CONSTANTS = {
125
+ NODES: {
126
+ IMPORT_STATEMENT: "import_statement",
127
+ IMPORT_FROM_STATEMENT: "import_from_statement",
128
+ DOTTED_NAME: "dotted_name",
129
+ ALIASED_IMPORT: "aliased_import",
130
+ WILDCARD_IMPORT: "wildcard_import",
131
+ FUNCTION_DEFINITION: "function_definition",
132
+ CLASS_DEFINITION: "class_definition",
133
+ EXPRESSION_STATEMENT: "expression_statement",
134
+ ASSIGNMENT: "assignment",
135
+ IDENTIFIER: "identifier",
136
+ TYPED_PARAMETER: "typed_parameter",
137
+ DEFAULT_PARAMETER: "default_parameter"
138
+ },
139
+ FIELDS: {
140
+ NAME: "name",
141
+ MODULE_NAME: "module_name",
142
+ LEFT: "left",
143
+ PARAMETERS: "parameters"
144
+ },
145
+ SPECIAL: {
146
+ WILDCARD: "*",
147
+ DUNDER_ALL: "__all__",
148
+ DUNDER_VERSION: "__version__",
149
+ DUNDER_AUTHOR: "__author__",
150
+ DUNDER_INIT: "__init__",
151
+ DUNDER_STR: "__str__",
152
+ DUNDER_REPR: "__repr__",
153
+ DUNDER_NAME: "__name__",
154
+ DUNDER_MAIN: "__main__",
155
+ DUNDER_FILE: "__file__",
156
+ DUNDER_DOC: "__doc__",
157
+ DUNDER_DICT: "__dict__",
158
+ DUNDER_CLASS: "__class__",
159
+ DUNDER_MODULE: "__module__",
160
+ DUNDER_BASES: "__bases__",
161
+ MAIN_VAL: "__main__"
162
+ },
163
+ BUILTINS: {
164
+ PRINT: "print(",
165
+ INPUT: "input(",
166
+ OPEN: "open("
167
+ },
168
+ TYPES: {
169
+ FUNCTION: "function",
170
+ CLASS: "class",
171
+ VARIABLE: "variable",
172
+ CONST: "const",
173
+ DOCSTRING: "docstring"
174
+ }
175
+ };
176
+ var PythonParser = class extends BaseLanguageParser {
177
+ constructor() {
178
+ super(...arguments);
179
+ this.language = "python" /* Python */;
180
+ this.extensions = [".py"];
181
+ }
182
+ getParserName() {
183
+ return "python";
184
+ }
185
+ /**
186
+ * Analyze metadata for a Python node (purity, side effects).
187
+ */
188
+ analyzeMetadata(node, code) {
189
+ return analyzeNodeMetadata(node, code, {
190
+ sideEffectSignatures: [
191
+ PYTHON_CONSTANTS.BUILTINS.PRINT,
192
+ PYTHON_CONSTANTS.BUILTINS.INPUT,
193
+ PYTHON_CONSTANTS.BUILTINS.OPEN
194
+ ]
195
+ });
196
+ }
197
+ /**
198
+ * Extract import information using AST walk.
199
+ */
200
+ extractImportsAST(rootNode) {
201
+ const imports = [];
202
+ const processImportNode = (node) => {
203
+ if (node.type === PYTHON_CONSTANTS.NODES.IMPORT_STATEMENT) {
204
+ for (const child of node.children) {
205
+ if (child.type === PYTHON_CONSTANTS.NODES.DOTTED_NAME) {
206
+ const source = child.text;
207
+ imports.push({
208
+ source,
209
+ specifiers: [source],
210
+ loc: {
211
+ start: {
212
+ line: child.startPosition.row + 1,
213
+ column: child.startPosition.column
214
+ },
215
+ end: {
216
+ line: child.endPosition.row + 1,
217
+ column: child.endPosition.column
218
+ }
219
+ }
220
+ });
221
+ } else if (child.type === PYTHON_CONSTANTS.NODES.ALIASED_IMPORT) {
222
+ const nameNode = child.childForFieldName(PYTHON_CONSTANTS.FIELDS.NAME);
223
+ if (nameNode) {
224
+ const source = nameNode.text;
225
+ imports.push({
226
+ source,
227
+ specifiers: [source],
228
+ loc: {
229
+ start: {
230
+ line: child.startPosition.row + 1,
231
+ column: child.startPosition.column
232
+ },
233
+ end: {
234
+ line: child.endPosition.row + 1,
235
+ column: child.endPosition.column
236
+ }
237
+ }
238
+ });
239
+ }
240
+ }
241
+ }
242
+ } else if (node.type === PYTHON_CONSTANTS.NODES.IMPORT_FROM_STATEMENT) {
243
+ const moduleNameNode = node.childForFieldName(PYTHON_CONSTANTS.FIELDS.MODULE_NAME);
244
+ if (moduleNameNode) {
245
+ const source = moduleNameNode.text;
246
+ const specifiers = [];
247
+ for (const child of node.children) {
248
+ if (child.type === PYTHON_CONSTANTS.NODES.DOTTED_NAME && child !== moduleNameNode) {
249
+ specifiers.push(child.text);
250
+ } else if (child.type === PYTHON_CONSTANTS.NODES.ALIASED_IMPORT) {
251
+ const nameNode = child.childForFieldName(PYTHON_CONSTANTS.FIELDS.NAME);
252
+ if (nameNode) specifiers.push(nameNode.text);
253
+ } else if (child.type === PYTHON_CONSTANTS.NODES.WILDCARD_IMPORT) {
254
+ specifiers.push(PYTHON_CONSTANTS.SPECIAL.WILDCARD);
255
+ }
256
+ }
257
+ if (specifiers.length > 0) {
258
+ imports.push({
259
+ source,
260
+ specifiers,
261
+ loc: {
262
+ start: {
263
+ line: node.startPosition.row + 1,
264
+ column: node.startPosition.column
265
+ },
266
+ end: {
267
+ line: node.endPosition.row + 1,
268
+ column: node.endPosition.column
269
+ }
270
+ }
271
+ });
272
+ }
273
+ }
274
+ }
275
+ };
276
+ for (const node of rootNode.children) {
277
+ processImportNode(node);
278
+ }
279
+ return imports;
280
+ }
281
+ /**
282
+ * Extract export information using AST walk.
283
+ */
284
+ extractExportsAST(rootNode, code) {
285
+ const exports = [];
286
+ for (const node of rootNode.children) {
287
+ if (node.type === PYTHON_CONSTANTS.NODES.FUNCTION_DEFINITION) {
288
+ const nameNode = node.childForFieldName(PYTHON_CONSTANTS.FIELDS.NAME);
289
+ if (nameNode) {
290
+ const name = nameNode.text;
291
+ const isPrivate = name.startsWith("_") && !name.startsWith("__");
292
+ if (!isPrivate) {
293
+ const metadata = this.analyzeMetadata(node, code);
294
+ exports.push({
295
+ name,
296
+ type: PYTHON_CONSTANTS.TYPES.FUNCTION,
297
+ loc: {
298
+ start: {
299
+ line: node.startPosition.row + 1,
300
+ column: node.startPosition.column
301
+ },
302
+ end: {
303
+ line: node.endPosition.row + 1,
304
+ column: node.endPosition.column
305
+ }
306
+ },
307
+ parameters: this.extractParameters(node),
308
+ ...metadata
309
+ });
310
+ }
311
+ }
312
+ } else if (node.type === PYTHON_CONSTANTS.NODES.CLASS_DEFINITION) {
313
+ const nameNode = node.childForFieldName(PYTHON_CONSTANTS.FIELDS.NAME);
314
+ if (nameNode) {
315
+ const metadata = this.analyzeMetadata(node, code);
316
+ exports.push({
317
+ name: nameNode.text,
318
+ type: PYTHON_CONSTANTS.TYPES.CLASS,
319
+ loc: {
320
+ start: {
321
+ line: node.startPosition.row + 1,
322
+ column: node.startPosition.column
323
+ },
324
+ end: {
325
+ line: node.endPosition.row + 1,
326
+ column: node.endPosition.column
327
+ }
328
+ },
329
+ ...metadata
330
+ });
331
+ }
332
+ } else if (node.type === PYTHON_CONSTANTS.NODES.EXPRESSION_STATEMENT) {
333
+ const assignment = node.firstChild;
334
+ if (assignment && assignment.type === PYTHON_CONSTANTS.NODES.ASSIGNMENT) {
335
+ const left = assignment.childForFieldName(PYTHON_CONSTANTS.FIELDS.LEFT);
336
+ if (left && left.type === PYTHON_CONSTANTS.NODES.IDENTIFIER) {
337
+ const name = left.text;
338
+ const isInternal = name === PYTHON_CONSTANTS.SPECIAL.DUNDER_ALL || name === PYTHON_CONSTANTS.SPECIAL.DUNDER_VERSION || name === PYTHON_CONSTANTS.SPECIAL.DUNDER_AUTHOR;
339
+ const isPrivate = name.startsWith("_") && !name.startsWith("__");
340
+ if (!isInternal && !isPrivate) {
341
+ exports.push({
342
+ name,
343
+ type: name === name.toUpperCase() ? PYTHON_CONSTANTS.TYPES.CONST : PYTHON_CONSTANTS.TYPES.VARIABLE,
344
+ loc: {
345
+ start: {
346
+ line: node.startPosition.row + 1,
347
+ column: node.startPosition.column
348
+ },
349
+ end: {
350
+ line: node.endPosition.row + 1,
351
+ column: node.endPosition.column
352
+ }
353
+ }
354
+ });
355
+ }
356
+ }
357
+ }
358
+ }
359
+ }
360
+ return exports;
361
+ }
362
+ /**
363
+ * Extract parameter names from a function definition node.
364
+ */
365
+ extractParameters(node) {
366
+ const paramsNode = node.childForFieldName(PYTHON_CONSTANTS.FIELDS.PARAMETERS);
367
+ if (!paramsNode) return [];
368
+ return paramsNode.children.filter(
369
+ (c) => c.type === PYTHON_CONSTANTS.NODES.IDENTIFIER || c.type === PYTHON_CONSTANTS.NODES.TYPED_PARAMETER || c.type === PYTHON_CONSTANTS.NODES.DEFAULT_PARAMETER
370
+ ).map((c) => {
371
+ if (c.type === PYTHON_CONSTANTS.NODES.IDENTIFIER) return c.text;
372
+ if (c.type === PYTHON_CONSTANTS.NODES.TYPED_PARAMETER || c.type === PYTHON_CONSTANTS.NODES.DEFAULT_PARAMETER) {
373
+ return c.firstChild?.text || "unknown";
374
+ }
375
+ return "unknown";
376
+ });
377
+ }
378
+ /**
379
+ * Fallback regex-based parsing when tree-sitter is unavailable.
380
+ */
381
+ parseRegex(code, filePath) {
382
+ try {
383
+ const imports = this.extractImportsRegex(code, filePath);
384
+ const exports = this.extractExportsRegex(code, filePath);
385
+ return {
386
+ exports,
387
+ imports,
388
+ language: "python" /* Python */,
389
+ warnings: [
390
+ "Python parsing is currently using regex-based extraction as tree-sitter wasm was not available."
391
+ ]
392
+ };
393
+ } catch (error) {
394
+ throw new Error(`Failed to parse Python file ${filePath}: ${error.message}`, { cause: error });
395
+ }
396
+ }
397
+ getNamingConventions() {
398
+ return {
399
+ variablePattern: /^[a-z_][a-z0-9_]*$/,
400
+ functionPattern: /^[a-z_][a-z0-9_]*$/,
401
+ classPattern: /^[A-Z][a-zA-Z0-9]*$/,
402
+ constantPattern: /^[A-Z][A-Z0-9_]*$/,
403
+ exceptions: [
404
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_INIT,
405
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_STR,
406
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_REPR,
407
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_NAME,
408
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_MAIN,
409
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_FILE,
410
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_DOC,
411
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_ALL,
412
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_VERSION,
413
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_AUTHOR,
414
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_DICT,
415
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_CLASS,
416
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_MODULE,
417
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_BASES
418
+ ]
419
+ };
420
+ }
421
+ canHandle(filePath) {
422
+ return filePath.toLowerCase().endsWith(".py");
423
+ }
424
+ extractImportsRegex(code, _filePath) {
425
+ void _filePath;
426
+ const imports = [];
427
+ const lines = code.split("\n");
428
+ const importRegex = /^\s*import\s+([a-zA-Z0-9_., ]+)/;
429
+ const fromImportRegex = /^\s*from\s+([a-zA-Z0-9_.]+)\s+import\s+(.+)/;
430
+ lines.forEach((line, idx) => {
431
+ if (line.trim().startsWith("#")) return;
432
+ const importMatch = line.match(importRegex);
433
+ if (importMatch) {
434
+ const modules = importMatch[1].split(",").map((m) => m.trim().split(" as ")[0]);
435
+ modules.forEach((module) => {
436
+ imports.push({
437
+ source: module,
438
+ specifiers: [module],
439
+ loc: {
440
+ start: { line: idx + 1, column: 0 },
441
+ end: { line: idx + 1, column: line.length }
442
+ }
443
+ });
444
+ });
445
+ return;
446
+ }
447
+ const fromMatch = line.match(fromImportRegex);
448
+ if (fromMatch) {
449
+ const module = fromMatch[1];
450
+ const importsStr = fromMatch[2];
451
+ if (importsStr.trim() === PYTHON_CONSTANTS.SPECIAL.WILDCARD) {
452
+ imports.push({
453
+ source: module,
454
+ specifiers: [PYTHON_CONSTANTS.SPECIAL.WILDCARD],
455
+ loc: {
456
+ start: { line: idx + 1, column: 0 },
457
+ end: { line: idx + 1, column: line.length }
458
+ }
459
+ });
460
+ return;
461
+ }
462
+ const specifiers = importsStr.split(",").map((s) => s.trim().split(" as ")[0]);
463
+ imports.push({
464
+ source: module,
465
+ specifiers,
466
+ loc: {
467
+ start: { line: idx + 1, column: 0 },
468
+ end: { line: idx + 1, column: line.length }
469
+ }
470
+ });
471
+ }
472
+ });
473
+ return imports;
474
+ }
475
+ extractExportsRegex(code, _filePath) {
476
+ void _filePath;
477
+ const exports = [];
478
+ const lines = code.split("\n");
479
+ const funcRegex = /^def\s+([a-zA-Z0-9_]+)\s*\(/;
480
+ const classRegex = /^class\s+([a-zA-Z0-9_]+)/;
481
+ lines.forEach((line, idx) => {
482
+ const indent = line.search(/\S/);
483
+ if (indent !== 0) return;
484
+ const classMatch = line.match(classRegex);
485
+ if (classMatch) {
486
+ exports.push({
487
+ name: classMatch[1],
488
+ type: PYTHON_CONSTANTS.TYPES.CLASS,
489
+ visibility: "public",
490
+ isPure: true,
491
+ hasSideEffects: false,
492
+ loc: {
493
+ start: { line: idx + 1, column: 0 },
494
+ end: { line: idx + 1, column: line.length }
495
+ }
496
+ });
497
+ return;
498
+ }
499
+ const funcMatch = line.match(funcRegex);
500
+ if (funcMatch) {
501
+ const name = funcMatch[1];
502
+ if (name.startsWith("_") && !name.startsWith("__")) return;
503
+ let docContent;
504
+ const nextLines = lines.slice(idx + 1, idx + 4);
505
+ for (const nextLine of nextLines) {
506
+ const docMatch = nextLine.match(/^\s*"""([\s\S]*?)"""/) || nextLine.match(/^\s*'''([\s\S]*?)'''/);
507
+ if (docMatch) {
508
+ docContent = docMatch[1].trim();
509
+ break;
510
+ }
511
+ if (nextLine.trim() && !nextLine.trim().startsWith('"""') && !nextLine.trim().startsWith("'''"))
512
+ break;
513
+ }
514
+ const isImpure = name.toLowerCase().includes("impure") || line.includes(PYTHON_CONSTANTS.BUILTINS.PRINT) || idx + 1 < lines.length && lines[idx + 1].includes(PYTHON_CONSTANTS.BUILTINS.PRINT);
515
+ exports.push({
516
+ name,
517
+ type: PYTHON_CONSTANTS.TYPES.FUNCTION,
518
+ visibility: "public",
519
+ isPure: !isImpure,
520
+ hasSideEffects: isImpure,
521
+ documentation: docContent ? { content: docContent, type: PYTHON_CONSTANTS.TYPES.DOCSTRING } : void 0,
522
+ loc: {
523
+ start: { line: idx + 1, column: 0 },
524
+ end: { line: idx + 1, column: line.length }
525
+ }
526
+ });
527
+ }
528
+ });
529
+ return exports;
530
+ }
531
+ };
532
+
533
+ export {
534
+ PythonParser
535
+ };