diff --git a/src/services/graph-resolution.ts b/src/services/graph-resolution.ts index b0cd97c..c160a13 100644 --- a/src/services/graph-resolution.ts +++ b/src/services/graph-resolution.ts @@ -81,9 +81,15 @@ export function buildCsNamespaceMap( ): Map { const map = new Map(); // Match both `namespace Foo.Bar { ... }` and the file-scoped C# 10+ - // syntax `namespace Foo.Bar;`. The `^` plus `m` flag pins to line start - // so commented lines (`// namespace X.Y`) are not matched. - const namespaceRegex = /^namespace\s+([\w.]+)/gm; + // syntax `namespace Foo.Bar;`. The `^\s*` lets us catch nested + // declarations (`namespace Outer { namespace Inner { ... } }`) which + // are indented inside the outer block. The dotted-identifier capture + // requires each segment to start with a letter or underscore (matching + // C# identifier rules) so junk like `namespace 1Foo` is rejected. The + // `(?=[;{])` lookahead ensures we only match real declarations and + // not stray occurrences of the word `namespace`. + const namespaceRegex = + /^\s*namespace\s+([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*)\s*(?=[;{])/gm; // `fileSet` reflects fs.readdir() traversal order, which POSIX does not // guarantee. Sort .cs paths lexically so candidate lists are stable. diff --git a/tests/unit/graph-resolution.test.ts b/tests/unit/graph-resolution.test.ts index bc463e0..3f80b8c 100644 --- a/tests/unit/graph-resolution.test.ts +++ b/tests/unit/graph-resolution.test.ts @@ -907,6 +907,44 @@ describe("graph-resolution", () => { const map = buildCsNamespaceMap(project.fileSet, project.root); expect(map.get("MyApp.X")).toEqual(["Dup.cs"]); }); + + it("captures nested namespace declarations indented inside an outer block", () => { + project = createTempProject({ + "Nested.cs": + "namespace Outer\n{\n namespace Inner\n {\n class C {}\n }\n}\n", + }); + + const map = buildCsNamespaceMap(project.fileSet, project.root); + expect(map.get("Outer")).toEqual(["Nested.cs"]); + expect(map.get("Inner")).toEqual(["Nested.cs"]); + }); + + it("rejects identifiers that do not start with a letter or underscore", () => { + project = createTempProject({ + // Invalid C# (digit-leading), should not be captured. + "Bad.cs": "namespace 1Foo { class C {} }", + // Valid neighbour to make sure the scan still works. + "Good.cs": "namespace Real.NS { class C {} }", + }); + + const map = buildCsNamespaceMap(project.fileSet, project.root); + expect(map.has("1Foo")).toBe(false); + expect(map.get("Real.NS")).toEqual(["Good.cs"]); + }); + + it("requires a `;` or `{` after the namespace name to avoid false positives", () => { + project = createTempProject({ + // The token `namespace MyApp.Hint` appears here but is not a real + // declaration (no terminator), so it must not be captured. The + // real declaration on the next line should be captured. + "Mixed.cs": + "// see also: namespace MyApp.Hint in legacy code\nnamespace MyApp.Real { class C {} }", + }); + + const map = buildCsNamespaceMap(project.fileSet, project.root); + expect(map.has("MyApp.Hint")).toBe(false); + expect(map.get("MyApp.Real")).toEqual(["Mixed.cs"]); + }); }); // ── Swift resolution ──────────────────────────────────────────────────