feat: NE0002 — namespace must match folder structure - #12
Conversation
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
|
Important Review skippedAuto reviews are limited based on label configuration. 🏷️ Required labels (at least one) (1)
Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Summary
Implements NE0002: the declared namespace should match the folder structure, anchored at the
RootNamespaceMSBuild 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 equalRootNamespace+ the relative folder path (.-joined).Non-compliant
Behavior
RootNamespaceexactly.RootNamespace/ProjectDirare unavailable, the file is outside the project directory, or a folder segment is not a valid identifier.PublishSingleFile,NetEvolveAnalyzerDisableFileOrganizationRules) and skips generated code.Code fix
Rewrites the namespace declaration to the expected value (
WellKnownFixAllProviders.BatchFixersupported).Notes
FolderNamespacehelper (folder → namespace mapping) and exposesRootNamespace/ProjectDirviaCompilerVisibleProperty. Both are reused by NE0003 (analyzer: NE0003 — only one namespace declaration per file #5), which is stacked on this PR.docs/rules/NE0002.mdincluded.Closes #4