Repository navigation
C# 13: handle partial properties and indexers in documentation rules - #4148
Conversation
…in some cases From bjornhellander/NewStyleCopAnalyzers#99. Adapted while cherry-picking: dropped the fork's review notes file and the 'union' registrations, added the property and indexer registrations to the upstream compilation start actions, and kept upstream's SA1205 registration. Tests were merged into the existing upstream test classes and use ConfigureAwait(false), WorkItem(4021) and the upstream SA1201 message arguments. (cherry picked from commit 76c10906b2817e8fceee873c38a2864781b1eb8a)
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #4148 +/- ##
==========================================
+ Coverage 97.48% 97.51% +0.02%
==========================================
Files 1214 1223 +9
Lines 128311 130642 +2331
Branches 4209 4238 +29
==========================================
+ Hits 125087 127391 +2304
- Misses 2112 2126 +14
- Partials 1112 1125 +13 🚀 New features to boost your workflow:
|
| public async Task TestPartialPropertyWithoutDocumentationAsync() | ||
| { | ||
| var testCode = @" | ||
| /// <summary> | ||
| /// Some Documentation | ||
| /// </summary> | ||
| public partial class TypeName | ||
| { | ||
| public partial int [|MemberName|] { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Some Documentation | ||
| /// </summary> | ||
| public partial class TypeName | ||
| { | ||
| /// <summary> | ||
| /// Some Documentation | ||
| /// </summary> | ||
| public partial int MemberName | ||
| { | ||
| get => 0; | ||
| set { } | ||
| } | ||
| }"; |
There was a problem hiding this comment.
Do we have a test for the opposite, where the less-defined partial declaration has the docs and the more completely defined partial does not?
IMO it's crazy that both partials would be expected to have docs. Keeping them both, and in sync, seems onerous. IMO having either one documented should be adequate. And if both are documented, we should probably have an analyzer call out when they don't match exactly between xml elements that they both provide.
There was a problem hiding this comment.
Done: documenting either part is now enough for SA1601 on partial methods, properties and indexers (6ea9c6a, which adds the opposite-direction tests for all three), and on constructors and events in #4152 (f012d3d). #4149 matches this: an undocumented part gets no SA1600, and gets SA1601 only when no part is documented (tests in 532209b). Partial types are unchanged for now, and #4176 tracks them.
… and indexers An undocumented part of a partial member is no longer reported when another part of the same member is documented. The compiler uses the implementing declaration's documentation when present, and otherwise the defining declaration's. When no part is documented, each undocumented part is still reported. Partial types are unchanged. The other part is only looked up (via IMethodSymbol.PartialDefinitionPart / PartialImplementationPart, and the same properties on IPropertySymbol through a cached light-up accessor) after the current part is found to be undocumented.
…ents Extends the #4148 change to C# 14 partial constructors and events. The other part of an event is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart / PartialImplementationPart; constructors use IMethodSymbol's.
# Conflicts: # StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp13/DocumentationRules/SA1600CSharp13UnitTests.cs # StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp13/OrderingRules/SA1201CSharp13UnitTests.cs # StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp13/OrderingRules/SA1202CSharp13UnitTests.cs
…ents Extends the #4148 change to C# 14 partial constructors and events. The other part of an event is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart / PartialImplementationPart; constructors use IMethodSymbol's.
The C# 6 test project runs on Roslyn 1.3.2, whose analyzer driver does not run syntax node actions on the implementing declaration of a partial method, so only the defining declaration is reported there. Roslyn 2.0+ (C# 7 and later test projects) reports both parts.
…ents Extends the #4148 change to C# 14 partial constructors and events. The other part of an event is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart / PartialImplementationPart; constructors use IMethodSymbol's.
…ents Extends the #4148 change to C# 14 partial constructors and events. The other part of an event is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart / PartialImplementationPart; constructors use IMethodSymbol's.
…ents Extends the #4148 change to C# 14 partial constructors and events. The other part of an event is found through a cached light-up accessor for IEventSymbol.PartialDefinitionPart / PartialImplementationPart; constructors use IMethodSymbol's.
…ed part An undocumented part of a partial method, property or indexer whose other part is documented gets neither SA1600 (partial members are left to SA1601) nor SA1601 (one documented part is enough, #4148).
Part of #4045 (C# 13 audit), checklist item #4021 (partial properties and indexers).
Ported from @bjornhellander's 76c10906b (bjornhellander/NewStyleCopAnalyzers#99), cherry-picked with
-xand his authorship kept.Bugs: before this, C# 13 partial properties and indexers got none of the "partial element" treatment:
<content>tag wasn't accepted in place of<summary>, which is what these rules allow for partial types and methods.Fix:
PartialElementDocumentationSummaryBase(SA1605/SA1607) and SA1601 now register forPropertyDeclarationandIndexerDeclarationand handle members with thepartialmodifier. The declaring part (accessors with no bodies) is treated like a partial method definition, so<content>is accepted there.ElementDocumentationSummaryBase(SA1604/SA1606) now skips partial properties and indexers, the same way it already skips other partial elements.PartialDefinitionPart/PartialImplementationPart: directly onIMethodSymbol(available since Roslyn 1.2.1), and onIPropertySymbolthrough a new light-up accessor (IPropertySymbolExtensions, a static delegate created once, so no per-node reflection). The semantic lookup only runs after the current part is found to be undocumented, so documented members cost nothing extra. Partial types keep their current behavior (every part must be documented); SA1601: consider requiring documentation on only one part of a partial type #4176 tracks whether they should follow.<summary>) doesn't count as documenting the member, same as for a single part.LightupHelpers.SupportsCSharp7for that.Behavior changes
Tests
...DefinitionWithoutDocumentationAsync); that's the intended change above.New C# 13 tests (
[WorkItem(4021)]) for SA1600, SA1601, SA1604, SA1605, SA1606, SA1607 (including<include>documentation and expression-bodied / block-bodied implementing parts), SA1201, SA1202, SA1205 and SA1206. All tests for these rules pass locally on C# 12, 13 and 15.Adaptations: the fork also registers for C# 15
uniondeclarations; that's dropped here, since upstream doesn't have those registrations. Upstream's SA1205 registration is unchanged. Tests useConfigureAwait(false)and upstream's SA1201 message arguments. Björn left a few// TODO:notes in the code; I kept them as written for review.Conflicts: this PR and #4142 both change the registration block in
SA1601PartialElementsMustBeDocumented.cs, so whichever merges second needs a trivial merge.