@aiderdesk/tree-sitter-utils 0.1.0

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.
Files changed (59) hide show
  1. package/README.md +151 -0
  2. package/dist/cache-manager.d.ts +16 -0
  3. package/dist/cache-manager.d.ts.map +1 -0
  4. package/dist/constants.d.ts +4 -0
  5. package/dist/constants.d.ts.map +1 -0
  6. package/dist/file-utils.d.ts +2 -0
  7. package/dist/file-utils.d.ts.map +1 -0
  8. package/dist/graph-builder.d.ts +13 -0
  9. package/dist/graph-builder.d.ts.map +1 -0
  10. package/dist/index.d.ts +27 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +1070 -0
  13. package/dist/index.js.map +7 -0
  14. package/dist/language-detector.d.ts +5 -0
  15. package/dist/language-detector.d.ts.map +1 -0
  16. package/dist/logger.d.ts +11 -0
  17. package/dist/logger.d.ts.map +1 -0
  18. package/dist/pagerank.d.ts +7 -0
  19. package/dist/pagerank.d.ts.map +1 -0
  20. package/dist/symbol-extractor.d.ts +14 -0
  21. package/dist/symbol-extractor.d.ts.map +1 -0
  22. package/dist/tree-renderer.d.ts +9 -0
  23. package/dist/tree-renderer.d.ts.map +1 -0
  24. package/dist/tree-sitter-analyzer.d.ts +16 -0
  25. package/dist/tree-sitter-analyzer.d.ts.map +1 -0
  26. package/dist/tree-sitter-parser.d.ts +13 -0
  27. package/dist/tree-sitter-parser.d.ts.map +1 -0
  28. package/dist/types.d.ts +57 -0
  29. package/dist/types.d.ts.map +1 -0
  30. package/dist/wasm-downloader.d.ts +7 -0
  31. package/dist/wasm-downloader.d.ts.map +1 -0
  32. package/package.json +62 -0
  33. package/resources/queries/c-tags.scm +9 -0
  34. package/resources/queries/c_sharp-tags.scm +46 -0
  35. package/resources/queries/cpp-tags.scm +15 -0
  36. package/resources/queries/dart-tags.scm +91 -0
  37. package/resources/queries/elisp-tags.scm +8 -0
  38. package/resources/queries/elixir-tags.scm +54 -0
  39. package/resources/queries/elm-tags.scm +19 -0
  40. package/resources/queries/fortran-tags.scm +15 -0
  41. package/resources/queries/go-tags.scm +30 -0
  42. package/resources/queries/haskell-tags.scm +3 -0
  43. package/resources/queries/hcl-tags.scm +77 -0
  44. package/resources/queries/java-tags.scm +20 -0
  45. package/resources/queries/javascript-tags.scm +94 -0
  46. package/resources/queries/julia-tags.scm +60 -0
  47. package/resources/queries/kotlin-tags.scm +27 -0
  48. package/resources/queries/matlab-tags.scm +10 -0
  49. package/resources/queries/ocaml-tags.scm +115 -0
  50. package/resources/queries/ocaml_interface-tags.scm +98 -0
  51. package/resources/queries/php-tags.scm +26 -0
  52. package/resources/queries/python-tags.scm +12 -0
  53. package/resources/queries/ql-tags.scm +26 -0
  54. package/resources/queries/ruby-tags.scm +64 -0
  55. package/resources/queries/rust-tags.scm +60 -0
  56. package/resources/queries/scala-tags.scm +65 -0
  57. package/resources/queries/tsx-tags.scm +51 -0
  58. package/resources/queries/typescript-tags.scm +61 -0
  59. package/resources/queries/zig-tags.scm +3 -0
package/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # @aiderdesk/tree-sitter-utils
2
+
3
+ A comprehensive tree-sitter library for code analysis, symbol extraction, and repository mapping.
4
+
5
+ ## Features
6
+
7
+ - 🌳 **Tree-Sitter Based**: Uses tree-sitter for accurate AST parsing
8
+ - πŸ“¦ **On-Demand WASM Loading**: Automatically downloads language WASM files when needed
9
+ - πŸ” **Symbol Extraction**: Extract functions, classes, variables, and other symbols from code
10
+ - πŸ—ΊοΈ **Repository Mapping**: Generate comprehensive repository maps with PageRank-based symbol ranking
11
+ - πŸ’Ύ **Smart Caching**: SQLite-based caching for fast repeated analysis
12
+ - 🌐 **Multi-Language Support**: Supports 18+ programming languages out of the box
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @aiderdesk/tree-sitter-utils
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ### Extract Symbols from Files
23
+
24
+ ```typescript
25
+ import { extractSymbols } from '@aiderdesk/tree-sitter-utils';
26
+
27
+ const symbols = await extractSymbols([
28
+ '/path/to/file.ts',
29
+ '/path/to/another-file.py'
30
+ ]);
31
+
32
+ console.log(symbols);
33
+ // [
34
+ // { name: 'MyClass', kind: 'def', line: 10, rel_fname: 'file.ts', ... },
35
+ // { name: 'myFunction', kind: 'def', line: 25, rel_fname: 'file.ts', ... }
36
+ // ]
37
+ ```
38
+
39
+ ### Generate Repository Map
40
+
41
+ ```typescript
42
+ import { getRepoMap } from '@aiderdesk/tree-sitter-utils';
43
+
44
+ const map = await getRepoMap('/path/to/project', {
45
+ maxLines: 1000,
46
+ excludePatterns: ['node_modules/**', 'dist/**']
47
+ });
48
+
49
+ console.log(map);
50
+ // src/index.ts:
51
+ // β”œβ”€β”€ class MyClass
52
+ // β”œβ”€β”€ function myFunction
53
+ // ...
54
+ ```
55
+
56
+ ### Advanced Usage with TreeSitterAnalyzer
57
+
58
+ ```typescript
59
+ import { TreeSitterAnalyzer } from '@aiderdesk/tree-sitter-utils';
60
+
61
+ const analyzer = new TreeSitterAnalyzer();
62
+
63
+ // Initialize (optional - lazy initialization supported)
64
+ await analyzer.initialize();
65
+
66
+ // Get repository map
67
+ const repoMap = await analyzer.getRepoMap('/path/to/project', {
68
+ maxLines: 500,
69
+ mentionedFiles: new Set(['src/important.ts']),
70
+ mentionedIdents: new Set(['ImportantClass'])
71
+ });
72
+
73
+ // Extract symbols
74
+ const symbols = await analyzer.extractSymbols(['/path/to/file.ts'], {
75
+ useCache: true
76
+ });
77
+
78
+ // Parse single file
79
+ const parseResult = await analyzer.parseFile('/path/to/file.ts');
80
+ ```
81
+
82
+ ## Supported Languages
83
+
84
+ - Python
85
+ - JavaScript / JSX
86
+ - TypeScript / TSX
87
+ - Java
88
+ - Go
89
+ - Rust
90
+ - C / C++
91
+ - C#
92
+ - Ruby
93
+ - PHP
94
+ - Scala
95
+ - Dart
96
+ - OCaml / OCaml Interface
97
+ - Haskell
98
+ - Julia
99
+
100
+ ## Configuration Options
101
+
102
+ ### RepoMapOptions
103
+
104
+ ```typescript
105
+ interface RepoMapOptions {
106
+ root: string; // Root directory of the project
107
+ files?: string[]; // Specific files to analyze (optional)
108
+ excludePatterns?: string[]; // Glob patterns to exclude
109
+ mentionedFiles?: Set<string>; // Files to prioritize in ranking
110
+ mentionedIdents?: Set<string>; // Identifiers to prioritize
111
+ chatFiles?: Set<string>; // Files to exclude from output
112
+ maxLines?: number; // Maximum lines in output
113
+ }
114
+ ```
115
+
116
+ ### SymbolOptions
117
+
118
+ ```typescript
119
+ interface SymbolOptions {
120
+ useCache?: boolean; // Use SQLite cache (default: true)
121
+ rootDir?: string; // Root directory for relative paths
122
+ }
123
+ ```
124
+
125
+ ## How It Works
126
+
127
+ 1. **On-Demand WASM Loading**: When a language is first encountered, the library automatically downloads its WASM file from unpkg and caches it in `~/.cache/aiderdesk/tree-sitter/wasm/`
128
+
129
+ 2. **Tree-Sitter Parsing**: Uses tree-sitter queries (`.scm` files) to extract definitions and references from source code
130
+
131
+ 3. **PageRank Algorithm**: Builds a dependency graph and applies PageRank to rank symbols by importance
132
+
133
+ 4. **Smart Caching**: SQLite-based caching tracks file modification times to avoid re-parsing unchanged files
134
+
135
+ ## API Reference
136
+
137
+ ### Convenience Functions
138
+
139
+ - `extractSymbols(filePaths: string[], options?: SymbolOptions): Promise<Symbol[]>`
140
+ - `getRepoMap(root: string, options?: RepoMapOptions): Promise<string>`
141
+
142
+ ### TreeSitterAnalyzer Class
143
+
144
+ - `initialize(): Promise<void>` - Initialize the analyzer
145
+ - `getRepoMap(options: RepoMapOptions): Promise<string>` - Generate repository map
146
+ - `extractSymbols(filePaths: string[], options?: SymbolOptions): Promise<Symbol[]>` - Extract symbols
147
+ - `parseFile(filePath: string): Promise<ParseResult>` - Parse single file
148
+
149
+ ## License
150
+
151
+ MIT
@@ -0,0 +1,16 @@
1
+ import { Tag } from './types';
2
+ export declare class CacheManager {
3
+ private db;
4
+ private dbPath;
5
+ constructor(dbPath?: string);
6
+ initialize(): Promise<void>;
7
+ getFileCache(filePath: string): Promise<{
8
+ tags: Tag[];
9
+ mtime: number;
10
+ } | null>;
11
+ setFileCache(filePath: string, mtime: number, tags: Tag[]): Promise<void>;
12
+ getFileMtime(filePath: string): Promise<number | null>;
13
+ clearCache(): Promise<void>;
14
+ close(): void;
15
+ }
16
+ //# sourceMappingURL=cache-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-manager.d.ts","sourceRoot":"","sources":["../src/cache-manager.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAmB9B,qBAAa,YAAY;IACvB,OAAO,CAAC,EAAE,CAA6B;IACvC,OAAO,CAAC,MAAM,CAAS;gBAEX,MAAM,CAAC,EAAE,MAAM;IAIrB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAuB3B,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,GAAG,EAAE,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IAsB9E,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBzE,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAUtD,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAcjC,KAAK,IAAI,IAAI;CAOd"}
@@ -0,0 +1,4 @@
1
+ export declare const RESOURCES_DIR: string;
2
+ export declare const QUERIES_DIR: string;
3
+ export declare const WASM_DIR: string;
4
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,aAAa,QAAwC,CAAC;AACnE,eAAO,MAAM,WAAW,QAAsC,CAAC;AAC/D,eAAO,MAAM,QAAQ,QAAmC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const getAllFiles: (baseDir: string, excludePatterns?: string[]) => Promise<string[]>;
2
+ //# sourceMappingURL=file-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-utils.d.ts","sourceRoot":"","sources":["../src/file-utils.ts"],"names":[],"mappings":"AAsFA,eAAO,MAAM,WAAW,GAAU,SAAS,MAAM,EAAE,kBAAiB,MAAM,EAAO,KAAG,OAAO,CAAC,MAAM,EAAE,CAsBnG,CAAC"}
@@ -0,0 +1,13 @@
1
+ import Graph from 'graphology';
2
+ import { Tag } from './types';
3
+ export declare class GraphBuilder {
4
+ buildDependencyGraph(tags: Tag[]): {
5
+ graph: Graph;
6
+ defines: Map<string, Set<string>>;
7
+ references: Map<string, string[]>;
8
+ };
9
+ private isSnakeCase;
10
+ private isKebabCase;
11
+ private isCamelCase;
12
+ }
13
+ //# sourceMappingURL=graph-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-builder.d.ts","sourceRoot":"","sources":["../src/graph-builder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,YAAY,CAAC;AAE/B,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAG9B,qBAAa,YAAY;IACvB,oBAAoB,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG;QACjC,KAAK,EAAE,KAAK,CAAC;QACb,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;QAClC,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;KACnC;IAiFD,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,WAAW;CAGpB"}
@@ -0,0 +1,27 @@
1
+ export type { Tag, Symbol, RepoMapOptions, SymbolOptions, GraphEdge, RankedDefinition, ParseResult, LanguageConfig, Logger } from './types';
2
+ export { TreeSitterAnalyzer } from './tree-sitter-analyzer';
3
+ export { TreeSitterParser } from './tree-sitter-parser';
4
+ export { SymbolExtractor } from './symbol-extractor';
5
+ export { GraphBuilder } from './graph-builder';
6
+ export { PageRankCalculator } from './pagerank';
7
+ export { TreeRenderer } from './tree-renderer';
8
+ export { CacheManager } from './cache-manager';
9
+ export { detectLanguage, isLanguageSupported, getSupportedExtensions, getSupportedLanguages } from './language-detector';
10
+ export { downloadWasmForLanguage, getLanguageWasmConfig, getSupportedLanguagesForWasm } from './wasm-downloader';
11
+ export { setLogger, getLogger } from './logger';
12
+ export { getAllFiles } from './file-utils';
13
+ /**
14
+ * Extract symbols from files
15
+ * @param filePaths - Array of file paths to extract symbols from
16
+ * @param options - Symbol extraction options
17
+ * @returns Array of symbols
18
+ */
19
+ export declare function extractSymbols(filePaths: string[], options?: import('./types').SymbolOptions): Promise<import('./types').Symbol[]>;
20
+ /**
21
+ * Generate a repository map
22
+ * @param root - Root directory of the repository
23
+ * @param options - Repository map options
24
+ * @returns Formatted repository map string
25
+ */
26
+ export declare function getRepoMap(root: string, options?: Omit<import('./types').RepoMapOptions, 'root'>): Promise<string>;
27
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,gBAAgB,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAG5I,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG/C,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AACzH,OAAO,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC;AACjH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAe3C;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,OAAO,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,SAAS,EAAE,MAAM,EAAE,CAAC,CAGxI;AAED;;;;;GAKG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,OAAO,SAAS,EAAE,cAAc,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAGxH"}