Skip to content

Commit 733cd70

Browse files
committed
When applying SA1623 code fix on empty <summary> tag, "Gets the value" will be the default text inside the <summary>
1 parent d5f07d4 commit 733cd70

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/PropertySummaryDocumentationCodeFixProvider.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,18 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
6262
var textElement = (XmlTextSyntax)summaryElement.Content.FirstOrDefault();
6363
if (textElement == null)
6464
{
65-
return document;
65+
var newTextToken = SyntaxFactory.XmlTextLiteral(SyntaxTriviaList.Empty, "the value", "the value", SyntaxTriviaList.Empty);
66+
textElement = SyntaxFactory.XmlText(SyntaxTokenList.Create(newTextToken));
67+
var updatedReturns = summaryElement.WithContent(XmlSyntaxFactory.List(textElement));
68+
syntaxRoot = syntaxRoot.ReplaceNode(summaryElement, updatedReturns);
6669
}
6770

6871
var textToken = textElement.TextTokens.First(token => token.IsKind(SyntaxKind.XmlTextLiteralToken));
6972
var text = textToken.ValueText;
7073

7174
// preserve leading whitespace
7275
int index = 0;
73-
while (char.IsWhiteSpace(text, index))
76+
while (text.Length > index && char.IsWhiteSpace(text, index))
7477
{
7578
index++;
7679
}
@@ -89,14 +92,18 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
8992
modifiedText = text.Substring(index);
9093
}
9194

92-
modifiedText = char.ToLowerInvariant(modifiedText[0]) + modifiedText.Substring(1);
95+
if (modifiedText.Length > 0)
96+
{
97+
modifiedText = char.ToLowerInvariant(modifiedText[0]) + modifiedText.Substring(1);
98+
}
9399

94100
// create the new text string
95101
var textToAdd = diagnostic.Properties[PropertySummaryDocumentationAnalyzer.ExpectedTextKey];
96102
var newText = $"{preservedWhitespace}{textToAdd} {modifiedText}";
97103

98104
// replace the token
99-
var newTextTokens = textElement.TextTokens.Replace(textToken, SyntaxFactory.XmlTextLiteral(textToken.LeadingTrivia, newText, newText, textToken.TrailingTrivia));
105+
var newXmlTextLiteral = SyntaxFactory.XmlTextLiteral(textToken.LeadingTrivia, newText, newText, textToken.TrailingTrivia);
106+
var newTextTokens = textElement.TextTokens.Replace(textToken, newXmlTextLiteral);
100107
var newTextElement = textElement.WithTextTokens(newTextTokens);
101108

102109
var newSyntaxRoot = syntaxRoot.ReplaceNode(textElement, newTextElement);

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1623UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public int Property
171171

172172
var fixedTestCode = @"public class ClassName
173173
{
174-
/// <summary>Gets Property</summary>
174+
/// <summary>Gets the value</summary>
175175
public int Property
176176
{
177177
get;

0 commit comments

Comments
 (0)