Skip to content

Commit 19c739d

Browse files
committed
Fixed issue where PropertySummaryDocumentationAnalyzer would provide incorrect summary text
1 parent 057eede commit 19c739d

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 imcomplete summary tag (with only get or set, when get and set are needed) will be fixed correctly.
181+
/// 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
@@ -121,7 +121,7 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
121121

122122
if (getter != null || expressionBody != null)
123123
{
124-
bool startsWithGetOrSet = text.StartsWith(startingTextGetsOrSets, StringComparison.Ordinal);
124+
bool startsWithGetOrSet = text.StartsWith(startingTextGetsOrSets, StringComparison.OrdinalIgnoreCase);
125125

126126
if (setter != null)
127127
{
@@ -210,7 +210,7 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
210210
diagnosticProperties.Add(TextToRemoveKey, startingTextGetsOrSets);
211211
context.ReportDiagnostic(Diagnostic.Create(SA1624Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), "get", startingTextGets));
212212
}
213-
else if (!text.StartsWith(startingTextGets, StringComparison.Ordinal))
213+
else if (!text.StartsWith(startingTextGets, StringComparison.OrdinalIgnoreCase))
214214
{
215215
diagnosticProperties.Add(ExpectedTextKey, startingTextGets);
216216
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextGets));
@@ -224,7 +224,7 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
224224
diagnosticProperties.Add(TextToRemoveKey, startingTextGetsOrSets);
225225
context.ReportDiagnostic(Diagnostic.Create(SA1624Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), "set", startingTextSets));
226226
}
227-
else if (!text.StartsWith(startingTextSets, StringComparison.Ordinal))
227+
else if (!text.StartsWith(startingTextSets, StringComparison.OrdinalIgnoreCase))
228228
{
229229
diagnosticProperties.Add(ExpectedTextKey, startingTextSets);
230230
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextSets));
@@ -235,6 +235,16 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
235235
if (!startsWithGetOrSet)
236236
{
237237
diagnosticProperties.Add(ExpectedTextKey, startingTextGetsOrSets);
238+
239+
if (text.StartsWith(startingTextGets, StringComparison.OrdinalIgnoreCase))
240+
{
241+
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextGets.Length));
242+
}
243+
else if (text.StartsWith(startingTextSets, StringComparison.OrdinalIgnoreCase))
244+
{
245+
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextSets.Length));
246+
}
247+
238248
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextGetsOrSets));
239249
}
240250
}

0 commit comments

Comments
 (0)