Skip to content

Commit 7a9bb66

Browse files
committed
Fix SA1629 and SA1608 reporting for interfaces
1 parent 47762e5 commit 7a9bb66

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,19 @@ public async Task TestTypeWithoutContentDocumentationAsync(string typeName)
111111
await this.VerifyCSharpDiagnosticAsync(string.Format(testCode, typeName), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
112112
}
113113

114-
[Fact]
115-
public async Task TestClassWithDefaultDocumentationAsync()
114+
[Theory]
115+
[InlineData("class")]
116+
[InlineData("struct")]
117+
[InlineData("interface")]
118+
public async Task TestTypeWithDefaultDocumentationAsync(string typeName)
116119
{
117-
var testCode = @"
120+
var testCode = $@"
118121
/// <summary>
119122
/// Summary description for the ClassName class.
120123
/// </summary>
121-
public class ClassName
122-
{
123-
}";
124+
public {typeName} ClassName
125+
{{
126+
}}";
124127

125128
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(2, 5);
126129

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public interface ITest
314314

315315
var fixedTestCode = @"
316316
/// <summary>
317-
/// Test interface <see cref=""ITest""/>
317+
/// Test interface <see cref=""ITest""/>.
318318
/// </summary>
319319
public interface ITest
320320
{
@@ -325,7 +325,11 @@ public interface ITest
325325
}
326326
";
327327

328-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(8, 41);
328+
DiagnosticResult[] expected =
329+
{
330+
this.CSharpDiagnostic().WithLocation(3, 39),
331+
this.CSharpDiagnostic().WithLocation(8, 41),
332+
};
329333

330334
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
331335
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ internal abstract class ElementDocumentationBase : DiagnosticAnalyzer
3030
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> conversionOperatorDeclarationAction;
3131
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> classDeclarationAction;
3232
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> structDeclarationAction;
33+
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> interfaceDeclarationAction;
3334
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> enumDeclarationAction;
3435
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> fieldDeclarationAction;
3536
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> propertyDeclarationAction;
@@ -47,6 +48,7 @@ protected ElementDocumentationBase(bool inheritDocSuppressesWarnings, string mat
4748
this.conversionOperatorDeclarationAction = this.HandleConversionOperatorDeclaration;
4849
this.classDeclarationAction = this.HandleClassDeclaration;
4950
this.structDeclarationAction = this.HandleStructDeclaration;
51+
this.interfaceDeclarationAction = this.HandleInterfaceDeclaration;
5052
this.enumDeclarationAction = this.HandleEnumDeclaration;
5153
this.fieldDeclarationAction = this.HandleFieldDeclaration;
5254
this.propertyDeclarationAction = this.HandlePropertyDeclaration;
@@ -66,6 +68,7 @@ public override void Initialize(AnalysisContext context)
6668
context.RegisterSyntaxNodeAction(this.conversionOperatorDeclarationAction, SyntaxKind.ConversionOperatorDeclaration);
6769
context.RegisterSyntaxNodeAction(this.classDeclarationAction, SyntaxKind.ClassDeclaration);
6870
context.RegisterSyntaxNodeAction(this.structDeclarationAction, SyntaxKind.StructDeclaration);
71+
context.RegisterSyntaxNodeAction(this.interfaceDeclarationAction, SyntaxKind.InterfaceDeclaration);
6972
context.RegisterSyntaxNodeAction(this.enumDeclarationAction, SyntaxKind.EnumDeclaration);
7073
context.RegisterSyntaxNodeAction(this.fieldDeclarationAction, SyntaxKind.FieldDeclaration);
7174
context.RegisterSyntaxNodeAction(this.propertyDeclarationAction, SyntaxKind.PropertyDeclaration);
@@ -195,6 +198,16 @@ private void HandleStructDeclaration(SyntaxNodeAnalysisContext context, StyleCop
195198
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
196199
}
197200

201+
private void HandleInterfaceDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
202+
{
203+
var node = (InterfaceDeclarationSyntax)context.Node;
204+
205+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
206+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
207+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
208+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
209+
}
210+
198211
private void HandleEnumDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
199212
{
200213
var node = (EnumDeclarationSyntax)context.Node;

0 commit comments

Comments
 (0)