Repository navigation
SA1201: support configurable member-kind ordering - #4324
Conversation
Support partial member-kind orders with contiguous named groups, JSON and EditorConfig configuration, and safe shared ordering fixes. Preserve default behavior and report invalid configuration through SA0002. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add Russian and Chinese diagnostic translations and update all three localized configuration and rule references, preserving examples and English bookmarks. These resolve the CI resource-completeness and documentation-localization failures. Translations have not received native-speaker review. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Localization-test compatibility and ordering behavior remain unresolved.
4 open findings
What changed in this PR
Adds configurable member-kind ordering to SA1201 while preserving the default order when no configuration is supplied.
Changes:
- Adds JSON and EditorConfig settings for partial or empty member-kind orders.
- Updates diagnostics and shared ordering fixes, with safeguards against unsafe moves.
- Adds regression tests, schema support, and documentation.
| File | Description |
|---|---|
| test/StyleCop.Analyzers.Test/SpecialRules/SA0002UnitTests.cs | Tests invalid configuration values. |
| test/StyleCop.Analyzers.Test/OrderingRules/SA1201UnitTests.MemberKindOrder.cs | Tests configured ordering and fix safety. |
| test/StyleCop.Analyzers.Test/OrderingRules/SA1201UnitTests.cs | Enables partial test-class extensions. |
| test/StyleCop.Analyzers.Test.CSharp8/OrderingRules/SA1201CSharp8UnitTests.MemberKindOrder.cs | Tests EditorConfig configuration and validation. |
| test/StyleCop.Analyzers.Test.CSharp15/OrderingRules/SA1201CSharp15UnitTests.cs | Tests union ordering. |
| test/StyleCop.Analyzers.Test.CSharp14/OrderingRules/SA1201CSharp14UnitTests.cs | Tests extension-block ordering. |
| test/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1201CSharp10UnitTests.cs | Tests record ordering. |
| StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json | Defines the configuration schema. |
| src/StyleCop.Analyzers/SpecialRules/SA0002InvalidSettingsFile.cs | Validates later-file configuration. |
| src/StyleCop.Analyzers/Settings/SettingsHelper.cs | Separates validation from cached fallback settings. |
| src/StyleCop.Analyzers/Settings/ObjectModel/SettingsReader.cs | Supports explicit empty lists. |
| src/StyleCop.Analyzers/Settings/ObjectModel/OrderingSettings.cs | Loads member-kind ordering settings. |
| src/StyleCop.Analyzers/Settings/ObjectModel/MemberKindOrder.cs | Maps kinds and ordering priorities. |
| src/StyleCop.Analyzers/OrderingRules/SA1201ElementsMustAppearInTheCorrectOrder.cs | Analyzes configured groups and interruptions. |
| src/StyleCop.Analyzers/OrderingRules/OrderingResources.resx | Adds the interruption message. |
| src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/OrderingResources.Designer.cs | Exposes the generated message accessor. |
| src/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs | Implements configured moves and safety checks. |
| documentation/SA1201.md | Explains customizable ordering. |
| documentation/SA0002.md | Documents configuration validation errors. |
| documentation/Configuration.md | Documents settings, precedence, and safeguards. |
Files not reviewed (1)
- src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/OrderingResources.Designer.cs: Generated file
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #4324 +/- ##
==========================================
+ Coverage 97.63% 97.66% +0.02%
==========================================
Files 1321 1324 +3
Lines 144930 146324 +1394
Branches 4874 4945 +71
==========================================
+ Hits 141504 142904 +1400
+ Misses 2173 2171 -2
+ Partials 1253 1249 -4 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🔵 Needs a closer look
Shared settings validation and cross-rule reordering changes warrant human review across supported compilers.
4 open findings
Files not reviewed (1)
- src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/OrderingResources.Designer.cs: Generated file
🧠 Review effort: Balanced
Keep both SA1201 descriptors available to the analyzer while generating one rule inventory entry per ID. Add a regression assertion against the generated report. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Track comparable static and readonly members across permitted gaps, respecting higher-priority traits and incomplete members. Keep the default analyzer loops unchanged. Treat constants as implicitly static and readonly consistently in configured fixes. Preserve safe gap insertion targets when an earlier initializer blocks further movement. Focused base/newest suites pass on both frameworks, and all 24 documentation-localization checks pass. Release callback measurements over 20,000 type callbacks show overlapping default timings and no added default allocations. Configured SA1204 costs about 26 ms versus 15 ms and allocates 192 bytes per type; configured SA1214 costs about 8.8 ms versus 6.1 ms without added allocations. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Shared ordering fixes, initializer safety, and per-file settings interactions warrant final human review despite no confirmed outstanding defects.
0 open findings
4 resolved since last review
Files not reviewed (1)
- src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/OrderingResources.Designer.cs: Generated file
🧠 Review effort: Balanced


SA1201 currently has a fixed declaration-kind order;
elementOrderonly controls the precedence of ordering traits. This adds an opt-in custom order for the use cases discussed in #4317 and the previously closed requests #1610 and #2541.Configuration and behavior
{ "settings": { "orderingRules": { "memberKindOrder": ["enum", "field", "property", "constructor", "method"] } } }The equivalent EditorConfig key is
stylecop.ordering.memberKindOrder. JSON takes precedence over the complete EditorConfig list.kindfromelementOrderdisables kind checks. Higher-priority traits partition the groups.class; record structs and unions usestruct. Both event forms shareevent.Unknown or duplicate kinds produce SA0002, including values scoped to later source files. Validation bypasses cached fallback settings to avoid a race that could suppress the error. The status inventory lists SA1201 once despite its two message descriptors.
Safety tradeoff: configured fixes refuse moves across directives, known field dependencies, or another runtime initializer in the same static/instance initialization sequence. These cases retain the diagnostic and require manual correction rather than potentially changing program behavior. A safe insertion immediately after a blocking initializer is still permitted.
Validation
Release solution build succeeds with zero warnings. Focused ordering/settings suites passed on the oldest and newest compiler projects on both
net10.0andnet472. Following review fixes, focused ordering/localization runs pass with 205 tests per framework in the base project and 331 per framework in C# 15. All four documentation editions build, and all 24 documentation-localization checks pass, including inventory uniqueness.Regression tests reproduced unsafe initializer moves, missed later-file configuration errors, blocked safe gap insertion, duplicate inventory entries, and missed static/readonly checks before their fixes. Schema, configuration reference, migration table, rule documentation, and generated resources are updated. New diagnostic translations are provided for Russian and Chinese, and the three localized documentation editions are updated; these translations have not received native-speaker review.
Performance
Analysis remains syntax-only and linear over immediate type members, with cached configuration and no additional syntax registrations or semantic-model work. The original default analyzer loops remain unchanged. Release measurements use 200 types with 10 fields, properties, and methods each, invoking each callback 100 times per operation (20,000 type callbacks), with 8 warmups and 11 samples of 5 operations.
SA1201 default medians remain around 10-11 ms with no added measured callback allocations; configured partial ordering is around 6 ms. Default SA1204/SA1214 timings overlap their baselines, without added allocations. Correctly checking configured gaps adds work: SA1204 measured about 25.7 ms versus 15.5 ms and allocates 192 bytes per type for local kind state; SA1214 measured about 8.8 ms versus 6.1 ms without added allocations. These are isolated callback measurements, not an IDE-wide speedup claim.
SA0002 checks later trees for this setting once per compilation and validates each distinct raw order once. A 2,000-tree callback harness measured approximately 0.13 ms and 56 additional bytes for absent configuration, or 0.17 ms and 2.3 KB additional allocations for a shared configured order. Performance harnesses and raw results are retained as session artifacts outside the repository.