Skip to content

Commit 0bdc1f8

Browse files
committed
Merge pull request #2138 from vweijsters/fix-2098
Fixed issue where PropertySummaryDocumentationAnalyzer would provide …
2 parents da9bc63 + 19c739d commit 0bdc1f8

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,55 @@ public int Property
176176
Assert.Empty(offeredFixes);
177177
}
178178

179+
/// <summary>
180+
/// Verifies that an incomplete summary tag (with only get or set, when get and set are needed) will be fixed
181+
/// correctly. Regression test for #2098.
182+
/// </summary>
183+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
184+
[Fact]
185+
public async Task IncompleteSummaryTagShouldBeFixedCorrectlyAsync()
186+
{
187+
var testCode = @"
188+
public class TestClass
189+
{
190+
/// <summary>
191+
/// Gets the first test value.
192+
/// </summary>
193+
public int TestProperty1 { get; set; }
194+
195+
/// <summary>
196+
/// Sets the seconds test value.
197+
/// </summary>
198+
public int TestProperty2 { get; set; }
199+
}
200+
";
201+
202+
var fixedTestCode = @"
203+
public class TestClass
204+
{
205+
/// <summary>
206+
/// Gets or sets the first test value.
207+
/// </summary>
208+
public int TestProperty1 { get; set; }
209+
210+
/// <summary>
211+
/// Gets or sets the seconds test value.
212+
/// </summary>
213+
public int TestProperty2 { get; set; }
214+
}
215+
";
216+
217+
DiagnosticResult[] expected =
218+
{
219+
this.CSharpDiagnostic(PropertySummaryDocumentationAnalyzer.SA1623Descriptor).WithLocation(7, 16).WithArguments("Gets or sets"),
220+
this.CSharpDiagnostic(PropertySummaryDocumentationAnalyzer.SA1623Descriptor).WithLocation(12, 16).WithArguments("Gets or sets"),
221+
};
222+
223+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
224+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
225+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
226+
}
227+
179228
/// <inheritdoc/>
180229
protected override CodeFixProvider GetCSharpCodeFixProvider()
181230
{

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/PropertySummaryDocumentationAnalyzer.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
119119
var textElement = summaryElement.Content.FirstOrDefault() as XmlTextSyntax;
120120
var text = textElement == null ? string.Empty : XmlCommentHelper.GetText(textElement, true).TrimStart();
121121

122-
bool startsWithGetOrSet = text.StartsWith(startingTextGetsOrSets, StringComparison.Ordinal);
122+
bool startsWithGetOrSet = text.StartsWith(startingTextGetsOrSets, StringComparison.OrdinalIgnoreCase);
123123
bool getterVisible, setterVisible;
124124
if (getter != null && setter != null)
125125
{
@@ -217,6 +217,16 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
217217
if (!startsWithGetOrSet)
218218
{
219219
diagnosticProperties.Add(ExpectedTextKey, startingTextGetsOrSets);
220+
221+
if (text.StartsWith(startingTextGets, StringComparison.OrdinalIgnoreCase))
222+
{
223+
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextGets.Length));
224+
}
225+
else if (text.StartsWith(startingTextSets, StringComparison.OrdinalIgnoreCase))
226+
{
227+
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextSets.Length));
228+
}
229+
220230
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextGetsOrSets));
221231
}
222232
}
@@ -228,7 +238,7 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
228238
diagnosticProperties.Add(TextToRemoveKey, startingTextGetsOrSets);
229239
context.ReportDiagnostic(Diagnostic.Create(SA1624Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), "get", startingTextGets));
230240
}
231-
else if (!text.StartsWith(startingTextGets, StringComparison.Ordinal))
241+
else if (!text.StartsWith(startingTextGets, StringComparison.OrdinalIgnoreCase))
232242
{
233243
diagnosticProperties.Add(ExpectedTextKey, startingTextGets);
234244
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextGets));
@@ -243,7 +253,7 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
243253
diagnosticProperties.Add(TextToRemoveKey, startingTextGetsOrSets);
244254
context.ReportDiagnostic(Diagnostic.Create(SA1624Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), "set", startingTextSets));
245255
}
246-
else if (!text.StartsWith(startingTextSets, StringComparison.Ordinal))
256+
else if (!text.StartsWith(startingTextSets, StringComparison.OrdinalIgnoreCase))
247257
{
248258
diagnosticProperties.Add(ExpectedTextKey, startingTextSets);
249259
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextSets));

0 commit comments

Comments
 (0)