auryn-csharp 0.1.0__py3-none-any.whl

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,5 @@
1
+ """The C# language family. Registration is import-light: the `auryn.languages` entry
2
+ point targets the tiny `plugin` module (suffixes + a lazy GitLanguage), so file-based
3
+ discovery never imports tree-sitter or the Roslyn extractor. The live-source CSharpIngestor
4
+ registers separately via the `auryn.ingestors` group (loaded only by `auryn run`).
5
+ """
@@ -0,0 +1,38 @@
1
+ """Configuration for the C# ingestor."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+
8
+ @dataclass(frozen=True)
9
+ class CSharpSourceConfig:
10
+ path: str
11
+ """Root directory to scan for .cs files (recursively)."""
12
+
13
+ project_name: str
14
+ """Logical name for this project, used to build the Project node's id."""
15
+
16
+ solution_name: str | None = None
17
+ """Optional: wraps the Project in a Solution node. Omit to make Project the root
18
+ (id "csharp://{project_name}") rather than adding an unnecessary wrapper node."""
19
+
20
+ target_framework: str | None = None
21
+ """Free-text, e.g. "net8.0" -- informational only, not parsed from a .csproj."""
22
+
23
+ dto_name_suffixes: tuple[str, ...] = ("Dto", "Request", "Response", "ViewModel")
24
+ """Heuristic DTO classification fallback -- see the C# ingestor plan's
25
+ classification rules. A type used as an API endpoint's parameter/return type is
26
+ always classified as DTO regardless of name; this only covers everything else."""
27
+
28
+ workers: int | None = None
29
+ """Process pool size for parallel per-file parsing. None means os.cpu_count()."""
30
+
31
+
32
+ def project_id(project_name: str, solution_name: str | None) -> str:
33
+ """The id scheme every Namespace/File/Type/Method/etc. id is ultimately rooted
34
+ under (see resolution.py) -- shared by CSharpIngestor and git_sync.py so this
35
+ correctness-critical prefix is defined in exactly one place."""
36
+ if solution_name:
37
+ return f"csharp://{solution_name}/{project_name}"
38
+ return f"csharp://{project_name}"
@@ -0,0 +1,614 @@
1
+ """parse_file(path) -> RawFile: the parallelizable phase-1 unit (see the C# ingestor
2
+ plan's "Extraction pipeline"). Pure function, no shared state -- safe to run under a
3
+ ProcessPoolExecutor. Only picklable raw_model dataclasses are returned; no
4
+ tree-sitter node crosses that boundary.
5
+
6
+ parse_source(source, path_label) holds the actual logic and takes raw bytes
7
+ directly, so content sourced from somewhere other than the local filesystem (e.g.
8
+ a git blob, in ingestors/csharp/git_sync.py) can be parsed without ever being
9
+ written to a temp file first; parse_file is a thin, disk-reading wrapper around it.
10
+
11
+ Every field name/shape referenced below (e.g. method_declaration's "returns" field,
12
+ record_declaration's positional -- not field-named -- parameter_list, the params-
13
+ parameter grammar quirk in _parameters) was confirmed empirically against
14
+ tree-sitter-c-sharp 0.23.5 before writing this, not assumed from general C# grammar
15
+ knowledge -- see grammar.py's docstring for why this parser was chosen.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ from pathlib import Path
21
+
22
+ from auryn.core.docstrings import leading_comment
23
+ from auryn.core.http_seam import looks_like_route
24
+ from auryn.ingestors.csharp.grammar import new_parser
25
+ from auryn.ingestors.csharp.raw_model import (
26
+ RawAttribute,
27
+ RawCall,
28
+ RawEnumMember,
29
+ RawEvent,
30
+ RawField,
31
+ RawFile,
32
+ RawMethod,
33
+ RawParameter,
34
+ RawProperty,
35
+ RawType,
36
+ )
37
+
38
+ _TYPE_DECL_KINDS = {
39
+ "class_declaration": "Class",
40
+ "interface_declaration": "Interface",
41
+ "struct_declaration": "Struct",
42
+ "enum_declaration": "Enum",
43
+ "record_declaration": "Record",
44
+ "delegate_declaration": "Delegate",
45
+ }
46
+
47
+
48
+ def parse_file(path: str) -> RawFile:
49
+ return parse_source(Path(path).read_bytes(), path)
50
+
51
+
52
+ def parse_source(source: bytes, path_label: str) -> RawFile:
53
+ tree = new_parser().parse(source)
54
+ raw_file = RawFile(path=path_label)
55
+
56
+ # A file-scoped namespace (`namespace Foo.Bar;`, C# 10+) doesn't wrap the rest of
57
+ # the file as AST children the way a block-scoped one does -- every declaration
58
+ # after it remains a flat sibling at the compilation_unit level, so its namespace
59
+ # has to be tracked as loop state instead of via recursion.
60
+ current_namespace = ""
61
+ for child in tree.root_node.children:
62
+ if child.type == "using_directive":
63
+ ns = _using_namespace(child)
64
+ if ns:
65
+ raw_file.using_namespaces.append(ns)
66
+ elif child.type == "file_scoped_namespace_declaration":
67
+ name_node = child.child_by_field_name("name")
68
+ current_namespace = name_node.text.decode() if name_node is not None else ""
69
+ elif child.type == "namespace_declaration":
70
+ _walk_namespace(child, "", raw_file, source)
71
+ elif child.type in _TYPE_DECL_KINDS:
72
+ raw_file.types.append(_build_type(child, current_namespace, source))
73
+
74
+ return raw_file
75
+
76
+
77
+ def _walk_namespace(node, parent_namespace: str, raw_file: RawFile, source: bytes) -> None:
78
+ name_node = node.child_by_field_name("name")
79
+ # A block-scoped namespace's name field is a single node whose own text is
80
+ # already the full dotted segment (e.g. "Foo.Bar" as one qualified_name) --
81
+ # nesting isn't expressed by chained namespace_declarations for a dotted name,
82
+ # only by an actual nested `namespace X { ... }` block inside this one's body.
83
+ segment = name_node.text.decode() if name_node is not None else ""
84
+ full_namespace = f"{parent_namespace}.{segment}" if parent_namespace else segment
85
+
86
+ body = node.child_by_field_name("body")
87
+ if body is None:
88
+ return
89
+ for child in body.children:
90
+ if child.type == "namespace_declaration":
91
+ _walk_namespace(child, full_namespace, raw_file, source)
92
+ elif child.type in _TYPE_DECL_KINDS:
93
+ raw_file.types.append(_build_type(child, full_namespace, source))
94
+
95
+
96
+ def _using_namespace(node) -> str | None:
97
+ # No field name here (confirmed empirically) -- and for an alias form
98
+ # (`using Foo = System.Collections.Generic;`) there are two named children (the
99
+ # alias, then the real target), so the target is always the *last* one, not the
100
+ # first.
101
+ named = node.named_children
102
+ return named[-1].text.decode() if named else None
103
+
104
+
105
+ def _build_type(node, namespace: str, source: bytes) -> RawType:
106
+ kind = _TYPE_DECL_KINDS[node.type]
107
+ name_node = node.child_by_field_name("name")
108
+ modifiers = _modifiers(node)
109
+
110
+ raw_type = RawType(
111
+ kind=kind,
112
+ name=name_node.text.decode() if name_node is not None else "",
113
+ namespace=namespace,
114
+ accessibility=_accessibility(modifiers),
115
+ modifiers=modifiers,
116
+ type_parameters=_type_parameters(node),
117
+ attributes=_attributes(node),
118
+ source=_type_text(node),
119
+ doc=leading_comment(node, source),
120
+ )
121
+
122
+ if kind == "Record":
123
+ # "record struct"/"readonly record struct" has an extra positional `struct`
124
+ # token; plain/"record class" doesn't (or has an explicit `class` token).
125
+ raw_type.is_value_type = any(c.type == "struct" for c in node.children)
126
+
127
+ if kind == "Enum":
128
+ raw_type.underlying_type = _enum_underlying_type(node)
129
+ else:
130
+ raw_type.base_list = _base_list(node)
131
+
132
+ if kind == "Delegate":
133
+ raw_type.return_type = _type_text(node.child_by_field_name("type"))
134
+ raw_type.positional_parameters = _parameters(node.child_by_field_name("parameters"))
135
+ return raw_type
136
+
137
+ if kind == "Record":
138
+ # Unlike method_declaration, record_declaration's primary-constructor
139
+ # parameter_list has no field name -- it's positional (confirmed empirically).
140
+ param_list = next((c for c in node.children if c.type == "parameter_list"), None)
141
+ raw_type.positional_parameters = _parameters(param_list)
142
+
143
+ body = node.child_by_field_name("body")
144
+ if body is None:
145
+ return raw_type
146
+
147
+ if body.type == "enum_member_declaration_list":
148
+ raw_type.enum_members = _enum_members(body)
149
+ return raw_type
150
+
151
+ for member in body.children:
152
+ _dispatch_member(member, raw_type, source)
153
+
154
+ return raw_type
155
+
156
+
157
+ def _dispatch_member(node, raw_type: RawType, source: bytes) -> None:
158
+ if node.type == "field_declaration":
159
+ raw_type.fields.extend(_build_fields(node))
160
+ elif node.type == "event_field_declaration":
161
+ raw_type.events.extend(_build_events(node))
162
+ elif node.type == "property_declaration":
163
+ raw_type.properties.append(_build_property(node))
164
+ elif node.type == "method_declaration":
165
+ raw_type.methods.append(_build_method(node, "ORDINARY", source))
166
+ elif node.type == "constructor_declaration":
167
+ raw_type.methods.append(_build_method(node, "CONSTRUCTOR", source))
168
+ elif node.type == "destructor_declaration":
169
+ raw_type.methods.append(_build_method(node, "DESTRUCTOR", source))
170
+ elif node.type == "operator_declaration":
171
+ raw_type.methods.append(_build_operator_method(node, source))
172
+ elif node.type == "conversion_operator_declaration":
173
+ raw_type.methods.append(_build_conversion_operator_method(node, source))
174
+ elif node.type in _TYPE_DECL_KINDS:
175
+ raw_type.nested_types.append(_build_type(node, raw_type.namespace, source))
176
+ # Anything else (indexer_declaration, custom-accessor event_declaration, static
177
+ # constructors, etc.) is silently skipped -- best-effort, same philosophy as the
178
+ # PL/SQL ingestor's unrecognized ALL_DEPENDENCIES object types.
179
+
180
+
181
+ def _build_fields(node) -> list[RawField]:
182
+ modifiers = _modifiers(node)
183
+ accessibility = _accessibility(modifiers)
184
+ attrs = _attributes(node)
185
+ is_const = "const" in modifiers
186
+ is_readonly = "readonly" in modifiers
187
+ is_static = "static" in modifiers or is_const
188
+
189
+ var_decl = next((c for c in node.children if c.type == "variable_declaration"), None)
190
+ if var_decl is None:
191
+ return []
192
+ type_text = _type_text(var_decl.child_by_field_name("type"))
193
+
194
+ fields = []
195
+ for declarator in var_decl.children:
196
+ if declarator.type != "variable_declarator":
197
+ continue
198
+ name_node = declarator.child_by_field_name("name")
199
+ fields.append(
200
+ RawField(
201
+ name=name_node.text.decode() if name_node is not None else declarator.text.decode(),
202
+ accessibility=accessibility,
203
+ type=type_text,
204
+ is_readonly=is_readonly,
205
+ is_const=is_const,
206
+ is_static=is_static,
207
+ attributes=attrs,
208
+ )
209
+ )
210
+ return fields
211
+
212
+
213
+ def _build_events(node) -> list[RawEvent]:
214
+ modifiers = _modifiers(node)
215
+ accessibility = _accessibility(modifiers)
216
+ attrs = _attributes(node)
217
+ is_static = "static" in modifiers
218
+
219
+ var_decl = next((c for c in node.children if c.type == "variable_declaration"), None)
220
+ if var_decl is None:
221
+ return []
222
+ type_text = _type_text(var_decl.child_by_field_name("type"))
223
+
224
+ events = []
225
+ for declarator in var_decl.children:
226
+ if declarator.type != "variable_declarator":
227
+ continue
228
+ name_node = declarator.child_by_field_name("name")
229
+ events.append(
230
+ RawEvent(
231
+ name=name_node.text.decode() if name_node is not None else declarator.text.decode(),
232
+ accessibility=accessibility,
233
+ type=type_text,
234
+ is_static=is_static,
235
+ attributes=attrs,
236
+ )
237
+ )
238
+ return events
239
+
240
+
241
+ def _build_property(node) -> RawProperty:
242
+ modifiers = _modifiers(node)
243
+ accessibility = _accessibility(modifiers)
244
+ is_static = "static" in modifiers
245
+ attrs = _attributes(node)
246
+ name_node = node.child_by_field_name("name")
247
+ type_text = _type_text(node.child_by_field_name("type"))
248
+
249
+ accessors_node = node.child_by_field_name("accessors")
250
+ value_node = node.child_by_field_name("value")
251
+ if accessors_node is not None:
252
+ has_getter = False
253
+ has_setter = False
254
+ is_auto = True
255
+ for accessor in accessors_node.named_children:
256
+ if accessor.type != "accessor_declaration":
257
+ continue
258
+ accessor_name_node = accessor.child_by_field_name("name")
259
+ keyword = accessor_name_node.type if accessor_name_node is not None else ""
260
+ if keyword == "get":
261
+ has_getter = True
262
+ elif keyword in ("set", "init"):
263
+ has_setter = True
264
+ if accessor.child_by_field_name("body") is not None:
265
+ is_auto = False
266
+ elif value_node is not None:
267
+ # `=> expr;` form -- always getter-only, and never auto-implemented (it has
268
+ # a computed body, just spelled with an arrow instead of accessor braces).
269
+ has_getter, has_setter, is_auto = True, False, False
270
+ else:
271
+ has_getter, has_setter, is_auto = False, False, False
272
+
273
+ return RawProperty(
274
+ name=name_node.text.decode() if name_node is not None else "",
275
+ accessibility=accessibility,
276
+ type=type_text,
277
+ has_getter=has_getter,
278
+ has_setter=has_setter,
279
+ is_auto_implemented=is_auto,
280
+ is_static=is_static,
281
+ attributes=attrs,
282
+ )
283
+
284
+
285
+ def _build_method(node, kind: str, source: bytes) -> RawMethod:
286
+ modifiers = _modifiers(node)
287
+ accessibility = _accessibility(modifiers)
288
+ attrs = _attributes(node)
289
+ name_node = node.child_by_field_name("name")
290
+ name = name_node.text.decode() if name_node is not None else kind.lower()
291
+ return_type = _type_text(node.child_by_field_name("returns")) if kind == "ORDINARY" else None
292
+ params = _parameters(node.child_by_field_name("parameters"))
293
+ body_node = node.child_by_field_name("body")
294
+
295
+ return RawMethod(
296
+ name=name,
297
+ kind=kind,
298
+ accessibility=accessibility,
299
+ modifiers=modifiers,
300
+ return_type=return_type,
301
+ parameters=params,
302
+ attributes=attrs,
303
+ source=_type_text(node),
304
+ doc=leading_comment(node, source),
305
+ calls=_extract_calls(body_node) if body_node is not None else [],
306
+ )
307
+
308
+
309
+ def _build_operator_method(node, source: bytes) -> RawMethod:
310
+ modifiers = _modifiers(node)
311
+ accessibility = _accessibility(modifiers)
312
+ attrs = _attributes(node)
313
+ return_type = _type_text(node.child_by_field_name("type"))
314
+ op_node = node.child_by_field_name("operator")
315
+ op_text = op_node.text.decode() if op_node is not None else "?"
316
+ params = _parameters(node.child_by_field_name("parameters"))
317
+ body_node = node.child_by_field_name("body")
318
+
319
+ return RawMethod(
320
+ name=f"operator{op_text}",
321
+ kind="OPERATOR",
322
+ accessibility=accessibility,
323
+ modifiers=modifiers,
324
+ return_type=return_type,
325
+ parameters=params,
326
+ attributes=attrs,
327
+ source=_type_text(node),
328
+ doc=leading_comment(node, source),
329
+ calls=_extract_calls(body_node) if body_node is not None else [],
330
+ )
331
+
332
+
333
+ def _build_conversion_operator_method(node, source: bytes) -> RawMethod:
334
+ modifiers = _modifiers(node)
335
+ accessibility = _accessibility(modifiers)
336
+ attrs = _attributes(node)
337
+ return_type = _type_text(node.child_by_field_name("type"))
338
+ conversion_kind = next((c.text.decode() for c in node.children if c.type in ("implicit", "explicit")), "explicit")
339
+ params = _parameters(node.child_by_field_name("parameters"))
340
+ body_node = node.child_by_field_name("body")
341
+
342
+ return RawMethod(
343
+ name=f"operator {conversion_kind} {return_type}",
344
+ kind="OPERATOR",
345
+ accessibility=accessibility,
346
+ modifiers=modifiers,
347
+ return_type=return_type,
348
+ parameters=params,
349
+ attributes=attrs,
350
+ source=_type_text(node),
351
+ doc=leading_comment(node, source),
352
+ calls=_extract_calls(body_node) if body_node is not None else [],
353
+ )
354
+
355
+
356
+ def _parameters(parameter_list_node) -> list[RawParameter]:
357
+ if parameter_list_node is None:
358
+ return []
359
+
360
+ parameters: list[RawParameter] = []
361
+ position = 0
362
+ pending_params = False
363
+ pending_type = None
364
+
365
+ for i, child in enumerate(parameter_list_node.children):
366
+ field = parameter_list_node.field_name_for_child(i)
367
+ if child.type == "params":
368
+ pending_params = True
369
+ elif child.type == "parameter":
370
+ parameters.append(_build_parameter(child, position, pending_params))
371
+ position += 1
372
+ pending_params = False
373
+ elif field == "type":
374
+ # The `params T[] name` grammar quirk: type/name are attached directly to
375
+ # parameter_list instead of wrapped in a `parameter` node (confirmed
376
+ # empirically -- see module docstring).
377
+ pending_type = child
378
+ elif field == "name" and pending_type is not None:
379
+ parameters.append(
380
+ RawParameter(
381
+ name=child.text.decode(),
382
+ type=_type_text(pending_type),
383
+ direction="PARAMS" if pending_params else "VALUE",
384
+ position=position,
385
+ has_default=False,
386
+ )
387
+ )
388
+ position += 1
389
+ pending_params = False
390
+ pending_type = None
391
+
392
+ return parameters
393
+
394
+
395
+ def _build_parameter(node, position: int, forced_params: bool) -> RawParameter:
396
+ type_node = node.child_by_field_name("type")
397
+ name_node = node.child_by_field_name("name")
398
+ direction = "PARAMS" if forced_params else "VALUE"
399
+ for child in node.children:
400
+ if child.type == "modifier":
401
+ text = child.text.decode()
402
+ if text in ("ref", "out", "in"):
403
+ direction = text.upper()
404
+ has_default = any(child.type == "=" for child in node.children)
405
+
406
+ return RawParameter(
407
+ name=name_node.text.decode() if name_node is not None else "",
408
+ type=_type_text(type_node),
409
+ direction=direction,
410
+ position=position,
411
+ has_default=has_default,
412
+ attributes=_attributes(node),
413
+ )
414
+
415
+
416
+ def _enum_underlying_type(node) -> str | None:
417
+ base_list_node = next((c for c in node.children if c.type == "base_list"), None)
418
+ if base_list_node is None:
419
+ return None
420
+ named = base_list_node.named_children
421
+ return named[0].text.decode() if named else None
422
+
423
+
424
+ def _enum_members(body) -> list[RawEnumMember]:
425
+ members = []
426
+ for child in body.named_children:
427
+ if child.type != "enum_member_declaration":
428
+ continue
429
+ name_node = child.child_by_field_name("name")
430
+ value_node = child.child_by_field_name("value")
431
+ members.append(
432
+ RawEnumMember(
433
+ name=name_node.text.decode() if name_node is not None else "",
434
+ value=value_node.text.decode() if value_node is not None else None,
435
+ )
436
+ )
437
+ return members
438
+
439
+
440
+ def _base_list(node) -> list[str]:
441
+ base_list_node = next((c for c in node.children if c.type == "base_list"), None)
442
+ if base_list_node is None:
443
+ return []
444
+ return [_base_entry_name(c) for c in base_list_node.named_children]
445
+
446
+
447
+ def _base_entry_name(node) -> str:
448
+ if node.type == "generic_name":
449
+ inner = node.children[0]
450
+ return _base_entry_name(inner) if inner.type == "qualified_name" else inner.text.decode()
451
+ if node.type == "qualified_name":
452
+ name_node = node.child_by_field_name("name")
453
+ return name_node.text.decode() if name_node is not None else node.text.decode()
454
+ return node.text.decode()
455
+
456
+
457
+ def _modifiers(node) -> list[str]:
458
+ return [c.text.decode() for c in node.children if c.type == "modifier"]
459
+
460
+
461
+ def _accessibility(modifiers: list[str]) -> str:
462
+ has = lambda kw: kw in modifiers # noqa: E731
463
+ if has("private") and has("protected"):
464
+ return "private protected"
465
+ if has("protected") and has("internal"):
466
+ return "protected internal"
467
+ for kw in ("public", "private", "protected", "internal"):
468
+ if has(kw):
469
+ return kw
470
+ # C#'s real default varies by context (private for class members, public for
471
+ # interface members, internal for top-level types) -- collapsing to one default
472
+ # is a deliberate simplification; accessibility here is informational only, never
473
+ # used to decide what to ingest.
474
+ return "internal"
475
+
476
+
477
+ def _type_parameters(node) -> list[str]:
478
+ tp_list = node.child_by_field_name("type_parameters")
479
+ if tp_list is None:
480
+ # class/struct/record don't field-name this (unlike interface_declaration) --
481
+ # confirmed empirically to be a genuine grammar inconsistency, not a mistake.
482
+ tp_list = next((c for c in node.children if c.type == "type_parameter_list"), None)
483
+ if tp_list is None:
484
+ return []
485
+ return [
486
+ c.child_by_field_name("name").text.decode() for c in tp_list.named_children if c.type == "type_parameter"
487
+ ]
488
+
489
+
490
+ def _attributes(node) -> list[RawAttribute]:
491
+ attrs = []
492
+ for child in node.children:
493
+ if child.type != "attribute_list":
494
+ continue
495
+ for attr_node in child.named_children:
496
+ if attr_node.type == "attribute":
497
+ attrs.append(_build_attribute(attr_node))
498
+ return attrs
499
+
500
+
501
+ def _build_attribute(node) -> RawAttribute:
502
+ name_node = node.child_by_field_name("name")
503
+ name = name_node.text.decode() if name_node is not None else node.text.decode()
504
+ # Normalize the rarely-used explicit "Attribute" suffix (`[HttpGetAttribute(...)]`
505
+ # means the same thing as `[HttpGet(...)]`) so every consumer can match on one
506
+ # spelling.
507
+ if name.endswith("Attribute") and name != "Attribute":
508
+ name = name[: -len("Attribute")]
509
+ args = []
510
+ arg_list = next((c for c in node.children if c.type == "attribute_argument_list"), None)
511
+ if arg_list is not None:
512
+ for arg_node in arg_list.named_children:
513
+ if arg_node.type == "attribute_argument":
514
+ args.append(_attribute_argument_text(arg_node))
515
+ return RawAttribute(name=name, arguments=args)
516
+
517
+
518
+ def _attribute_argument_text(node) -> str:
519
+ # Best-effort: unwrap a bare string literal (covers the common
520
+ # Route("template")/HttpGet("template") case, named or positional) without a full
521
+ # C# constant-expression evaluator; anything else falls back to raw source text.
522
+ string_literal = next((c for c in node.named_children if c.type == "string_literal"), None)
523
+ if string_literal is not None:
524
+ content = next((c for c in string_literal.named_children if c.type == "string_literal_content"), None)
525
+ return content.text.decode() if content is not None else string_literal.text.decode()
526
+ return node.text.decode()
527
+
528
+
529
+ def _extract_calls(node) -> list[RawCall]:
530
+ calls: list[RawCall] = []
531
+ _walk_calls(node, calls)
532
+ return calls
533
+
534
+
535
+ # HttpClient call idioms (ADR 0006). Tree-sitter has no types, so detection is by the distinctive
536
+ # async method name + a URL-shaped first string arg (looks_like_route) -- so `repo.GetAsync("key")`
537
+ # is not mistaken for HTTP. The Roslyn deep pass detects the same calls precisely (by type).
538
+ _CS_HTTP_METHODS = {
539
+ "getasync": "GET", "getstringasync": "GET", "getbytearrayasync": "GET", "getstreamasync": "GET",
540
+ "getfromjsonasync": "GET", "postasync": "POST", "postasjsonasync": "POST",
541
+ "putasync": "PUT", "putasjsonasync": "PUT", "deleteasync": "DELETE", "deletefromjsonasync": "DELETE",
542
+ "patchasync": "PATCH", "patchasjsonasync": "PATCH",
543
+ }
544
+
545
+
546
+ def _walk_calls(node, calls: list[RawCall]) -> None:
547
+ if node.type == "invocation_expression":
548
+ func = node.child_by_field_name("function")
549
+ if func is not None:
550
+ call = _call_reference(func)
551
+ verb = _CS_HTTP_METHODS.get(call.name.lower())
552
+ if verb is not None: # gate the arg walk on the cheap name check (perf)
553
+ url = _first_string_arg(node)
554
+ if url is not None and looks_like_route(url):
555
+ call.http_url, call.http_method = url, verb
556
+ calls.append(call)
557
+ for child in node.children:
558
+ _walk_calls(child, calls)
559
+
560
+
561
+ def _first_string_arg(invocation) -> str | None:
562
+ """The first string argument of an invocation, unquoted -- the request URL for an HttpClient
563
+ call. Interpolated URLs (`$"api/x/{id}"`) become `api/x/{param}` so they match a route template."""
564
+ args = invocation.child_by_field_name("arguments")
565
+ if args is None:
566
+ return None
567
+ for arg in args.children:
568
+ for node in (arg, *arg.children):
569
+ if node.type in ("string_literal", "verbatim_string_literal", "raw_string_literal"):
570
+ text = node.text.decode()
571
+ if text.startswith("@"):
572
+ text = text[1:]
573
+ return text.strip('"')
574
+ for arg in args.children:
575
+ for node in (arg, *arg.children):
576
+ if node.type == "interpolated_string_expression":
577
+ return _interpolated_route(node.text.decode())
578
+ return None
579
+
580
+
581
+ def _interpolated_route(text: str) -> str:
582
+ """`$"api/widgets/{id}"` -> `api/widgets/{param}`: replace each top-level `{...}` (a C#
583
+ interpolation) with `{param}`, then drop the `$`/`@` prefix and surrounding quotes."""
584
+ out: list[str] = []
585
+ depth = 0
586
+ for ch in text:
587
+ if ch == "{":
588
+ if depth == 0:
589
+ out.append("{param}")
590
+ depth += 1
591
+ elif ch == "}":
592
+ depth = max(0, depth - 1)
593
+ elif depth == 0:
594
+ out.append(ch)
595
+ return "".join(out).lstrip("$@").strip('"')
596
+
597
+
598
+ def _call_reference(node) -> RawCall:
599
+ if node.type == "member_access_expression":
600
+ name_node = node.child_by_field_name("name")
601
+ name = name_node.text.decode() if name_node is not None else node.text.decode()
602
+ receiver_node = node.child_by_field_name("expression")
603
+ receiver = None
604
+ if receiver_node is not None and receiver_node.type in ("identifier", "this"):
605
+ receiver = receiver_node.text.decode()
606
+ return RawCall(name=name, receiver=receiver)
607
+ if node.type == "generic_name":
608
+ inner = node.children[0]
609
+ return _call_reference(inner) if inner.type == "member_access_expression" else RawCall(name=inner.text.decode())
610
+ return RawCall(name=node.text.decode())
611
+
612
+
613
+ def _type_text(node) -> str | None:
614
+ return node.text.decode(errors="replace") if node is not None else None