Skip to content

Commit

Permalink
Merge pull request #3725 from bjornhellander/feature/sa1600-records
Browse files Browse the repository at this point in the history
Update SA1600 to also handle records
  • Loading branch information
sharwell committed Nov 20, 2023
2 parents b029a8e + f18c68a commit c0246ab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.DocumentationRules;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.DocumentationRules.SA1600ElementsMustBeDocumented,
Expand Down Expand Up @@ -40,52 +41,19 @@ public async Task TestRegressionMethodGlobalNamespaceAsync(string code)
await VerifyCSharpDiagnosticAsync(this.LanguageVersion, testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestClassWithoutDocumentationAsync()
{
await this.TestTypeWithoutDocumentationAsync("class", false).ConfigureAwait(false);
}

[Fact]
public async Task TestStructWithoutDocumentationAsync()
{
await this.TestTypeWithoutDocumentationAsync("struct", false).ConfigureAwait(false);
}

[Fact]
public async Task TestEnumWithoutDocumentationAsync()
{
await this.TestTypeWithoutDocumentationAsync("enum", false).ConfigureAwait(false);
}

[Fact]
public async Task TestInterfaceWithoutDocumentationAsync()
{
await this.TestTypeWithoutDocumentationAsync("interface", true).ConfigureAwait(false);
}

[Fact]
public async Task TestClassWithDocumentationAsync()
{
await this.TestTypeWithDocumentationAsync("class").ConfigureAwait(false);
}

[Fact]
public async Task TestStructWithDocumentationAsync()
{
await this.TestTypeWithDocumentationAsync("struct").ConfigureAwait(false);
}

[Fact]
public async Task TestEnumWithDocumentationAsync()
[Theory]
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
public async Task TestBaseTypeWithoutDocumentationAsync(string type)
{
await this.TestTypeWithDocumentationAsync("enum").ConfigureAwait(false);
var isInterface = type == "interface";
await this.TestTypeWithoutDocumentationAsync(type, isInterface).ConfigureAwait(false);
}

[Fact]
public async Task TestInterfaceWithDocumentationAsync()
[Theory]
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
public async Task TestBaseTypeWithDocumentationAsync(string type)
{
await this.TestTypeWithDocumentationAsync("interface").ConfigureAwait(false);
await this.TestTypeWithDocumentationAsync(type).ConfigureAwait(false);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;

/// <summary>
Expand All @@ -24,7 +25,7 @@ namespace StyleCop.Analyzers.DocumentationRules
///
/// <para>A violation of this rule occurs if an element is completely missing a documentation header, or if the
/// header is empty. In C# the following types of elements can have documentation headers: classes, constructors,
/// delegates, enums, events, finalizers, indexers, interfaces, methods, properties, and structs.</para>
/// delegates, enums, events, finalizers, indexers, interfaces, methods, properties, records, and structs.</para>
/// </remarks>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class SA1600ElementsMustBeDocumented : DiagnosticAnalyzer
Expand All @@ -41,9 +42,6 @@ internal class SA1600ElementsMustBeDocumented : DiagnosticAnalyzer
private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.DocumentationRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly ImmutableArray<SyntaxKind> BaseTypeDeclarationKinds =
ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.EnumDeclaration);

private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> BaseTypeDeclarationAction = Analyzer.HandleBaseTypeDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> MethodDeclarationAction = Analyzer.HandleMethodDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> ConstructorDeclarationAction = Analyzer.HandleConstructorDeclaration;
Expand Down Expand Up @@ -114,7 +112,7 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(context =>
{
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, SyntaxKinds.BaseTypeDeclaration);
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
context.RegisterSyntaxNodeAction(ConstructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
context.RegisterSyntaxNodeAction(DestructorDeclarationAction, SyntaxKind.DestructorDeclaration);
Expand Down
2 changes: 1 addition & 1 deletion documentation/SA1600.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ A C# code element is missing a documentation header.

C# syntax provides a mechanism for inserting documentation for classes and elements directly into the code, through the use of Xml documentation headers. For an introduction to these headers and a description of the header syntax, see the following article: [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/xml-documentation-comments](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/xml-documentation-comments).

A violation of this rule occurs if an element is completely missing a documentation header, or if the header is empty. In C# the following types of elements can have documentation headers: classes, constructors, delegates, enums, events, finalizers, indexers, interfaces, methods, properties, and structs.
A violation of this rule occurs if an element is completely missing a documentation header, or if the header is empty. In C# the following types of elements can have documentation headers: classes, constructors, delegates, enums, events, finalizers, indexers, interfaces, methods, properties, records, and structs.

## How to fix violations

Expand Down

0 comments on commit c0246ab

Please sign in to comment.