Skip to content

Commit

Permalink
Merge pull request #3696 from bjornhellander/feature/sa1642-records
Browse files Browse the repository at this point in the history
Update SA1642 and its code fix to handle record structs correctly
  • Loading branch information
sharwell committed Sep 27, 2023
2 parents af76eb5 + 472780c commit d1c94ed
Show file tree
Hide file tree
Showing 3 changed files with 196 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 d1c94ed

Please sign in to comment.