Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 | 1x 1x 1x 1x 47x 47x 47x 47x 47x 47x 55x 55x 55x 55x 55x 55x 47x 47x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 144x 13x 13x 55x 13x 13x 13x 13x 13x 13x 13x 13x 12x 13x 1x 3x 3x 3x 3x 1x 13x 13x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 930x 930x 62x 62x 62x 930x 5x 5x 5x 5x 930x 2x 2x 930x 23x 23x 23x 23x 23x 23x 23x 930x 861x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 66x 66x 67x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 62x 5x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 67x 67x 67x 67x 55x 55x 55x 55x 110x 110x 55x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 1x 1x 1x 7x 1x 1x 1x 1x 67x 67x 67x 54x 54x 69x 69x 69x 69x 69x 69x 69x 1x 69x 69x 69x 69x 69x 7x 7x 7x 15x 8x 8x 15x 7x 69x 69x 124x 124x 124x 124x 1x 124x 124x 124x 124x 5405x 5405x 2x 2x 2x 5405x 4x 4x 4x 5399x 5405x 28x 28x 28x 28x 28x 5405x 2x 2x 2x 2x 2x 5405x 4x 4x 4x 4x 4x 5365x 5365x 5365x 124x 124x 2x 2x 2x 2x 2x 4x 4x 4x 67x 64x 64x 1x 28x 28x 28x 28x 28x 28x 28x 448x 2x 2x 2x 2x 448x 28x 28x 28x 28x 418x 418x 418x 28x 28x 2x 2x 2x 2x 2x 2x 2x 50x 2x 2x 2x 2x 48x 48x 48x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 10x 10x 2x 2x 2x 2x 2x 10x 2x 2x 2x 2x 2x 10x 4x 4x 4x 4x 2x 2x 2x 4x 4x 124x 124x 124x 124x 124x 124x 124x 124x 124x 123x 124x 1x 55x 55x 55x 55x 55x | /**
* Go walkProject — emit FunctionOccurrences + CallSiteRecords.
*
* Identifies the callable shapes:
*
* - `function_declaration` → 'function-declaration'
* - `method_declaration` (has receiver) → 'method'
* - `enclosingClass` = receiver type name (e.g. `Foo` in
* `func (f *Foo) bar() {}`). Pointer `*Foo` and value `Foo`
* receivers both yield `Foo` — the dereference is stripped.
* - `func_literal` → 'arrow'
* - one synthetic `<module-init>` per file owning top-level non-fn
* items (`package`, `import`, `var`, `const`, `type`).
*
* Body hashing: sha256 of normalized body text. Normalization:
* 1. Strip line comments (`// …` to end-of-line).
* 2. Strip block comments (`/* … *\/`). Go does NOT support nested
* block comments, unlike Rust.
* 3. Preserve string literals: both interpreted (`"…"`) and raw
* (backtick `…`) forms. Their content is part of behavior.
* 4. Preserve rune literals (`'x'`, `'\n'`).
* 5. Collapse whitespace.
*
* Call-site records:
* - `call_expression` — every Go function/method/built-in call.
* The resolver decodes:
* - `foo(args)` — identifier target
* - `obj.Method(args)` — selector_expression's `field`
* - `pkg.Func(args)` — same selector_expression shape
* - `Type{}.method(args)` — selector_expression on composite_literal
* - 'creation' edges — for each `func_literal` nested inside a parent
* function/method/module-init, emit a creation edge so reachability
* flows through closures even when dispatch is unresolvable.
* Mirror of lang-typescript's `isInlineCallable`.
*
* Test detection:
* - File-level: filename ends with `_test.go`. Go's toolchain enforces
* this convention, so the predicate is exact.
* - No function-level detection — `func TestXxx(t *testing.T)` only
* compiles into a test when its file is `_test.go`.
*/
import { createHash } from 'node:crypto';
import { relative, sep } from 'node:path';
import type { GoParsedFile, GoParsedProject } from './parse.js';
import type {
CallSiteRecord,
DependencySiteRecord,
FunctionOccurrence,
ParseError,
WalkInput,
WalkOutput,
} from '@opensip-tools/graph';
import type Parser from 'tree-sitter';
const TEST_FILE_NAME_RE = /(?:^|\/)[^/]+_test\.go$/;
const GENERATED_PATH_RE = /\bvendor\/|\.pb\.go$|_generated\.go$|\.gen\.go$|zz_generated_/;
export function walkProject(input: WalkInput<GoParsedProject>): WalkOutput {
const occurrences: Record<string, FunctionOccurrence[]> = Object.create(null) as Record<
string,
FunctionOccurrence[]
>;
const callSites: CallSiteRecord[] = [];
const dependencySites: DependencySiteRecord[] = [];
const parseErrors: ParseError[] = [];
const sortedPaths = [...input.files].filter((p) => input.project.files.has(p)).sort();
for (const path of sortedPaths) {
const file = input.project.files.get(path);
if (!file) continue;
try {
walkFile(path, file, input.projectDirAbs, occurrences, callSites, dependencySites);
} catch (error) {
parseErrors.push({
filePath: relative(input.projectDirAbs, path),
message: error instanceof Error ? error.message : String(error),
});
}
}
return { occurrences, callSites, dependencySites, parseErrors };
}
function walkFile(
absPath: string,
file: GoParsedFile,
projectDirAbs: string,
out: Record<string, FunctionOccurrence[]>,
callSites: CallSiteRecord[],
dependencySites: DependencySiteRecord[],
): void {
const filePathProjectRel = relative(projectDirAbs, absPath).split(sep).join('/');
const inTestFile = isTestFile(filePathProjectRel);
const definedInGenerated = isGeneratedFile(filePathProjectRel);
const packageName = extractPackageName(file);
const moduleInit = synthesizeModuleInit(
file,
filePathProjectRel,
packageName,
inTestFile,
definedInGenerated,
);
record(out, moduleInit);
// Phase 4 (DEC-498): walk top-level imports as dependency sites. Owner
// is the file's synthesized module-init occurrence.
collectDependencySites(file, moduleInit.bodyHash, dependencySites);
const ctx: WalkCtx = {
file,
filePathProjectRel,
packageName,
fileInTestFile: inTestFile,
definedInGenerated,
out,
callSites,
};
const initialFrame: Frame = { ownerHash: moduleInit.bodyHash };
for (const child of file.tree.rootNode.children) visit(child, initialFrame, ctx);
}
/**
* Walk a Go file's top-level `import_declaration` nodes; emit one
* `DependencySiteRecord` per `import_spec`, regardless of whether the
* declaration is single (`import "fmt"`) or grouped (`import ( … )`).
*
* Phase 4 of opensip's substrate consolidation (DEC-498). The emitted
* `specifier` is the raw import path WITHOUT the surrounding quotes
* (e.g. `'fmt'`, `'github.com/user/repo/pkg/sub'`).
*
* Aliased imports (`alias "path"`), blank imports (`_ "path"`), and dot
* imports (`. "path"`) all emit one dep site keyed by the import path;
* the alias / blank / dot prefix doesn't change the dependency target.
*
* Out of scope at v1:
* - Conditional / nested imports inside function bodies (Go doesn't
* permit these — imports are always file-top-level).
* - `go.work`-mediated multi-module workspaces (a follow-up).
*/
function collectDependencySites(
file: GoParsedFile,
moduleInitHash: string,
out: DependencySiteRecord[],
): void {
for (const stmt of file.tree.rootNode.namedChildren) {
if (stmt.type !== 'import_declaration') continue;
collectFromImportDeclaration(stmt, file, moduleInitHash, out);
}
}
function collectFromImportDeclaration(
decl: Parser.SyntaxNode,
file: GoParsedFile,
moduleInitHash: string,
out: DependencySiteRecord[],
): void {
for (const child of decl.namedChildren) {
if (child.type === 'import_spec') {
pushImportSpec(child, file, moduleInitHash, out);
} else if (child.type === 'import_spec_list') {
for (const spec of child.namedChildren) {
if (spec.type === 'import_spec') {
pushImportSpec(spec, file, moduleInitHash, out);
}
}
}
}
}
function pushImportSpec(
spec: Parser.SyntaxNode,
file: GoParsedFile,
ownerHash: string,
out: DependencySiteRecord[],
): void {
// The `path` field is an interpreted_string_literal. Its `text` is the
// quoted form (`"fmt"`); strip outer quotes to get the raw specifier.
const pathNode = spec.childForFieldName('path') ?? findInterpretedString(spec);
if (!pathNode) return;
const specifier = unquoteGoStringLiteral(pathNode.text);
if (specifier === null) return;
out.push({
nodeRef: spec,
sourceFileRef: file,
ownerHash,
specifier,
line: spec.startPosition.row + 1,
column: spec.startPosition.column,
});
}
function findInterpretedString(node: Parser.SyntaxNode): Parser.SyntaxNode | null {
/* v8 ignore start */
for (const child of node.namedChildren) {
if (child.type === 'interpreted_string_literal') return child;
}
return null;
/* v8 ignore stop */
}
function unquoteGoStringLiteral(text: string): string | null {
// Go import paths are always interpreted strings — wrapped in `"…"`
// with no escape sequences relevant to module paths. (Raw `\`…\``
// strings are NOT valid in import declarations per the Go spec.)
if (text.length < 2) return null;
if (text.startsWith('"') && text.endsWith('"')) return text.slice(1, -1);
/* v8 ignore next */
return null;
}
interface Frame {
readonly ownerHash: string;
}
interface WalkCtx {
readonly file: GoParsedFile;
readonly filePathProjectRel: string;
readonly packageName: string;
readonly fileInTestFile: boolean;
readonly definedInGenerated: boolean;
readonly out: Record<string, FunctionOccurrence[]>;
readonly callSites: CallSiteRecord[];
}
function visit(node: Parser.SyntaxNode, frame: Frame, ctx: WalkCtx): void {
if (node.type === 'function_declaration') {
visitFunction(node, frame, ctx, null);
return;
}
if (node.type === 'method_declaration') {
const receiverType = extractReceiverType(node);
visitFunction(node, frame, ctx, receiverType);
return;
}
if (node.type === 'func_literal' && visitClosure(node, frame, ctx)) {
return;
}
if (node.type === 'call_expression') {
ctx.callSites.push({
nodeRef: node,
sourceFileRef: ctx.file,
ownerHash: frame.ownerHash,
kind: 'call',
});
}
for (const child of node.children) visit(child, frame, ctx);
}
function visitFunction(
node: Parser.SyntaxNode,
frame: Frame,
ctx: WalkCtx,
receiverType: string | null,
): void {
const occ = buildFunctionOccurrence(node, ctx, receiverType);
if (!occ) return;
record(ctx.out, occ);
const childFrame: Frame = { ownerHash: occ.bodyHash };
const body = node.childForFieldName('body');
if (body) {
for (const child of body.children) visit(child, childFrame, ctx);
}
}
function visitClosure(node: Parser.SyntaxNode, frame: Frame, ctx: WalkCtx): boolean {
const occ = buildClosureOccurrence(node, ctx);
if (!occ) return false;
record(ctx.out, occ);
if (frame.ownerHash !== occ.bodyHash) {
ctx.callSites.push({
nodeRef: node,
sourceFileRef: ctx.file,
ownerHash: frame.ownerHash,
kind: 'creation',
childHash: occ.bodyHash,
});
}
const body = node.childForFieldName('body');
if (body) {
visit(body, { ownerHash: occ.bodyHash }, ctx);
}
return true;
}
function buildFunctionOccurrence(
node: Parser.SyntaxNode,
ctx: WalkCtx,
receiverType: string | null,
): FunctionOccurrence | null {
const name = nameOf(node) ?? '<anon-fn>';
const digest = digestGoBody(ctx.file.source.slice(node.startIndex, node.endIndex));
const kind: FunctionOccurrence['kind'] = receiverType === null ? 'function-declaration' : 'method';
const qualifiedBase = `${ctx.packageName}/${ctx.filePathProjectRel}`.replace(/\.go$/, '');
const qualifiedName = receiverType === null
? `${qualifiedBase}.${name}`
: `${qualifiedBase}.(${receiverType}).${name}`;
return {
bodyHash: digest.hash,
bodySize: digest.size,
simpleName: name,
qualifiedName,
filePath: ctx.filePathProjectRel,
line: node.startPosition.row + 1,
column: node.startPosition.column,
endLine: node.endPosition.row + 1,
kind,
params: extractParams(node),
returnType: null,
enclosingClass: receiverType,
decorators: [],
visibility: classifyVisibility(name),
inTestFile: ctx.fileInTestFile,
definedInGenerated: ctx.definedInGenerated,
calls: [],
};
}
function buildClosureOccurrence(
node: Parser.SyntaxNode,
ctx: WalkCtx,
): FunctionOccurrence | null {
const digest = digestGoBody(ctx.file.source.slice(node.startIndex, node.endIndex));
const startLine = node.startPosition.row + 1;
const startCol = node.startPosition.column;
const simpleName = `<arrow:${ctx.filePathProjectRel}:${String(startLine)}:${String(startCol)}>`;
const qualifiedBase = `${ctx.packageName}/${ctx.filePathProjectRel}`.replace(/\.go$/, '');
return {
bodyHash: digest.hash,
bodySize: digest.size,
simpleName,
qualifiedName: `${qualifiedBase}.<closure:${String(startLine)}:${String(startCol)}>`,
filePath: ctx.filePathProjectRel,
line: startLine,
column: startCol,
endLine: node.endPosition.row + 1,
kind: 'arrow',
params: extractClosureParams(node),
returnType: null,
enclosingClass: null,
decorators: [],
visibility: 'private',
inTestFile: ctx.fileInTestFile,
definedInGenerated: ctx.definedInGenerated,
calls: [],
};
}
function synthesizeModuleInit(
file: GoParsedFile,
filePathProjectRel: string,
packageName: string,
inTestFile: boolean,
definedInGenerated: boolean,
): FunctionOccurrence {
const root = file.tree.rootNode;
const topLevelText = root.children
.map((c) => file.source.slice(c.startIndex, c.endIndex))
.join('\n');
const digest = digestSyntheticBody(`${filePathProjectRel}\n${topLevelText}`);
const simpleName = `<module-init:${filePathProjectRel}>`;
const qualifiedBase = `${packageName}/${filePathProjectRel}`.replace(/\.go$/, '');
return {
bodyHash: digest.hash,
bodySize: digest.size,
simpleName,
qualifiedName: `${qualifiedBase}.<module-init>`,
filePath: filePathProjectRel,
line: 1,
column: 0,
endLine: root.endPosition.row + 1,
kind: 'module-init',
params: [],
returnType: null,
enclosingClass: null,
decorators: [],
visibility: 'module-local',
inTestFile,
definedInGenerated,
calls: [],
};
}
// ── helpers ───────────────────────────────────────────────────────
function nameOf(node: Parser.SyntaxNode): string | null {
const name = node.childForFieldName('name');
return name ? name.text : null;
}
function extractPackageName(file: GoParsedFile): string {
for (const child of file.tree.rootNode.children) {
if (child.type === 'package_clause') {
// package_clause: `package` keyword followed by identifier
for (const c of child.children) {
if (c.type === 'package_identifier' || c.type === 'identifier') return c.text;
}
}
}
/* v8 ignore next */
return 'main';
}
function extractReceiverType(node: Parser.SyntaxNode): string | null {
// method_declaration has a `receiver` field of type parameter_list
// containing one parameter_declaration. The declaration's `type`
// is either pointer_type (e.g. `*Foo`) or type_identifier (`Foo`).
const receiver = node.childForFieldName('receiver');
if (!receiver) return null;
for (const param of receiver.namedChildren) {
if (param.type !== 'parameter_declaration') continue;
const ty = param.childForFieldName('type') ?? param.namedChild(param.namedChildCount - 1);
if (!ty) continue;
return decodeReceiverTypeNode(ty);
}
/* v8 ignore next */
return null;
}
function decodeReceiverTypeNode(node: Parser.SyntaxNode): string | null {
if (node.type === 'pointer_type') {
// *Foo or *Foo[T] — descend through the pointer to the named type.
const inner = node.namedChild(0);
return inner ? decodeReceiverTypeNode(inner) : null;
}
if (node.type === 'type_identifier') return node.text;
if (node.type === 'generic_type') {
// Foo[T] — the trailing name is the type. tree-sitter-go usually
// exposes the base via a named child.
const inner = node.childForFieldName('type') ?? node.namedChild(0);
return inner ? decodeReceiverTypeNode(inner) : null;
}
/* v8 ignore next */
return node.text;
}
function classifyVisibility(name: string): FunctionOccurrence['visibility'] {
// Go visibility is determined by the first character's case. The
// primary check is "is the first character an uppercase ASCII letter".
// Unicode-case rules also count per the Go spec, but ASCII covers the
// overwhelming majority of real-world Go.
const first = name.charAt(0);
if (first >= 'A' && first <= 'Z') return 'exported';
return 'module-local';
}
function extractParams(
node: Parser.SyntaxNode,
): readonly { name: string; optional: boolean; rest: boolean }[] {
const params = node.childForFieldName('parameters');
if (!params) return [];
return collectParamEntries(params);
}
// Closures share the same parameters field shape as function_declaration.
const extractClosureParams = extractParams;
function collectParamEntries(
params: Parser.SyntaxNode,
): readonly { name: string; optional: boolean; rest: boolean }[] {
const out: { name: string; optional: boolean; rest: boolean }[] = [];
for (const child of params.namedChildren) {
if (child.type !== 'parameter_declaration' && child.type !== 'variadic_parameter_declaration') {
continue;
}
const isRest = child.type === 'variadic_parameter_declaration';
// A parameter_declaration may bind multiple names to one type:
// `func f(a, b int)` produces a single declaration node with two
// `name` children. Iterate the named identifiers.
for (const inner of child.namedChildren) {
if (inner.type === 'identifier') {
out.push({ name: inner.text, optional: false, rest: isRest });
}
}
}
return out;
}
// ── body normalization ────────────────────────────────────────────
interface BodyDigest {
readonly hash: string;
readonly size: number;
}
function digestGoBody(text: string): BodyDigest {
const normalized = normalizeWhitespace(stripGoComments(text));
return { hash: sha256(normalized), size: normalized.length };
}
// Synthetic bodies (module-init) use the same normalization as real
// bodies; alias for self-documenting call sites.
const digestSyntheticBody = digestGoBody;
/**
* Strip Go line comments (// to end of line) and block comments
* (slash-star ... star-slash). Go does NOT support nested block
* comments. Preserve string literals (both interpreted `"…"` and raw
* backtick `…`); preserve rune literals.
*/
function stripGoComments(text: string): string {
let out = '';
let i = 0;
while (i < text.length) {
const next2 = text.slice(i, i + 2);
if (next2 === '//') {
i = skipToEndOfLine(text, i);
continue;
}
if (next2 === '/*') {
i = skipBlockComment(text, i + 2);
continue;
}
const c = text[i];
if (c === '"') {
const block = consumeInterpretedString(text, i);
out += block.text;
i = block.index;
continue;
}
if (c === '`') {
const block = consumeRawString(text, i);
out += block.text;
i = block.index;
continue;
}
if (c === "'") {
const block = consumeRuneLiteral(text, i);
out += block.text;
i = block.index;
continue;
}
out += c;
i++;
}
return out;
}
function skipToEndOfLine(text: string, start: number): number {
let i = start;
while (i < text.length && text[i] !== '\n') i++;
return i;
}
function skipBlockComment(text: string, start: number): number {
// Go block comments do NOT nest. Scan to the first `*/`.
let i = start;
while (i < text.length) {
if (text.slice(i, i + 2) === '*/') return i + 2;
i++;
}
/* v8 ignore next */
return i;
}
function consumeInterpretedString(
text: string,
start: number,
): { readonly text: string; readonly index: number } {
let i = start + 1;
let buf = '"';
while (i < text.length) {
if (text[i] === '\\' && i + 1 < text.length) {
buf += text.slice(i, i + 2);
i += 2;
continue;
}
if (text[i] === '"') {
buf += '"';
i++;
break;
}
/* v8 ignore next */
if (text[i] === '\n') break;
buf += text[i];
i++;
}
return { text: buf, index: i };
}
function consumeRawString(
text: string,
start: number,
): { readonly text: string; readonly index: number } {
// Raw strings span across newlines and have no escape sequences.
let i = start + 1;
let buf = '`';
while (i < text.length) {
if (text[i] === '`') {
buf += '`';
i++;
break;
}
buf += text[i];
i++;
}
return { text: buf, index: i };
}
function consumeRuneLiteral(
text: string,
start: number,
): { readonly text: string; readonly index: number } {
// A rune literal is a `'`, then either a single char or an escape
// sequence (`\n`, `A`, `\xff`), then a closing `'`. Walk to the
// next unescaped `'` within a small window.
let i = start + 1;
let buf = "'";
let escape = false;
while (i < text.length) {
const c = text[i];
if (escape) {
buf += c;
escape = false;
i++;
continue;
}
if (c === '\\') {
buf += c;
escape = true;
i++;
continue;
}
if (c === "'") {
buf += c;
i++;
break;
}
/* v8 ignore next */
if (c === '\n') break;
buf += c;
i++;
}
return { text: buf, index: i };
}
function normalizeWhitespace(s: string): string {
return s.replaceAll(/\s+/g, ' ').trim();
}
function sha256(s: string): string {
return createHash('sha256').update(s, 'utf8').digest('hex');
}
// ── output helpers ────────────────────────────────────────────────
function record(out: Record<string, FunctionOccurrence[]>, occ: FunctionOccurrence): void {
const list = out[occ.simpleName];
if (list) list.push(occ);
else out[occ.simpleName] = [occ];
}
export function isTestFile(rel: string): boolean {
return TEST_FILE_NAME_RE.test(rel);
}
function isGeneratedFile(rel: string): boolean {
return GENERATED_PATH_RE.test(rel);
}
|