Skip to content

Commit

Permalink
Correct SA1629 to add a period if the documentation ends in an xml en…
Browse files Browse the repository at this point in the history
…tity. Previously replaced the semicolon in the xml entity. #3802
  • Loading branch information
bjornhellander committed Jun 6, 2024
1 parent de67e30 commit cc18acf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.DocumentationRules
{
using System.Threading;
Expand Down Expand Up @@ -962,10 +960,32 @@ public interface ITest
await VerifyCSharpDiagnosticAsync(testCode, testSettings, expectedResult, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[InlineData("<")]
[InlineData("&")]
[InlineData(""")]
[WorkItem(3802, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3802")]
public async Task TestSentenceEndingWithXmlEntityAsync(string xmlEntity)
{
var testCode = $@"
/// <summary>Something {xmlEntity}[|<|]/summary>
public class TestClass
{{
}}";

var fixedTestCode = $@"
/// <summary>Something {xmlEntity}.</summary>
public class TestClass
{{
}}";

await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
}

private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, expected, cancellationToken);

private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
private static Task VerifyCSharpDiagnosticAsync(string source, string? testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
{
var test = CreateTest(testSettings, expected);
test.TestCode = source;
Expand All @@ -985,7 +1005,7 @@ private static Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expec
return test.RunAsync(cancellationToken);
}

private static StyleCopCodeFixVerifier<SA1629DocumentationTextMustEndWithAPeriod, SA1629CodeFixProvider>.CSharpTest CreateTest(string testSettings, DiagnosticResult[] expected)
private static StyleCopCodeFixVerifier<SA1629DocumentationTextMustEndWithAPeriod, SA1629CodeFixProvider>.CSharpTest CreateTest(string? testSettings, DiagnosticResult[] expected)
{
string contentClassInheritDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<TestClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -66,6 +67,8 @@ internal class SA1629DocumentationTextMustEndWithAPeriod : ElementDocumentationB

private static readonly ImmutableDictionary<string, string> NoCodeFixProperties = ImmutableDictionary.Create<string, string>().Add(NoCodeFixKey, string.Empty);

private static readonly Regex XmlEntityRegex = new Regex("&[a-z]+;$");

/// <summary>
/// Initializes a new instance of the <see cref="SA1629DocumentationTextMustEndWithAPeriod"/> class.
/// </summary>
Expand Down Expand Up @@ -131,8 +134,9 @@ private static void HandleSectionOrBlockXmlElement(SyntaxNodeAnalysisContext con
{
int spanStart = textToken.SpanStart + textWithoutTrailingWhitespace.Length;
ImmutableDictionary<string, string> properties = null;
if (textWithoutTrailingWhitespace.EndsWith(",", StringComparison.Ordinal)
|| textWithoutTrailingWhitespace.EndsWith(";", StringComparison.Ordinal))
if (textWithoutTrailingWhitespace.EndsWith(",", StringComparison.Ordinal) ||
(textWithoutTrailingWhitespace.EndsWith(";", StringComparison.Ordinal) &&
!XmlEntityRegex.IsMatch(textWithoutTrailingWhitespace)))
{
spanStart -= 1;
SetReplaceChar();
Expand Down

0 comments on commit cc18acf

Please sign in to comment.