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).
This commit is contained in:
Giancarlo Erra
2026-04-28 00:30:36 +01:00
parent fc249fdfcf
commit ea69a72145
2 changed files with 47 additions and 3 deletions
+9 -3
View File
@@ -81,9 +81,15 @@ export function buildCsNamespaceMap(
): Map<string, string[]> {
const map = new Map<string, string[]>();
// 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.
+38
View File
@@ -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 ──────────────────────────────────────────────────