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

Function extract

src/resolution/frameworks/csharp.ts:141–321  ·  view source on GitHub ↗
(filePath, content)

Source from the content-addressed store, hash-verified

139 },
140
141 extract(filePath, content) {
142 if (!filePath.endsWith('.cs')) return { nodes: [], references: [] };
143 const nodes: Node[] = [];
144 const references: UnresolvedRef[] = [];
145 const now = Date.now();
146 const safe = stripCommentsForRegex(content, 'csharp');
147
148 // Class-level [Route("api/[controller]")] prefix — joined onto each action.
149 let classPrefix = '';
150 const cls = /\[Route\s*\(\s*"([^"]+)"[^)]*\)\]\s*(?:\[[^\]]*\]\s*)*(?:public\s+|sealed\s+|abstract\s+|partial\s+)*class\b/.exec(safe);
151 if (cls) classPrefix = cls[1]!;
152
153 // [HttpGet], [HttpGet("path")], [HttpPost("path", Name="x")] — BARE or with a
154 // path. (The old regex required a string, so bare attributes — with the route
155 // on the class [Route] — were missed; eShopOnWeb was 24 bare / 2 string.)
156 const attrRegex = /\[(HttpGet|HttpPost|HttpPut|HttpPatch|HttpDelete)(?:\s*\(\s*"([^"]+)"[^)]*\))?\s*\]/g;
157 let match: RegExpExecArray | null;
158 while ((match = attrRegex.exec(safe)) !== null) {
159 const verb = match[1]!;
160 const method = verb.replace(/^Http/, '').toUpperCase();
161 const routePath = joinCsPath(classPrefix, match[2] || '');
162 const line = safe.slice(0, match.index).split('\n').length;
163
164 const routeNode: Node = {
165 id: `route:${filePath}:${line}:${method}:${routePath}`,
166 kind: 'route',
167 name: `${method} ${routePath}`,
168 qualifiedName: `${filePath}::route:${routePath}`,
169 filePath,
170 startLine: line,
171 endLine: line,
172 startColumn: 0,
173 endColumn: match[0].length,
174 language: 'csharp',
175 updatedAt: now,
176 };
177 nodes.push(routeNode);
178
179 // Next method declaration (skip stacked attributes; C# puts the return type
180 // before the name). Bounded so we don't grab a far one.
181 const tail = safe.slice(match.index + match[0].length, match.index + match[0].length + 600);
182 const methodMatch = tail.match(/(?:public|private|protected|internal)\s+[\w<>,\s\[\]?.]+?\s+(\w+)\s*\(/);
183 if (methodMatch) {
184 references.push({
185 fromNodeId: routeNode.id,
186 referenceName: methodMatch[1]!,
187 referenceKind: 'references',
188 line,
189 column: 0,
190 filePath,
191 language: 'csharp',
192 });
193 }
194 }
195
196 // Minimal APIs: app.MapGet("/path", handler)
197 const minimalRegex = /\.Map(Get|Post|Put|Patch|Delete)\s*\(\s*"([^"]+)"\s*,\s*([^,)]+)/g;
198 while ((match = minimalRegex.exec(safe)) !== null) {

Callers

nothing calls this directly

Calls 6

stripCommentsForRegexFunction · 0.90
joinCsPathFunction · 0.85
extractCSharpTailIdentFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected