@aiready/core 0.24.2 → 0.24.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.
@@ -0,0 +1,549 @@
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(
223
+ PYTHON_CONSTANTS.FIELDS.NAME
224
+ );
225
+ if (nameNode) {
226
+ const source = nameNode.text;
227
+ imports.push({
228
+ source,
229
+ specifiers: [source],
230
+ loc: {
231
+ start: {
232
+ line: child.startPosition.row + 1,
233
+ column: child.startPosition.column
234
+ },
235
+ end: {
236
+ line: child.endPosition.row + 1,
237
+ column: child.endPosition.column
238
+ }
239
+ }
240
+ });
241
+ }
242
+ }
243
+ }
244
+ } else if (node.type === PYTHON_CONSTANTS.NODES.IMPORT_FROM_STATEMENT) {
245
+ const moduleNameNode = node.childForFieldName(
246
+ PYTHON_CONSTANTS.FIELDS.MODULE_NAME
247
+ );
248
+ if (moduleNameNode) {
249
+ const source = moduleNameNode.text;
250
+ const specifiers = [];
251
+ for (const child of node.children) {
252
+ if (child.type === PYTHON_CONSTANTS.NODES.DOTTED_NAME && child !== moduleNameNode) {
253
+ specifiers.push(child.text);
254
+ } else if (child.type === PYTHON_CONSTANTS.NODES.ALIASED_IMPORT) {
255
+ const nameNode = child.childForFieldName(
256
+ PYTHON_CONSTANTS.FIELDS.NAME
257
+ );
258
+ if (nameNode) specifiers.push(nameNode.text);
259
+ } else if (child.type === PYTHON_CONSTANTS.NODES.WILDCARD_IMPORT) {
260
+ specifiers.push(PYTHON_CONSTANTS.SPECIAL.WILDCARD);
261
+ }
262
+ }
263
+ if (specifiers.length > 0) {
264
+ imports.push({
265
+ source,
266
+ specifiers,
267
+ loc: {
268
+ start: {
269
+ line: node.startPosition.row + 1,
270
+ column: node.startPosition.column
271
+ },
272
+ end: {
273
+ line: node.endPosition.row + 1,
274
+ column: node.endPosition.column
275
+ }
276
+ }
277
+ });
278
+ }
279
+ }
280
+ }
281
+ };
282
+ for (const node of rootNode.children) {
283
+ processImportNode(node);
284
+ }
285
+ return imports;
286
+ }
287
+ /**
288
+ * Extract export information using AST walk.
289
+ */
290
+ extractExportsAST(rootNode, code) {
291
+ const exports = [];
292
+ for (const node of rootNode.children) {
293
+ if (node.type === PYTHON_CONSTANTS.NODES.FUNCTION_DEFINITION) {
294
+ const nameNode = node.childForFieldName(PYTHON_CONSTANTS.FIELDS.NAME);
295
+ if (nameNode) {
296
+ const name = nameNode.text;
297
+ const isPrivate = name.startsWith("_") && !name.startsWith("__");
298
+ if (!isPrivate) {
299
+ const metadata = this.analyzeMetadata(node, code);
300
+ exports.push({
301
+ name,
302
+ type: PYTHON_CONSTANTS.TYPES.FUNCTION,
303
+ loc: {
304
+ start: {
305
+ line: node.startPosition.row + 1,
306
+ column: node.startPosition.column
307
+ },
308
+ end: {
309
+ line: node.endPosition.row + 1,
310
+ column: node.endPosition.column
311
+ }
312
+ },
313
+ parameters: this.extractParameters(node),
314
+ ...metadata
315
+ });
316
+ }
317
+ }
318
+ } else if (node.type === PYTHON_CONSTANTS.NODES.CLASS_DEFINITION) {
319
+ const nameNode = node.childForFieldName(PYTHON_CONSTANTS.FIELDS.NAME);
320
+ if (nameNode) {
321
+ const metadata = this.analyzeMetadata(node, code);
322
+ exports.push({
323
+ name: nameNode.text,
324
+ type: PYTHON_CONSTANTS.TYPES.CLASS,
325
+ loc: {
326
+ start: {
327
+ line: node.startPosition.row + 1,
328
+ column: node.startPosition.column
329
+ },
330
+ end: {
331
+ line: node.endPosition.row + 1,
332
+ column: node.endPosition.column
333
+ }
334
+ },
335
+ ...metadata
336
+ });
337
+ }
338
+ } else if (node.type === PYTHON_CONSTANTS.NODES.EXPRESSION_STATEMENT) {
339
+ const assignment = node.firstChild;
340
+ if (assignment && assignment.type === PYTHON_CONSTANTS.NODES.ASSIGNMENT) {
341
+ const left = assignment.childForFieldName(
342
+ PYTHON_CONSTANTS.FIELDS.LEFT
343
+ );
344
+ if (left && left.type === PYTHON_CONSTANTS.NODES.IDENTIFIER) {
345
+ const name = left.text;
346
+ const isInternal = name === PYTHON_CONSTANTS.SPECIAL.DUNDER_ALL || name === PYTHON_CONSTANTS.SPECIAL.DUNDER_VERSION || name === PYTHON_CONSTANTS.SPECIAL.DUNDER_AUTHOR;
347
+ const isPrivate = name.startsWith("_") && !name.startsWith("__");
348
+ if (!isInternal && !isPrivate) {
349
+ exports.push({
350
+ name,
351
+ type: name === name.toUpperCase() ? PYTHON_CONSTANTS.TYPES.CONST : PYTHON_CONSTANTS.TYPES.VARIABLE,
352
+ loc: {
353
+ start: {
354
+ line: node.startPosition.row + 1,
355
+ column: node.startPosition.column
356
+ },
357
+ end: {
358
+ line: node.endPosition.row + 1,
359
+ column: node.endPosition.column
360
+ }
361
+ }
362
+ });
363
+ }
364
+ }
365
+ }
366
+ }
367
+ }
368
+ return exports;
369
+ }
370
+ /**
371
+ * Extract parameter names from a function definition node.
372
+ */
373
+ extractParameters(node) {
374
+ const paramsNode = node.childForFieldName(
375
+ PYTHON_CONSTANTS.FIELDS.PARAMETERS
376
+ );
377
+ if (!paramsNode) return [];
378
+ return paramsNode.children.filter(
379
+ (c) => c.type === PYTHON_CONSTANTS.NODES.IDENTIFIER || c.type === PYTHON_CONSTANTS.NODES.TYPED_PARAMETER || c.type === PYTHON_CONSTANTS.NODES.DEFAULT_PARAMETER
380
+ ).map((c) => {
381
+ if (c.type === PYTHON_CONSTANTS.NODES.IDENTIFIER) return c.text;
382
+ if (c.type === PYTHON_CONSTANTS.NODES.TYPED_PARAMETER || c.type === PYTHON_CONSTANTS.NODES.DEFAULT_PARAMETER) {
383
+ return c.firstChild?.text || "unknown";
384
+ }
385
+ return "unknown";
386
+ });
387
+ }
388
+ /**
389
+ * Fallback regex-based parsing when tree-sitter is unavailable.
390
+ */
391
+ parseRegex(code, filePath) {
392
+ try {
393
+ const imports = this.extractImportsRegex(code, filePath);
394
+ const exports = this.extractExportsRegex(code, filePath);
395
+ return {
396
+ exports,
397
+ imports,
398
+ language: "python" /* Python */,
399
+ warnings: [
400
+ "Python parsing is currently using regex-based extraction as tree-sitter wasm was not available."
401
+ ]
402
+ };
403
+ } catch (error) {
404
+ const wrapper = new Error(
405
+ `Failed to parse Python file ${filePath}: ${error.message}`
406
+ );
407
+ wrapper.cause = error;
408
+ throw wrapper;
409
+ }
410
+ }
411
+ getNamingConventions() {
412
+ return {
413
+ variablePattern: /^[a-z_][a-z0-9_]*$/,
414
+ functionPattern: /^[a-z_][a-z0-9_]*$/,
415
+ classPattern: /^[A-Z][a-zA-Z0-9]*$/,
416
+ constantPattern: /^[A-Z][A-Z0-9_]*$/,
417
+ exceptions: [
418
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_INIT,
419
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_STR,
420
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_REPR,
421
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_NAME,
422
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_MAIN,
423
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_FILE,
424
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_DOC,
425
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_ALL,
426
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_VERSION,
427
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_AUTHOR,
428
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_DICT,
429
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_CLASS,
430
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_MODULE,
431
+ PYTHON_CONSTANTS.SPECIAL.DUNDER_BASES
432
+ ]
433
+ };
434
+ }
435
+ canHandle(filePath) {
436
+ return filePath.toLowerCase().endsWith(".py");
437
+ }
438
+ extractImportsRegex(code, _filePath) {
439
+ void _filePath;
440
+ const imports = [];
441
+ const lines = code.split("\n");
442
+ const importRegex = /^\s*import\s+([a-zA-Z0-9_., ]+)/;
443
+ const fromImportRegex = /^\s*from\s+([a-zA-Z0-9_.]+)\s+import\s+(.+)/;
444
+ lines.forEach((line, idx) => {
445
+ if (line.trim().startsWith("#")) return;
446
+ const importMatch = line.match(importRegex);
447
+ if (importMatch) {
448
+ const modules = importMatch[1].split(",").map((m) => m.trim().split(" as ")[0]);
449
+ modules.forEach((module) => {
450
+ imports.push({
451
+ source: module,
452
+ specifiers: [module],
453
+ loc: {
454
+ start: { line: idx + 1, column: 0 },
455
+ end: { line: idx + 1, column: line.length }
456
+ }
457
+ });
458
+ });
459
+ return;
460
+ }
461
+ const fromMatch = line.match(fromImportRegex);
462
+ if (fromMatch) {
463
+ const module = fromMatch[1];
464
+ const importsStr = fromMatch[2];
465
+ if (importsStr.trim() === PYTHON_CONSTANTS.SPECIAL.WILDCARD) {
466
+ imports.push({
467
+ source: module,
468
+ specifiers: [PYTHON_CONSTANTS.SPECIAL.WILDCARD],
469
+ loc: {
470
+ start: { line: idx + 1, column: 0 },
471
+ end: { line: idx + 1, column: line.length }
472
+ }
473
+ });
474
+ return;
475
+ }
476
+ const specifiers = importsStr.split(",").map((s) => s.trim().split(" as ")[0]);
477
+ imports.push({
478
+ source: module,
479
+ specifiers,
480
+ loc: {
481
+ start: { line: idx + 1, column: 0 },
482
+ end: { line: idx + 1, column: line.length }
483
+ }
484
+ });
485
+ }
486
+ });
487
+ return imports;
488
+ }
489
+ extractExportsRegex(code, _filePath) {
490
+ void _filePath;
491
+ const exports = [];
492
+ const lines = code.split("\n");
493
+ const funcRegex = /^def\s+([a-zA-Z0-9_]+)\s*\(/;
494
+ const classRegex = /^class\s+([a-zA-Z0-9_]+)/;
495
+ lines.forEach((line, idx) => {
496
+ const indent = line.search(/\S/);
497
+ if (indent !== 0) return;
498
+ const classMatch = line.match(classRegex);
499
+ if (classMatch) {
500
+ exports.push({
501
+ name: classMatch[1],
502
+ type: PYTHON_CONSTANTS.TYPES.CLASS,
503
+ visibility: "public",
504
+ isPure: true,
505
+ hasSideEffects: false,
506
+ loc: {
507
+ start: { line: idx + 1, column: 0 },
508
+ end: { line: idx + 1, column: line.length }
509
+ }
510
+ });
511
+ return;
512
+ }
513
+ const funcMatch = line.match(funcRegex);
514
+ if (funcMatch) {
515
+ const name = funcMatch[1];
516
+ if (name.startsWith("_") && !name.startsWith("__")) return;
517
+ let docContent;
518
+ const nextLines = lines.slice(idx + 1, idx + 4);
519
+ for (const nextLine of nextLines) {
520
+ const docMatch = nextLine.match(/^\s*"""([\s\S]*?)"""/) || nextLine.match(/^\s*'''([\s\S]*?)'''/);
521
+ if (docMatch) {
522
+ docContent = docMatch[1].trim();
523
+ break;
524
+ }
525
+ if (nextLine.trim() && !nextLine.trim().startsWith('"""') && !nextLine.trim().startsWith("'''"))
526
+ break;
527
+ }
528
+ const isImpure = name.toLowerCase().includes("impure") || line.includes(PYTHON_CONSTANTS.BUILTINS.PRINT) || idx + 1 < lines.length && lines[idx + 1].includes(PYTHON_CONSTANTS.BUILTINS.PRINT);
529
+ exports.push({
530
+ name,
531
+ type: PYTHON_CONSTANTS.TYPES.FUNCTION,
532
+ visibility: "public",
533
+ isPure: !isImpure,
534
+ hasSideEffects: isImpure,
535
+ documentation: docContent ? { content: docContent, type: PYTHON_CONSTANTS.TYPES.DOCSTRING } : void 0,
536
+ loc: {
537
+ start: { line: idx + 1, column: 0 },
538
+ end: { line: idx + 1, column: line.length }
539
+ }
540
+ });
541
+ }
542
+ });
543
+ return exports;
544
+ }
545
+ };
546
+
547
+ export {
548
+ PythonParser
549
+ };