@snevins/repo-mapper 1.0.3

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,609 @@
1
+ /**
2
+ * Language configuration for tree-sitter parsing.
3
+ * Registry maps language id to config with extensions, grammar, definition types, builtins.
4
+ */
5
+ import { resolve } from "node:path";
6
+ /**
7
+ * Common builtins shared across languages.
8
+ */
9
+ const COMMON_BUILTINS = new Set([
10
+ "if",
11
+ "else",
12
+ "for",
13
+ "while",
14
+ "return",
15
+ "true",
16
+ "false",
17
+ "null",
18
+ ]);
19
+ /**
20
+ * TypeScript/JavaScript shared builtins.
21
+ */
22
+ const TS_JS_BUILTINS = new Set([
23
+ ...COMMON_BUILTINS,
24
+ // Control flow
25
+ "do",
26
+ "switch",
27
+ "case",
28
+ "break",
29
+ "continue",
30
+ "throw",
31
+ "try",
32
+ "catch",
33
+ "finally",
34
+ // Declarations
35
+ "function",
36
+ "class",
37
+ "const",
38
+ "let",
39
+ "var",
40
+ "import",
41
+ "export",
42
+ "default",
43
+ "from",
44
+ "as",
45
+ "type",
46
+ "interface",
47
+ "enum",
48
+ "namespace",
49
+ "module",
50
+ "declare",
51
+ "extends",
52
+ "implements",
53
+ // Literals and primitives
54
+ "undefined",
55
+ "NaN",
56
+ "Infinity",
57
+ // Type keywords
58
+ "string",
59
+ "number",
60
+ "boolean",
61
+ "object",
62
+ "symbol",
63
+ "bigint",
64
+ "void",
65
+ "any",
66
+ "unknown",
67
+ "never",
68
+ // Special
69
+ "this",
70
+ "super",
71
+ "new",
72
+ "typeof",
73
+ "instanceof",
74
+ "in",
75
+ "of",
76
+ "async",
77
+ "await",
78
+ "yield",
79
+ "static",
80
+ "get",
81
+ "set",
82
+ "public",
83
+ "private",
84
+ "protected",
85
+ "readonly",
86
+ "abstract",
87
+ // Common globals
88
+ "console",
89
+ "print",
90
+ "window",
91
+ "document",
92
+ "global",
93
+ "globalThis",
94
+ "process",
95
+ "require",
96
+ "exports",
97
+ "__dirname",
98
+ "__filename",
99
+ "Buffer",
100
+ "Symbol",
101
+ "Promise",
102
+ "Array",
103
+ "Object",
104
+ "String",
105
+ "Number",
106
+ "Boolean",
107
+ "Error",
108
+ "Map",
109
+ "Set",
110
+ "WeakMap",
111
+ "WeakSet",
112
+ "Date",
113
+ "RegExp",
114
+ "JSON",
115
+ "Math",
116
+ ]);
117
+ /**
118
+ * TypeScript/JavaScript definition types.
119
+ */
120
+ const TS_JS_DEFINITION_TYPES = new Set([
121
+ "function_declaration",
122
+ "generator_function_declaration",
123
+ "class_declaration",
124
+ "abstract_class_declaration",
125
+ "interface_declaration",
126
+ "type_alias_declaration",
127
+ "enum_declaration",
128
+ "method_definition",
129
+ ]);
130
+ /**
131
+ * Python builtins.
132
+ */
133
+ const PYTHON_BUILTINS = new Set([
134
+ ...COMMON_BUILTINS,
135
+ // Keywords
136
+ "and",
137
+ "as",
138
+ "assert",
139
+ "async",
140
+ "await",
141
+ "break",
142
+ "class",
143
+ "continue",
144
+ "def",
145
+ "del",
146
+ "elif",
147
+ "except",
148
+ "finally",
149
+ "from",
150
+ "global",
151
+ "import",
152
+ "in",
153
+ "is",
154
+ "lambda",
155
+ "nonlocal",
156
+ "not",
157
+ "or",
158
+ "pass",
159
+ "raise",
160
+ "try",
161
+ "with",
162
+ "yield",
163
+ // Constants
164
+ "None",
165
+ "True",
166
+ "False",
167
+ // Builtins
168
+ "print",
169
+ "len",
170
+ "range",
171
+ "str",
172
+ "int",
173
+ "float",
174
+ "list",
175
+ "dict",
176
+ "set",
177
+ "tuple",
178
+ "bool",
179
+ "type",
180
+ "object",
181
+ "self",
182
+ "cls",
183
+ "__init__",
184
+ "__name__",
185
+ "__main__",
186
+ ]);
187
+ /**
188
+ * Python definition types.
189
+ */
190
+ const PYTHON_DEFINITION_TYPES = new Set([
191
+ "function_definition",
192
+ "class_definition",
193
+ ]);
194
+ /**
195
+ * Go builtins.
196
+ */
197
+ const GO_BUILTINS = new Set([
198
+ ...COMMON_BUILTINS,
199
+ // Keywords
200
+ "break",
201
+ "case",
202
+ "chan",
203
+ "const",
204
+ "continue",
205
+ "default",
206
+ "defer",
207
+ "fallthrough",
208
+ "func",
209
+ "go",
210
+ "goto",
211
+ "import",
212
+ "interface",
213
+ "map",
214
+ "package",
215
+ "range",
216
+ "select",
217
+ "struct",
218
+ "switch",
219
+ "type",
220
+ "var",
221
+ // Builtins
222
+ "append",
223
+ "cap",
224
+ "close",
225
+ "copy",
226
+ "delete",
227
+ "len",
228
+ "make",
229
+ "new",
230
+ "panic",
231
+ "print",
232
+ "println",
233
+ "recover",
234
+ // Types
235
+ "bool",
236
+ "byte",
237
+ "complex64",
238
+ "complex128",
239
+ "error",
240
+ "float32",
241
+ "float64",
242
+ "int",
243
+ "int8",
244
+ "int16",
245
+ "int32",
246
+ "int64",
247
+ "rune",
248
+ "string",
249
+ "uint",
250
+ "uint8",
251
+ "uint16",
252
+ "uint32",
253
+ "uint64",
254
+ "uintptr",
255
+ "nil",
256
+ ]);
257
+ /**
258
+ * Go definition types.
259
+ */
260
+ const GO_DEFINITION_TYPES = new Set([
261
+ "function_declaration",
262
+ "method_declaration",
263
+ "type_declaration",
264
+ "type_spec",
265
+ ]);
266
+ /**
267
+ * Rust builtins.
268
+ */
269
+ const RUST_BUILTINS = new Set([
270
+ ...COMMON_BUILTINS,
271
+ // Keywords
272
+ "as",
273
+ "async",
274
+ "await",
275
+ "break",
276
+ "const",
277
+ "continue",
278
+ "crate",
279
+ "dyn",
280
+ "enum",
281
+ "extern",
282
+ "fn",
283
+ "impl",
284
+ "in",
285
+ "let",
286
+ "loop",
287
+ "match",
288
+ "mod",
289
+ "move",
290
+ "mut",
291
+ "pub",
292
+ "ref",
293
+ "Self",
294
+ "self",
295
+ "static",
296
+ "struct",
297
+ "super",
298
+ "trait",
299
+ "type",
300
+ "unsafe",
301
+ "use",
302
+ "where",
303
+ // Types
304
+ "bool",
305
+ "char",
306
+ "str",
307
+ "i8",
308
+ "i16",
309
+ "i32",
310
+ "i64",
311
+ "i128",
312
+ "isize",
313
+ "u8",
314
+ "u16",
315
+ "u32",
316
+ "u64",
317
+ "u128",
318
+ "usize",
319
+ "f32",
320
+ "f64",
321
+ // Common
322
+ "Some",
323
+ "None",
324
+ "Ok",
325
+ "Err",
326
+ "Option",
327
+ "Result",
328
+ "Vec",
329
+ "String",
330
+ "Box",
331
+ "Rc",
332
+ "Arc",
333
+ "println",
334
+ "print",
335
+ "format",
336
+ "panic",
337
+ ]);
338
+ /**
339
+ * Rust definition types.
340
+ */
341
+ const RUST_DEFINITION_TYPES = new Set([
342
+ "function_item",
343
+ "struct_item",
344
+ "enum_item",
345
+ "trait_item",
346
+ "impl_item",
347
+ "type_item",
348
+ "mod_item",
349
+ ]);
350
+ /**
351
+ * Solidity builtins.
352
+ */
353
+ const SOLIDITY_BUILTINS = new Set([
354
+ ...COMMON_BUILTINS,
355
+ // Keywords
356
+ "abstract",
357
+ "after",
358
+ "alias",
359
+ "apply",
360
+ "auto",
361
+ "case",
362
+ "catch",
363
+ "constant",
364
+ "copyof",
365
+ "default",
366
+ "define",
367
+ "emit",
368
+ "error",
369
+ "event",
370
+ "external",
371
+ "fallback",
372
+ "final",
373
+ "function",
374
+ "immutable",
375
+ "implements",
376
+ "import",
377
+ "in",
378
+ "indexed",
379
+ "inline",
380
+ "internal",
381
+ "is",
382
+ "let",
383
+ "macro",
384
+ "match",
385
+ "memory",
386
+ "modifier",
387
+ "mutable",
388
+ "new",
389
+ "of",
390
+ "override",
391
+ "partial",
392
+ "payable",
393
+ "pragma",
394
+ "private",
395
+ "promise",
396
+ "public",
397
+ "pure",
398
+ "receive",
399
+ "reference",
400
+ "relocatable",
401
+ "revert",
402
+ "sealed",
403
+ "sizeof",
404
+ "static",
405
+ "storage",
406
+ "struct",
407
+ "super",
408
+ "supports",
409
+ "switch",
410
+ "this",
411
+ "throw",
412
+ "try",
413
+ "typedef",
414
+ "typeof",
415
+ "unchecked",
416
+ "using",
417
+ "var",
418
+ "view",
419
+ "virtual",
420
+ // Types
421
+ "address",
422
+ "bool",
423
+ "bytes",
424
+ "bytes1",
425
+ "bytes32",
426
+ "int",
427
+ "int8",
428
+ "int256",
429
+ "mapping",
430
+ "string",
431
+ "uint",
432
+ "uint8",
433
+ "uint256",
434
+ // Globals
435
+ "msg",
436
+ "block",
437
+ "tx",
438
+ "abi",
439
+ "require",
440
+ "assert",
441
+ "keccak256",
442
+ "sha256",
443
+ "ecrecover",
444
+ ]);
445
+ /**
446
+ * Solidity definition types.
447
+ */
448
+ const SOLIDITY_DEFINITION_TYPES = new Set([
449
+ "function_definition",
450
+ "contract_declaration",
451
+ "library_declaration",
452
+ "interface_declaration",
453
+ "struct_declaration",
454
+ "enum_declaration",
455
+ "event_definition",
456
+ "error_declaration",
457
+ "modifier_definition",
458
+ ]);
459
+ /**
460
+ * Language registry - keyed by language id.
461
+ */
462
+ export const LANGUAGE_REGISTRY = {
463
+ typescript: {
464
+ extensions: [".ts", ".mts", ".cts"],
465
+ grammarName: "typescript",
466
+ wasmPath: (nm) => resolve(nm, "tree-sitter-typescript", "tree-sitter-typescript.wasm"),
467
+ definitionTypes: TS_JS_DEFINITION_TYPES,
468
+ builtins: TS_JS_BUILTINS,
469
+ fallbackPatterns: [
470
+ /^export\s+(?:async\s+)?function\s+(\w+)/m,
471
+ /^export\s+class\s+(\w+)/m,
472
+ /^export\s+interface\s+(\w+)/m,
473
+ /^export\s+type\s+(\w+)/m,
474
+ /^export\s+enum\s+(\w+)/m,
475
+ /^export\s+const\s+(\w+)/m,
476
+ ],
477
+ },
478
+ tsx: {
479
+ extensions: [".tsx"],
480
+ grammarName: "tsx",
481
+ wasmPath: (nm) => resolve(nm, "tree-sitter-typescript", "tree-sitter-tsx.wasm"),
482
+ definitionTypes: TS_JS_DEFINITION_TYPES,
483
+ builtins: TS_JS_BUILTINS,
484
+ fallbackPatterns: [
485
+ /^export\s+(?:async\s+)?function\s+(\w+)/m,
486
+ /^export\s+class\s+(\w+)/m,
487
+ /^export\s+const\s+(\w+)/m,
488
+ ],
489
+ },
490
+ javascript: {
491
+ extensions: [".js", ".mjs", ".cjs"],
492
+ grammarName: "javascript",
493
+ wasmPath: (nm) => resolve(nm, "tree-sitter-javascript", "tree-sitter-javascript.wasm"),
494
+ definitionTypes: TS_JS_DEFINITION_TYPES,
495
+ builtins: TS_JS_BUILTINS,
496
+ fallbackPatterns: [
497
+ /^export\s+(?:async\s+)?function\s+(\w+)/m,
498
+ /^export\s+class\s+(\w+)/m,
499
+ /^export\s+const\s+(\w+)/m,
500
+ ],
501
+ },
502
+ jsx: {
503
+ extensions: [".jsx"],
504
+ grammarName: "javascript", // Reuse cached JavaScript grammar (same WASM file)
505
+ wasmPath: (nm) => resolve(nm, "tree-sitter-javascript", "tree-sitter-javascript.wasm"),
506
+ definitionTypes: TS_JS_DEFINITION_TYPES,
507
+ builtins: TS_JS_BUILTINS,
508
+ fallbackPatterns: [
509
+ /^export\s+(?:async\s+)?function\s+(\w+)/m,
510
+ /^export\s+class\s+(\w+)/m,
511
+ /^export\s+const\s+(\w+)/m,
512
+ ],
513
+ },
514
+ python: {
515
+ extensions: [".py", ".pyw"],
516
+ grammarName: "python",
517
+ wasmPath: (nm) => resolve(nm, "tree-sitter-python", "tree-sitter-python.wasm"),
518
+ definitionTypes: PYTHON_DEFINITION_TYPES,
519
+ builtins: PYTHON_BUILTINS,
520
+ fallbackPatterns: [
521
+ /^(?:async\s+)?def\s+(\w+)/m,
522
+ /^class\s+(\w+)/m,
523
+ ],
524
+ },
525
+ go: {
526
+ extensions: [".go"],
527
+ grammarName: "go",
528
+ wasmPath: (nm) => resolve(nm, "tree-sitter-go", "tree-sitter-go.wasm"),
529
+ definitionTypes: GO_DEFINITION_TYPES,
530
+ builtins: GO_BUILTINS,
531
+ fallbackPatterns: [
532
+ /^func\s+(\w+)/m,
533
+ /^func\s+\([^)]+\)\s+(\w+)/m, // Method receiver
534
+ /^type\s+(\w+)\s+struct/m,
535
+ /^type\s+(\w+)\s+interface/m,
536
+ ],
537
+ },
538
+ rust: {
539
+ extensions: [".rs"],
540
+ grammarName: "rust",
541
+ wasmPath: (nm) => resolve(nm, "tree-sitter-rust", "tree-sitter-rust.wasm"),
542
+ definitionTypes: RUST_DEFINITION_TYPES,
543
+ builtins: RUST_BUILTINS,
544
+ fallbackPatterns: [
545
+ /^(?:pub\s+)?fn\s+(\w+)/m,
546
+ /^(?:pub\s+)?struct\s+(\w+)/m,
547
+ /^(?:pub\s+)?enum\s+(\w+)/m,
548
+ /^(?:pub\s+)?trait\s+(\w+)/m,
549
+ /^impl(?:<[^>]+>)?\s+(\w+)/m,
550
+ ],
551
+ },
552
+ solidity: {
553
+ extensions: [".sol"],
554
+ grammarName: "solidity",
555
+ wasmPath: (nm) => resolve(nm, "tree-sitter-solidity", "tree-sitter-solidity.wasm"),
556
+ definitionTypes: SOLIDITY_DEFINITION_TYPES,
557
+ builtins: SOLIDITY_BUILTINS,
558
+ fallbackPatterns: [
559
+ /^(?:abstract\s+)?contract\s+(\w+)/m,
560
+ /^library\s+(\w+)/m,
561
+ /^interface\s+(\w+)/m,
562
+ /^\s*function\s+(\w+)/m,
563
+ /^struct\s+(\w+)/m,
564
+ /^event\s+(\w+)/m,
565
+ /^error\s+(\w+)/m,
566
+ /^modifier\s+(\w+)/m,
567
+ ],
568
+ },
569
+ };
570
+ /**
571
+ * Build extension -> language lookup map.
572
+ */
573
+ function buildExtensionMap() {
574
+ const map = new Map();
575
+ for (const [langId, config] of Object.entries(LANGUAGE_REGISTRY)) {
576
+ for (const ext of config.extensions) {
577
+ map.set(ext, langId);
578
+ }
579
+ }
580
+ return map;
581
+ }
582
+ const EXTENSION_MAP = buildExtensionMap();
583
+ /**
584
+ * Get language config for a file extension.
585
+ * Returns undefined for unsupported extensions.
586
+ */
587
+ export function getLanguageForExtension(ext) {
588
+ const langId = EXTENSION_MAP.get(ext);
589
+ if (!langId)
590
+ return undefined;
591
+ return LANGUAGE_REGISTRY[langId];
592
+ }
593
+ /**
594
+ * Check if extension is supported for tree-sitter parsing.
595
+ */
596
+ export function isTreeSitterSupported(ext) {
597
+ return EXTENSION_MAP.has(ext);
598
+ }
599
+ /**
600
+ * Filter out builtin/keyword identifiers for a language.
601
+ * Also filters single-character identifiers as they're typically loop vars.
602
+ * If no config provided, uses TS/JS builtins as default.
603
+ */
604
+ export function isBuiltin(name, config) {
605
+ if (name.length <= 1)
606
+ return true;
607
+ const builtins = config?.builtins ?? TS_JS_BUILTINS;
608
+ return builtins.has(name);
609
+ }
@@ -0,0 +1,19 @@
1
+ import type { RankedDefinition, FileGraph } from "./types.js";
2
+ /**
3
+ * Format output per spec §3.5.
4
+ * Files sorted by PageRank descending.
5
+ * Definitions within file sorted by line ascending.
6
+ * Line numbers right-aligned to max width.
7
+ */
8
+ export declare function formatOutput(defs: readonly RankedDefinition[], graph: FileGraph, fileRanks: ReadonlyMap<string, number>, focusFiles?: readonly string[]): string;
9
+ /**
10
+ * Fit ranked definitions to token budget using binary search.
11
+ * Returns the largest output that fits within the budget.
12
+ *
13
+ * Algorithm from spec §8.2:
14
+ * 1. Binary search over number of definitions
15
+ * 2. Format first `mid` definitions
16
+ * 3. If tokens ≤ budget, try more; else try fewer
17
+ * 4. Return best fit
18
+ */
19
+ export declare function fitToTokenBudget(defs: readonly RankedDefinition[], graph: FileGraph, fileRanks: ReadonlyMap<string, number>, tokenBudget: number, focusFiles?: readonly string[]): string;