MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / memberTypesInTree

Function memberTypesInTree

src/graph/branch-guards.ts:2319–2398  ·  view source on GitHub ↗
(root: SyntaxNode, source: string, line: number)

Source from the content-addressed store, hash-verified

2317const CLASS_BODY_TYPES: ReadonlySet<string> = new Set(['class_body', 'declaration_list', 'field_declaration_list']);
2318
2319export function memberTypesInTree(root: SyntaxNode, source: string, line: number): Map<string, string> {
2320 const out = new Map<string, string>();
2321 const row = line - 1;
2322 let node: SyntaxNode | null = innermostAt(root, row, firstNonBlankColumn(source, row));
2323 let cls: SyntaxNode | null = null;
2324 for (let up = 0; node && up < 16; up++, node = node.parent) {
2325 if (CLASS_TYPES.has(node.type)) {
2326 cls = node;
2327 break;
2328 }
2329 }
2330 if (!cls) return out;
2331 const typeText = (n: SyntaxNode | null | undefined): string => (n ? collapse(n.text).replace(/^:\s*/, '').trim() : '');
2332 const put = (name: string | null | undefined, type: string) => {
2333 if (name && type && !out.has(name)) out.set(name, type);
2334 };
2335 const visitParams = (params: SyntaxNode | null) => {
2336 if (!params) return;
2337 for (const p of namedChildren(params)) {
2338 // TS: `private readonly x: T` (a parameter property); Kotlin: `val x: T`; C#/Java: `T x` — a field of the same name may follow.
2339 if (p.type === 'required_parameter' || p.type === 'optional_parameter') {
2340 if (!namedChildren(p).some((c) => c.type === 'accessibility_modifier' || c.type === 'override_modifier') && !/^\s*(?:public|private|protected|readonly)\b/.test(p.text)) continue;
2341 put(p.childForFieldName('pattern')?.text, typeText(p.childForFieldName('type')));
2342 } else if (p.type === 'class_parameter') {
2343 const kids = namedChildren(p);
2344 const name = kids.find((c) => c.type === 'simple_identifier');
2345 const type = kids.find((c) => c.type === 'user_type' || c.type === 'nullable_type');
2346 if (kids.some((c) => c.type === 'binding_pattern_kind')) put(name?.text, typeText(type));
2347 } else if (p.type === 'parameter' || p.type === 'formal_parameter') {
2348 put(p.childForFieldName('name')?.text, typeText(p.childForFieldName('type')));
2349 }
2350 }
2351 };
2352 // Kotlin's primary constructor sits on the class node itself.
2353 for (const c of namedChildren(cls)) if (c.type === 'primary_constructor') visitParams(namedChildren(c).find((n) => n.type === 'class_parameters') ?? c);
2354 const body = namedChildren(cls).find((c) => CLASS_BODY_TYPES.has(c.type)) ?? cls.childForFieldName('body');
2355 if (!body) return out;
2356 for (const m of namedChildren(body)) {
2357 switch (m.type) {
2358 case 'public_field_definition':
2359 case 'field_definition':
2360 put(m.childForFieldName('name')?.text, typeText(m.childForFieldName('type')));
2361 break;
2362 case 'method_definition':
2363 if (m.childForFieldName('name')?.text === 'constructor') visitParams(m.childForFieldName('parameters'));
2364 break;
2365 case 'field_declaration': {
2366 // Java: `type` + `declarator`; C#: a `variable_declaration` inside.
2367 const type = m.childForFieldName('type');
2368 if (type) {
2369 for (const d of namedChildren(m)) if (d.type === 'variable_declarator') put(d.childForFieldName('name')?.text, typeText(type));
2370 } else {
2371 const decl = namedChildren(m).find((c) => c.type === 'variable_declaration');
2372 const t = decl?.childForFieldName('type');
2373 for (const d of decl ? namedChildren(decl) : []) if (d.type === 'variable_declarator') put(d.childForFieldName('name')?.text, typeText(t));
2374 }
2375 break;
2376 }

Callers 2

memberTypesForFileFunction · 0.85
memberTypesInSourceFunction · 0.85

Calls 7

innermostAtFunction · 0.85
firstNonBlankColumnFunction · 0.85
namedChildrenFunction · 0.85
visitParamsFunction · 0.85
typeTextFunction · 0.85
hasMethod · 0.80
putFunction · 0.70

Tested by

no test coverage detected