Skip to content

Commit

Permalink
Update SA1642 and its code fix to handle record structs correctly. Al…
Browse files Browse the repository at this point in the history
…so add tests for records and record classes.

#3518
  • Loading branch information
bjornhellander committed Sep 16, 2023
1 parent 890236e commit 9ac20da
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Globalization;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
Expand All @@ -20,6 +20,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;

/// <summary>
/// Implements a code fix for <see cref="SA1642ConstructorSummaryDocumentationMustBeginWithStandardText"/>
Expand Down Expand Up @@ -83,7 +84,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

internal static ImmutableArray<string> GenerateStandardText(Document document, BaseMethodDeclarationSyntax methodDeclaration, BaseTypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
{
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration) || typeDeclaration.IsKind(SyntaxKindEx.RecordStructDeclaration);
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(methodDeclaration.SyntaxTree, cancellationToken);
var culture = settings.DocumentationRules.DocumentationCultureInfo;
var resourceManager = DocumentationResources.ResourceManager;
Expand Down Expand Up @@ -147,7 +148,19 @@ private static TypeParameterListSyntax GetTypeParameterList(BaseTypeDeclarationS
return classDeclaration.TypeParameterList;
}

return (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
if (typeDeclaration is StructDeclarationSyntax structDeclaration)
{
return structDeclaration.TypeParameterList;
}

if (RecordDeclarationSyntaxWrapper.IsInstance(typeDeclaration))
{
var recordDeclaration = (RecordDeclarationSyntaxWrapper)typeDeclaration;
return recordDeclaration.TypeParameterList;
}

Debug.Assert(false, $"Unhandled type {typeDeclaration.Kind()}");
return null;
}

private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlElementSyntax node, CancellationToken cancellationToken)
Expand Down Expand Up @@ -202,21 +215,10 @@ private static bool IsMultiLine(XmlElementSyntax node)
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlEmptyElementSyntax node)
{
var typeDeclaration = node.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>();

TypeParameterListSyntax typeParameterList;
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
{
typeParameterList = classDeclaration.TypeParameterList;
}
else
{
typeParameterList = (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
}
var typeParameterList = GetTypeParameterList(typeDeclaration);

var newRoot = root.ReplaceNode(node, BuildSeeElement(typeDeclaration.Identifier, typeParameterList));

var newDocument = document.WithSyntaxRoot(newRoot);

return Task.FromResult(newDocument);
}

Expand Down

0 comments on commit 9ac20da

Please sign in to comment.