Skip to content

Commit ab77a8f

Browse files
Simplified PropertySummaryDocumentationAnalyzer by extracting common code to helper methods.
1 parent c16da77 commit ab77a8f

1 file changed

Lines changed: 30 additions & 58 deletions

File tree

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

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,7 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
231231
// Both getter and setter are visible.
232232
if (!prefixIsGetsOrSets)
233233
{
234-
diagnosticProperties.Add(ExpectedTextKey, startingTextGetsOrSets);
235-
236-
if (prefixIsGets)
237-
{
238-
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextGets.Length));
239-
}
240-
else if (prefixIsSets)
241-
{
242-
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextSets.Length));
243-
}
244-
245-
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextGetsOrSets));
234+
ReportSa1623(context, diagnosticLocation, diagnosticProperties, text, expectedStartingText: startingTextGetsOrSets, unexpectedStartingText1: startingTextGets, unexpectedStartingText2: startingTextSets);
246235
}
247236
}
248237
else if (setter != null)
@@ -252,20 +241,11 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
252241
{
253242
if (prefixIsGetsOrSets)
254243
{
255-
diagnosticProperties.Add(ExpectedTextKey, startingTextGets);
256-
diagnosticProperties.Add(TextToRemoveKey, startingTextGetsOrSets);
257-
context.ReportDiagnostic(Diagnostic.Create(SA1624Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), "get", startingTextGets));
244+
ReportSa1624(context, diagnosticLocation, diagnosticProperties, accessor: "get", expectedStartingText: startingTextGets, startingTextToRemove: startingTextGetsOrSets);
258245
}
259246
else
260247
{
261-
diagnosticProperties.Add(ExpectedTextKey, startingTextGets);
262-
263-
if (prefixIsSets)
264-
{
265-
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextSets.Length));
266-
}
267-
268-
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextGets));
248+
ReportSa1623(context, diagnosticLocation, diagnosticProperties, text, expectedStartingText: startingTextGets, unexpectedStartingText1: startingTextSets);
269249
}
270250
}
271251
}
@@ -274,18 +254,7 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
274254
// Getter exists and is visible. Setter does not exist.
275255
if (!prefixIsGets)
276256
{
277-
diagnosticProperties.Add(ExpectedTextKey, startingTextGets);
278-
279-
if (prefixIsSets)
280-
{
281-
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextSets.Length));
282-
}
283-
else if (prefixIsGetsOrSets)
284-
{
285-
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextGetsOrSets.Length));
286-
}
287-
288-
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextGets));
257+
ReportSa1623(context, diagnosticLocation, diagnosticProperties, text, expectedStartingText: startingTextGets, unexpectedStartingText1: startingTextSets, unexpectedStartingText2: startingTextGetsOrSets);
289258
}
290259
}
291260
}
@@ -298,20 +267,11 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
298267
{
299268
if (prefixIsGetsOrSets)
300269
{
301-
diagnosticProperties.Add(ExpectedTextKey, startingTextSets);
302-
diagnosticProperties.Add(TextToRemoveKey, startingTextGetsOrSets);
303-
context.ReportDiagnostic(Diagnostic.Create(SA1624Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), "set", startingTextSets));
270+
ReportSa1624(context, diagnosticLocation, diagnosticProperties, accessor: "set", expectedStartingText: startingTextSets, startingTextToRemove: startingTextGetsOrSets);
304271
}
305272
else
306273
{
307-
diagnosticProperties.Add(ExpectedTextKey, startingTextSets);
308-
309-
if (prefixIsGets)
310-
{
311-
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextGets.Length));
312-
}
313-
314-
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextSets));
274+
ReportSa1623(context, diagnosticLocation, diagnosticProperties, text, expectedStartingText: startingTextSets, unexpectedStartingText1: startingTextGets);
315275
}
316276
}
317277
}
@@ -320,21 +280,33 @@ private static void AnalyzeSummaryElement(SyntaxNodeAnalysisContext context, Xml
320280
// Setter exists and is visible. Getter does not exist.
321281
if (!prefixIsSets)
322282
{
323-
diagnosticProperties.Add(ExpectedTextKey, startingTextSets);
324-
325-
if (prefixIsGetsOrSets)
326-
{
327-
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextGetsOrSets.Length));
328-
}
329-
else if (prefixIsGets)
330-
{
331-
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, startingTextGets.Length));
332-
}
333-
334-
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), startingTextSets));
283+
ReportSa1623(context, diagnosticLocation, diagnosticProperties, text, expectedStartingText: startingTextSets, unexpectedStartingText1: startingTextGetsOrSets, unexpectedStartingText2: startingTextGets);
335284
}
336285
}
337286
}
338287
}
288+
289+
private static void ReportSa1623(SyntaxNodeAnalysisContext context, Location diagnosticLocation, ImmutableDictionary<string, string>.Builder diagnosticProperties, string text, string expectedStartingText, string unexpectedStartingText1, string unexpectedStartingText2 = null)
290+
{
291+
diagnosticProperties.Add(ExpectedTextKey, expectedStartingText);
292+
293+
if (text.StartsWith(unexpectedStartingText1, StringComparison.OrdinalIgnoreCase))
294+
{
295+
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, unexpectedStartingText1.Length));
296+
}
297+
else if ((unexpectedStartingText2 != null) && text.StartsWith(unexpectedStartingText2, StringComparison.OrdinalIgnoreCase))
298+
{
299+
diagnosticProperties.Add(TextToRemoveKey, text.Substring(0, unexpectedStartingText2.Length));
300+
}
301+
302+
context.ReportDiagnostic(Diagnostic.Create(SA1623Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), expectedStartingText));
303+
}
304+
305+
private static void ReportSa1624(SyntaxNodeAnalysisContext context, Location diagnosticLocation, ImmutableDictionary<string, string>.Builder diagnosticProperties, string accessor, string expectedStartingText, string startingTextToRemove)
306+
{
307+
diagnosticProperties.Add(ExpectedTextKey, expectedStartingText);
308+
diagnosticProperties.Add(TextToRemoveKey, startingTextToRemove);
309+
context.ReportDiagnostic(Diagnostic.Create(SA1624Descriptor, diagnosticLocation, diagnosticProperties.ToImmutable(), accessor, expectedStartingText));
310+
}
339311
}
340312
}

0 commit comments

Comments
 (0)