fix(csharp): attribute declarations under file-scoped namespaces correctly#63
Open
alexander-heinrich wants to merge 1 commit into
Open
Conversation
…ectly The C# grammar represents a file-scoped namespace (namespace X;) as file_scoped_namespace_declaration, a distinct node type from namespace_declaration with no body field: every top-level declaration after it belongs to that namespace. _visit_top_level only matched namespace_declaration, so types under a file-scoped namespace were silently attributed to the module instead - no NAMESPACE CodeUnit, no CONTAINS edge from the namespace. Track the active file-scoped namespace while walking the top level in _visit_module and route subsequent declarations through _visit_ns_child, mirroring what _extract_namespace does with a braced body. Factor the shared CodeUnit/CONTAINS-edge creation into _create_namespace_unit. If the namespace node has no extractable name, later declarations keep falling back to module attribution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The C# parser only recognizes block-scoped namespaces (
namespace X { ... }). File-scoped namespaces (namespace X;, C# 10+) are a different tree-sitter node type, so they were ignored: noNAMESPACEnode was created, and every type in the file was attached directly to the module.Fix
While walking the top level in
_visit_module, watch for afile_scoped_namespace_declaration. When one appears, create the namespace unit and treat everything after it as a child of that namespace. (C# allows at most one file-scoped namespace per file, and nothing else can follow it at top level except its members, so this is safe.)The namespace-unit creation shared by both syntaxes now lives in one helper,
_create_namespace_unit.Impact
Measured on MediatR v14.2.0 (~3K lines of C#; 42 of 43 source files use file-scoped namespaces) by running
CSharpParser.parse_directoryoversrc/before and after the change. Before, trailmark recovers zero namespace nodes for the entire library, and all 52 types attach directly to their file modules. After, it recovers all 42 namespace declarations (9 distinct namespaces) and attributes all 52 types to their namespace.Test plan
test_csharp_parser.py(TestCSharpFileScopedNamespace, the existingSAMPLE_CODEfixture converted to file-scoped syntax): namespace node with exact id and location, module -> namespace edge, types contained by the namespace and not the module,usingdirectives before the namespace still registered, methods inside the types still foundruff check/ruff format --check/ty checkclean; mutation testing leaves no surviving mutants in touched code