Skip to content

Commit 3c893e7

Browse files
authored
Merge pull request #3137 from VitaliAntonov/issue-2950
SA1629 should not ignore enum members
2 parents b4d0192 + fce9296 commit 3c893e7

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1608UnitTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,48 @@ public class ClassName
146146
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
147147
}
148148

149+
[Fact]
150+
public async Task TestEnumMemberWithContentDocumentationAsync()
151+
{
152+
var testCode = @"
153+
public enum EnumName
154+
{
155+
/// <summary>
156+
/// Foo.
157+
/// </summary>
158+
EnumMember1 = 0,
159+
}
160+
";
161+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
162+
}
163+
164+
[Fact]
165+
public async Task TestEnumMemberWithDefaultDocumentationAsync()
166+
{
167+
var testCode = @"
168+
public enum EnumName
169+
{
170+
/// <summary>
171+
/// Summary description for the EnumMember1 enum member.
172+
/// </summary>
173+
EnumMember1 = 0,
174+
175+
/// <summary>
176+
/// Summary description
177+
/// for the EnumMember2 enum member.
178+
/// </summary>
179+
EnumMember2 = 1,
180+
}
181+
";
182+
DiagnosticResult[] expected = new[]
183+
{
184+
Diagnostic().WithLocation(4, 9),
185+
Diagnostic().WithLocation(9, 9),
186+
};
187+
188+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
189+
}
190+
149191
[Fact]
150192
public async Task TestClassWithIncludedEmptyDocumentationAsync()
151193
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1625UnitTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ public class TestClass
273273
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
274274
}
275275

276+
[Fact]
277+
public async Task VerifyThatDuplicatedDocumentationForEnumMemberDoesReportADiagnosticAsync()
278+
{
279+
var testCode = @"
280+
public enum EnumName
281+
{
282+
/// <summary>
283+
/// Some documentation.
284+
/// </summary>
285+
/// <remark>Some documentation.</remark>
286+
EnumMember = 0,
287+
}";
288+
var expected = Diagnostic().WithLocation(7, 9);
289+
290+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
291+
}
292+
276293
[Fact]
277294
public async Task VerifyThatCorrectIncludedDocumentationDoesNotReportADiagnosticAsync()
278295
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1629UnitTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ public int TestMethod2<T>(T arg1)
172172
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
173173
}
174174

175+
[Fact]
176+
[WorkItem(2950, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2950")]
177+
public async Task TestEnumMemberAsync()
178+
{
179+
var testCode = @"
180+
public enum TestEnum
181+
{
182+
/// <summary>
183+
/// Enum member
184+
/// </summary>
185+
EnumMember = 0,
186+
}
187+
";
188+
189+
var fixedTestCode = @"
190+
public enum TestEnum
191+
{
192+
/// <summary>
193+
/// Enum member.
194+
/// </summary>
195+
EnumMember = 0,
196+
}
197+
";
198+
199+
var expected = Diagnostic().WithLocation(5, 20);
200+
201+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
202+
}
203+
175204
[Fact]
176205
public async Task TestAugmentedInheritedDocumentationAsync()
177206
{

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/ElementDocumentationBase.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal abstract class ElementDocumentationBase : DiagnosticAnalyzer
3131
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> baseTypeDeclarationAction;
3232
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> fieldDeclarationAction;
3333
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> propertyDeclarationAction;
34+
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> enumMemberDeclarationAction;
3435

3536
protected ElementDocumentationBase(bool inheritDocSuppressesWarnings, string matchElementName = null)
3637
{
@@ -46,6 +47,7 @@ protected ElementDocumentationBase(bool inheritDocSuppressesWarnings, string mat
4647
this.baseTypeDeclarationAction = this.HandleBaseTypeDeclaration;
4748
this.fieldDeclarationAction = this.HandleFieldDeclaration;
4849
this.propertyDeclarationAction = this.HandlePropertyDeclaration;
50+
this.enumMemberDeclarationAction = this.HandleEnumMemberDeclaration;
4951
}
5052

5153
/// <inheritdoc/>
@@ -63,6 +65,7 @@ public override void Initialize(AnalysisContext context)
6365
context.RegisterSyntaxNodeAction(this.baseTypeDeclarationAction, SyntaxKinds.BaseTypeDeclaration);
6466
context.RegisterSyntaxNodeAction(this.fieldDeclarationAction, SyntaxKind.FieldDeclaration);
6567
context.RegisterSyntaxNodeAction(this.propertyDeclarationAction, SyntaxKind.PropertyDeclaration);
68+
context.RegisterSyntaxNodeAction(this.enumMemberDeclarationAction, SyntaxKind.EnumMemberDeclaration);
6669
}
6770

6871
/// <summary>
@@ -200,6 +203,16 @@ private void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context, StyleC
200203
this.HandleDeclaration(context, settings, needsComment, node, node.Identifier.GetLocation());
201204
}
202205

206+
private void HandleEnumMemberDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
207+
{
208+
var node = (EnumMemberDeclarationSyntax)context.Node;
209+
210+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility();
211+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
212+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
213+
this.HandleDeclaration(context, settings, needsComment, node, node.Identifier.GetLocation());
214+
}
215+
203216
private void HandleDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings, bool needsComment, SyntaxNode node, params Location[] locations)
204217
{
205218
var documentation = node.GetDocumentationCommentTriviaSyntax();

0 commit comments

Comments
 (0)