From ea69a721459039b9995e93c3d6a094327ffcb7a4 Mon Sep 17 00:00:00 2001 From: Giancarlo Erra Date: Tue, 28 Apr 2026 00:30:36 +0100 Subject: [PATCH] fix(graph): tighten C# namespace regex; capture nested declarations The previous regex `^namespace\s+([\w.]+)` was anchored at column 0, so nested namespace declarations (the `namespace Inner` block inside `namespace Outer { ... }`) were silently missed. The capture group also accepted invalid identifiers like `1Foo` and matched on stray occurrences of the word `namespace` not followed by `;` or `{`. The new regex `^\s*namespace\s+([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*)\s*(?=[;{])`: - `^\s*` allows leading whitespace, so indented (nested) declarations are captured. - The dotted-identifier capture requires each segment to start with a letter or underscore, matching C# identifier rules. - `(?=[;{])` requires a real terminator, filtering out spurious matches. Adds three regression tests: nested-namespace capture, rejection of digit-leading identifiers, and the terminator-lookahead behaviour. Found by CodeRabbit on PR #34 (nitpick). --- src/services/graph-resolution.ts | 12 ++++++--- tests/unit/graph-resolution.test.ts | 38 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) 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 ──────────────────────────────────────────────────