Skip to content

Commit ef91285

Browse files
Copilotmeziantou
andauthored
fix: narrow MA0211 to summary comments
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
1 parent d70bbeb commit ef91285

11 files changed

Lines changed: 79 additions & 100 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ If you are already using other analyzers, you can check [which rules are duplica
229229
|[MA0208](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0208.md)|Usage|\[FixedAddressValueType\] fields must be value types|⚠️|✔️||
230230
|[MA0209](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0209.md)|Performance|Use in keyword for in parameter|ℹ️||✔️|
231231
|[MA0210](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0210.md)|Performance|Use in keyword to call the in overload|ℹ️||✔️|
232-
|[MA0211](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0211.md)|Style|Use multi-line XML comment syntax|ℹ️||✔️|
232+
|[MA0211](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0211.md)|Style|Use multi-line syntax for XML summary comments|ℹ️||✔️|
233233

234234
<!-- rules -->
235235

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
|[MA0208](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0208.md)|Usage|\[FixedAddressValueType\] fields must be value types|<span title='Warning'>⚠️</span>|✔️||
210210
|[MA0209](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0209.md)|Performance|Use in keyword for in parameter|<span title='Info'>ℹ️</span>||✔️|
211211
|[MA0210](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0210.md)|Performance|Use in keyword to call the in overload|<span title='Info'>ℹ️</span>||✔️|
212-
|[MA0211](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0211.md)|Style|Use multi-line XML comment syntax|<span title='Info'>ℹ️</span>||✔️|
212+
|[MA0211](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0211.md)|Style|Use multi-line syntax for XML summary comments|<span title='Info'>ℹ️</span>||✔️|
213213

214214
|Id|Suppressed rule|Justification|
215215
|--|---------------|-------------|

docs/Rules/MA0211.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# MA0211 - Use multi-line XML comment syntax
1+
# MA0211 - Use multi-line syntax for XML summary comments
22
<!-- sources -->
33
Sources: [UseMultiLineXmlCommentSyntaxAnalyzer.cs](https://github.com/meziantou/Meziantou.Analyzer/blob/main/src/Meziantou.Analyzer/Rules/UseMultiLineXmlCommentSyntaxAnalyzer.cs), [UseMultiLineXmlCommentSyntaxFixer.cs](https://github.com/meziantou/Meziantou.Analyzer/blob/main/src/Meziantou.Analyzer.CodeFixers/Rules/UseMultiLineXmlCommentSyntaxFixer.cs)
44
<!-- sources -->
55

6-
This rule reports XML documentation comments written on a single line when they can be expanded to the standard multi-line XML comment form.
6+
This rule reports single-line `<summary>` XML documentation comments when they contain non-empty content and can be expanded to the standard multi-line XML comment form.
77

88
````csharp
99
/// <summary>Description</summary>
@@ -18,16 +18,15 @@ public class Sample { }
1818

1919
## When to use multi-line format
2020

21-
Use this rule when you want XML documentation comments to consistently use the multi-line form, even when the content fits on a single line.
21+
Use this rule when you want `<summary>` XML documentation comments to consistently use the multi-line form, even when the content fits on a single line.
2222

2323
## When NOT to use multi-line format
2424

2525
The analyzer will NOT suggest converting when:
2626

27-
- The XML element already spans multiple lines
28-
- The element contains multiple lines of source content
29-
- The element contains CDATA sections
30-
- The element contains nested XML elements (like `<c>`, `<see>`, etc.)
27+
- The `<summary>` element already spans multiple lines
28+
- The `<summary>` element is empty or contains only whitespace
29+
- The XML documentation element is not a `<summary>` element
3130

3231
## Examples
3332

@@ -42,10 +41,11 @@ public int Add(int a, int b) => a + b;
4241
/// </summary>
4342
public int Add(int a, int b) => a + b;
4443

45-
// Compliant: Multiple lines of source content
46-
/// <summary>
47-
/// Returns the sum of two numbers.
48-
/// This method handles integer overflow.
49-
/// </summary>
44+
// Compliant: Empty summary
45+
/// <summary></summary>
5046
public int Add(int a, int b) => a + b;
47+
48+
// Compliant: Other XML documentation elements are ignored
49+
/// <param name="value">The value</param>
50+
public void SetValue(int value) { }
5151
````

src/Meziantou.Analyzer.CodeFixers/Rules/UseMultiLineXmlCommentSyntaxFixer.cs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System.Collections.Immutable;
22
using System.Composition;
3-
using System.Text;
43
using Microsoft.CodeAnalysis;
54
using Microsoft.CodeAnalysis.CodeActions;
65
using Microsoft.CodeAnalysis.CodeFixes;
7-
using Microsoft.CodeAnalysis.CSharp;
86
using Microsoft.CodeAnalysis.CSharp.Syntax;
97
using Microsoft.CodeAnalysis.Text;
108

@@ -24,11 +22,14 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
2422
if (nodeToFix is not XmlElementSyntax elementSyntax)
2523
return;
2624

25+
if (!string.Equals(elementSyntax.StartTag.Name.LocalName.ValueText, "summary", StringComparison.Ordinal))
26+
return;
27+
2728
var sourceText = await context.Document.GetTextAsync(context.CancellationToken).ConfigureAwait(false);
2829
if (!TryCreateReplacementText(sourceText, elementSyntax, out var replacementText))
2930
return;
3031

31-
var title = "Use multi-line XML comment syntax";
32+
var title = "Use multi-line syntax for XML summary comments";
3233
var codeAction = CodeAction.Create(
3334
title,
3435
cancellationToken => FixAsync(context.Document, elementSyntax.Span, replacementText, cancellationToken),
@@ -53,32 +54,13 @@ private static bool TryCreateReplacementText(SourceText sourceText, XmlElementSy
5354
lineBreak = "\n";
5455
}
5556

56-
var contentText = new StringBuilder();
57-
foreach (var content in elementSyntax.Content)
58-
{
59-
if (content is not XmlTextSyntax textSyntax)
60-
continue;
61-
62-
foreach (var token in textSyntax.TextTokens)
63-
{
64-
var text = token.Text.Trim();
65-
if (string.IsNullOrWhiteSpace(text))
66-
continue;
67-
68-
if (contentText.Length > 0)
69-
{
70-
contentText.Append(' ');
71-
}
72-
73-
contentText.Append(text);
74-
}
75-
}
57+
var contentText = string.Concat(elementSyntax.Content.Select(static content => content.ToFullString())).Trim();
58+
if (contentText.Length == 0)
59+
return false;
7660

7761
var startTagText = elementSyntax.StartTag.ToString();
7862
var endTagText = elementSyntax.EndTag.ToString();
79-
replacementText = contentText.Length == 0
80-
? startTagText + lineBreak + commentPrefix + endTagText
81-
: startTagText + lineBreak + commentPrefix + contentText + lineBreak + commentPrefix + endTagText;
63+
replacementText = startTagText + lineBreak + commentPrefix + contentText + lineBreak + commentPrefix + endTagText;
8264
return true;
8365
}
8466

src/Meziantou.Analyzer.Pack/configuration/all-errors.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,5 +626,5 @@ dotnet_diagnostic.MA0209.severity = error
626626
# MA0210: Use in keyword to call the in overload
627627
dotnet_diagnostic.MA0210.severity = error
628628

629-
# MA0211: Use multi-line XML comment syntax
629+
# MA0211: Use multi-line syntax for XML summary comments
630630
dotnet_diagnostic.MA0211.severity = error

src/Meziantou.Analyzer.Pack/configuration/all-suggestions.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,5 +626,5 @@ dotnet_diagnostic.MA0209.severity = suggestion
626626
# MA0210: Use in keyword to call the in overload
627627
dotnet_diagnostic.MA0210.severity = suggestion
628628

629-
# MA0211: Use multi-line XML comment syntax
629+
# MA0211: Use multi-line syntax for XML summary comments
630630
dotnet_diagnostic.MA0211.severity = suggestion

src/Meziantou.Analyzer.Pack/configuration/all-warnings.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,5 +626,5 @@ dotnet_diagnostic.MA0209.severity = warning
626626
# MA0210: Use in keyword to call the in overload
627627
dotnet_diagnostic.MA0210.severity = warning
628628

629-
# MA0211: Use multi-line XML comment syntax
629+
# MA0211: Use multi-line syntax for XML summary comments
630630
dotnet_diagnostic.MA0211.severity = warning

src/Meziantou.Analyzer.Pack/configuration/default.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,5 +626,5 @@ dotnet_diagnostic.MA0209.severity = none
626626
# MA0210: Use in keyword to call the in overload
627627
dotnet_diagnostic.MA0210.severity = none
628628

629-
# MA0211: Use multi-line XML comment syntax
629+
# MA0211: Use multi-line syntax for XML summary comments
630630
dotnet_diagnostic.MA0211.severity = none

src/Meziantou.Analyzer.Pack/configuration/none.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,5 +626,5 @@ dotnet_diagnostic.MA0209.severity = none
626626
# MA0210: Use in keyword to call the in overload
627627
dotnet_diagnostic.MA0210.severity = none
628628

629-
# MA0211: Use multi-line XML comment syntax
629+
# MA0211: Use multi-line syntax for XML summary comments
630630
dotnet_diagnostic.MA0211.severity = none

src/Meziantou.Analyzer/Rules/UseMultiLineXmlCommentSyntaxAnalyzer.cs

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Immutable;
22
using Microsoft.CodeAnalysis;
3-
using Microsoft.CodeAnalysis.CSharp;
43
using Microsoft.CodeAnalysis.CSharp.Syntax;
54
using Microsoft.CodeAnalysis.Diagnostics;
65

@@ -11,12 +10,12 @@ public sealed class UseMultiLineXmlCommentSyntaxAnalyzer : DiagnosticAnalyzer
1110
{
1211
private static readonly DiagnosticDescriptor Rule = new(
1312
RuleIdentifiers.UseMultiLineXmlCommentSyntax,
14-
title: "Use multi-line XML comment syntax",
15-
messageFormat: "Use multi-line XML comment syntax",
13+
title: "Use multi-line syntax for XML summary comments",
14+
messageFormat: "Use multi-line syntax for XML summary comments",
1615
RuleCategories.Style,
1716
DiagnosticSeverity.Info,
1817
isEnabledByDefault: false,
19-
description: "Enforce multi-line XML documentation comment syntax for consistency.",
18+
description: "Enforce multi-line XML documentation comment syntax for <summary> elements.",
2019
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.UseMultiLineXmlCommentSyntax));
2120

2221
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
@@ -55,40 +54,16 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
5554
if (childNode is not XmlElementSyntax elementSyntax)
5655
continue;
5756

57+
if (!string.Equals(elementSyntax.StartTag.Name.LocalName.ValueText, "summary", StringComparison.Ordinal))
58+
continue;
59+
5860
var startLine = elementSyntax.StartTag.GetLocation().GetLineSpan().StartLinePosition.Line;
5961
var endLine = elementSyntax.EndTag.GetLocation().GetLineSpan().EndLinePosition.Line;
6062
if (endLine != startLine)
6163
continue;
6264

63-
var hasCDataOrOtherElements = false;
64-
var meaningfulTextTokenCount = 0;
65-
foreach (var content in elementSyntax.Content)
66-
{
67-
if (content is XmlTextSyntax textSyntax)
68-
{
69-
foreach (var token in textSyntax.TextTokens)
70-
{
71-
if (token.IsKind(SyntaxKind.XmlTextLiteralNewLineToken))
72-
continue;
73-
74-
var text = token.Text.Trim();
75-
if (!string.IsNullOrWhiteSpace(text))
76-
{
77-
meaningfulTextTokenCount++;
78-
}
79-
}
80-
}
81-
else if (content is XmlCDataSectionSyntax || content is XmlElementSyntax)
82-
{
83-
hasCDataOrOtherElements = true;
84-
break;
85-
}
86-
}
87-
88-
if (!hasCDataOrOtherElements && meaningfulTextTokenCount <= 1)
89-
{
65+
if (!string.IsNullOrWhiteSpace(string.Concat(elementSyntax.Content.Select(static content => content.ToFullString()))))
9066
context.ReportDiagnostic(Diagnostic.Create(Rule, elementSyntax.GetLocation()));
91-
}
9267
}
9368
}
9469
}

0 commit comments

Comments
 (0)