Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1600 to also handle records #3725

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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