Skip to content

feat: NE0002 — namespace must match folder structure - #12

Merged
samtrion merged 1 commit into
mainfrom
feat/ne0002-namespace-folder
Aug 3, 2026
Merged

feat: NE0002 — namespace must match folder structure#12
samtrion merged 1 commit into
mainfrom
feat/ne0002-namespace-folder

Conversation

@samtrion

@samtrion samtrion commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements NE0002: the declared namespace should match the folder structure, anchored at the RootNamespace MSBuild property. When a type's namespace does not follow the folder it lives in, the physical and logical layout diverge and navigation breaks.

Anchored at RootNamespace, every folder segment between the project directory and the file must appear as the corresponding namespace segment. A diagnostic is reported when the declared namespace does not equal RootNamespace + the relative folder path (.-joined).

Non-compliant

// RootNamespace = Geometry, file = Shapes/Primitives/Circle.cs
namespace Geometry.Shapes;   // NE0002: expected 'Geometry.Shapes.Primitives'
public sealed class Circle { }

Behavior

  • Evaluates each top-level namespace declaration. Nested namespaces are left to NE0003; global-namespace files are out of scope.
  • Files directly in the project root map to RootNamespace exactly.
  • No mapping is attempted (and nothing is reported) when RootNamespace/ProjectDir are unavailable, the file is outside the project directory, or a folder segment is not a valid identifier.
  • Honors the file-organization opt-outs (PublishSingleFile, NetEvolveAnalyzerDisableFileOrganizationRules) and skips generated code.

Code fix

Rewrites the namespace declaration to the expected value (WellKnownFixAllProviders.BatchFixer supported).

Notes

  • Introduces the shared FolderNamespace helper (folder → namespace mapping) and exposes RootNamespace/ProjectDir via CompilerVisibleProperty. Both are reused by NE0003 (analyzer: NE0003 — only one namespace declaration per file #5), which is stacked on this PR.
  • Unit + integration tests and docs/rules/NE0002.md included.

Closes #4

Add the NE0002 analyzer and code fix: anchored at the RootNamespace MSBuild
property, the declared namespace should equal RootNamespace joined with the
file's folder path relative to the project directory, so the physical and
logical layout stay aligned (comparable to Roslyn IDE0130, made configurable
and RootNamespace-aware).

- NamespaceMatchesFolderAnalyzer evaluates each top-level namespace
  declaration; nested namespaces are left to NE0003 and global-namespace
  files are out of scope
- NamespaceMatchesFolderCodeFixProvider rewrites the namespace to the
  expected value (batch fix-all supported)
- Shared FolderNamespace helper maps a file's folder path to its namespace;
  reused by the NE0003 flatten fix
- RootNamespace and ProjectDir exposed via CompilerVisibleProperty
- Honors the file-organization opt-outs (PublishSingleFile,
  NetEvolveAnalyzerDisableFileOrganizationRules) and skips generated code
- Unit + integration tests, docs/rules/NE0002.md

Closes #4
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are limited based on label configuration.

🏷️ Required labels (at least one) (1)
  • state:ready for merge

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4028a9c8-c921-4f8f-86a4-1a7bc4055ce4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.08%. Comparing base (4d860df) to head (6757e2d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #12      +/-   ##
==========================================
+ Coverage   98.59%   99.08%   +0.49%     
==========================================
  Files           4        7       +3     
  Lines         213      328     +115     
  Branches       44       62      +18     
==========================================
+ Hits          210      325     +115     
  Partials        3        3              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@samtrion
samtrion merged commit 26f0ef5 into main Aug 3, 2026
8 checks passed
@samtrion
samtrion deleted the feat/ne0002-namespace-folder branch August 3, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

analyzer: NE0002 — namespace must match folder structure (RootNamespace-anchored)

1 participant