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

Function returnsInTree

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

Source from the content-addressed store, hash-verified

2121}
2122
2123export function returnsInTree(root: SyntaxNode, line: number): ReturnSite[] {
2124 const row = line - 1;
2125 let node: SyntaxNode | null = innermostAt(root, row, firstNonBlankColumn(root.text, row));
2126 let definition: SyntaxNode | null = null;
2127 for (let up = 0; node && up < 12; up++, node = node.parent) {
2128 if (DEFINITION_TYPES.has(node.type)) {
2129 definition = node;
2130 break;
2131 }
2132 }
2133 if (!definition) return [];
2134 const out: ReturnSite[] = [];
2135 const visit = (n: SyntaxNode): void => {
2136 for (const c of namedChildren(n)) {
2137 if (NESTED_SCOPE_TYPES.has(c.type)) continue;
2138 const isReturn = c.type === 'return_statement' || (c.type === 'jump_expression' && /^return\b/.test(c.text));
2139 if (isReturn) {
2140 const expr = namedChildren(c)[0];
2141 if (expr) out.push({ line: c.startPosition.row + 1, column: c.startPosition.column, expression: collapse(expr.text) });
2142 continue;
2143 }
2144 visit(c);
2145 }
2146 };
2147 visit(definition);
2148 // A bare name: the string a same-class constant holds (`static final String X = "…"`).
2149 let cls: SyntaxNode | null = definition.parent;
2150 for (let up = 0; cls && up < 6 && !CLASS_TYPES.has(cls.type); up++) cls = cls.parent;
2151 if (cls) {
2152 const strings = new Map<string, string>();
2153 const collect = (n: SyntaxNode): void => {
2154 for (const c of namedChildren(n)) {
2155 if (c.type === 'variable_declarator') {
2156 const name = c.childForFieldName('name')?.text;
2157 const value = c.childForFieldName('value');
2158 if (name && value?.type === 'string_literal') strings.set(name, value.text.replace(/^"|"$/g, ''));
2159 } else if (c.type === 'field_declaration' || c.type === 'class_body') collect(c);
2160 }
2161 };
2162 collect(cls);
2163 for (const r of out) {
2164 if (/^[A-Za-z_$][\w$]*$/.test(r.expression) && strings.has(r.expression)) r.value = strings.get(r.expression)!;
2165 }
2166 }
2167 return out;
2168}
2169
2170export interface DefinitionDecorators {
2171 own: string[];

Callers 2

returnsForFileFunction · 0.85
returnsInSourceFunction · 0.85

Calls 6

innermostAtFunction · 0.85
firstNonBlankColumnFunction · 0.85
hasMethod · 0.80
visitFunction · 0.70
collectFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected