Skip to content

Commit cc18acf

Browse files
Correct SA1629 to add a period if the documentation ends in an xml entity. Previously replaced the semicolon in the xml entity. #3802
1 parent de67e30 commit cc18acf

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1629UnitTests.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.DocumentationRules
75
{
86
using System.Threading;
@@ -962,10 +960,32 @@ public interface ITest
962960
await VerifyCSharpDiagnosticAsync(testCode, testSettings, expectedResult, CancellationToken.None).ConfigureAwait(false);
963961
}
964962

963+
[Theory]
964+
[InlineData("<")]
965+
[InlineData("&")]
966+
[InlineData(""")]
967+
[WorkItem(3802, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3802")]
968+
public async Task TestSentenceEndingWithXmlEntityAsync(string xmlEntity)
969+
{
970+
var testCode = $@"
971+
/// <summary>Something {xmlEntity}[|<|]/summary>
972+
public class TestClass
973+
{{
974+
}}";
975+
976+
var fixedTestCode = $@"
977+
/// <summary>Something {xmlEntity}.</summary>
978+
public class TestClass
979+
{{
980+
}}";
981+
982+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
983+
}
984+
965985
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
966986
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, expected, cancellationToken);
967987

968-
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
988+
private static Task VerifyCSharpDiagnosticAsync(string source, string? testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
969989
{
970990
var test = CreateTest(testSettings, expected);
971991
test.TestCode = source;
@@ -985,7 +1005,7 @@ private static Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expec
9851005
return test.RunAsync(cancellationToken);
9861006
}
9871007

988-
private static StyleCopCodeFixVerifier<SA1629DocumentationTextMustEndWithAPeriod, SA1629CodeFixProvider>.CSharpTest CreateTest(string testSettings, DiagnosticResult[] expected)
1008+
private static StyleCopCodeFixVerifier<SA1629DocumentationTextMustEndWithAPeriod, SA1629CodeFixProvider>.CSharpTest CreateTest(string? testSettings, DiagnosticResult[] expected)
9891009
{
9901010
string contentClassInheritDoc = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
9911011
<TestClass>

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1629DocumentationTextMustEndWithAPeriod.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.DocumentationRules
99
using System.Collections.Generic;
1010
using System.Collections.Immutable;
1111
using System.Linq;
12+
using System.Text.RegularExpressions;
1213
using System.Xml.Linq;
1314
using Microsoft.CodeAnalysis;
1415
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -66,6 +67,8 @@ internal class SA1629DocumentationTextMustEndWithAPeriod : ElementDocumentationB
6667

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

70+
private static readonly Regex XmlEntityRegex = new Regex("&[a-z]+;$");
71+
6972
/// <summary>
7073
/// Initializes a new instance of the <see cref="SA1629DocumentationTextMustEndWithAPeriod"/> class.
7174
/// </summary>
@@ -131,8 +134,9 @@ private static void HandleSectionOrBlockXmlElement(SyntaxNodeAnalysisContext con
131134
{
132135
int spanStart = textToken.SpanStart + textWithoutTrailingWhitespace.Length;
133136
ImmutableDictionary<string, string> properties = null;
134-
if (textWithoutTrailingWhitespace.EndsWith(",", StringComparison.Ordinal)
135-
|| textWithoutTrailingWhitespace.EndsWith(";", StringComparison.Ordinal))
137+
if (textWithoutTrailingWhitespace.EndsWith(",", StringComparison.Ordinal) ||
138+
(textWithoutTrailingWhitespace.EndsWith(";", StringComparison.Ordinal) &&
139+
!XmlEntityRegex.IsMatch(textWithoutTrailingWhitespace)))
136140
{
137141
spanStart -= 1;
138142
SetReplaceChar();

0 commit comments

Comments
 (0)