Skip to content

Commit

Permalink
Merge pull request #3861 from MattFromRVA/SA1642_Fix
Browse files Browse the repository at this point in the history
SA1642: Allow <para> Tags in Constructor Documentation
  • Loading branch information
sharwell authored Jun 26, 2024
2 parents 51c772e + df78311 commit ff5c432
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,36 @@ public ClassName()
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3575, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3575")]
public async Task TestConstructorSummaryWithParaTagsAsync()
{
var testCode = @"
using System;
/// <summary>
/// Does a thing.
/// </summary>
public class B
{
/// <summary>
/// <para>
/// Initializes a new instance of the <see cref=""B""/> class.
/// </para>
/// <para>
/// Some more info about B.
/// </para>
/// </summary>
public B()
{
}
}
";

var expectedDiagnostics = DiagnosticResult.EmptyDiagnosticResults;

await VerifyCSharpFixAsync(testCode, expectedDiagnostics, testCode, CancellationToken.None).ConfigureAwait(false);
}

private static async Task TestEmptyConstructorAsync(string typeKind, string modifiers)
{
var testCode = @"namespace FooNamespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ protected static MatchResult HandleDeclaration(SyntaxNodeAnalysisContext context
diagnosticLocation = summaryElement.GetLocation();
diagnosticProperties = ImmutableDictionary.Create<string, string>();

// Handle empty or whitespace-only summary content
var firstElementWithContent = XmlCommentHelper.TryGetFirstTextElementWithContent(summaryElement);
if (firstElementWithContent == null)
{
// Report the diagnostic for empty or whitespace-only summaries
if (diagnosticDescriptor != null)
{
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, diagnosticLocation, diagnosticProperties));
}

return MatchResult.None;
}

// Check if the summary content starts with a <para> tag
if (firstElementWithContent != null && firstElementWithContent.Parent is XmlElementSyntax firstElement && firstElement.StartTag.Name.ToString() == "para")
{
// We found a correct standard text
return MatchResult.FoundMatch;
}

// Check if the summary content could be a correct standard text
if (summaryElement.Content.Count >= 3)
{
Expand Down
17 changes: 17 additions & 0 deletions documentation/SA1642.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ private Customer()
}
```

The `<para>` tag can be used within the `<summary>` tag to structure the text.
For example:

```csharp
/// <summary>
/// <para>
/// Initializes a new instance of the <see cref="Customer"/> class.
/// </para>
/// <para>
/// Additional information can be included in subsequent paragraphs.
/// </para>
/// </summary>
public Customer()
{
}
```

## How to fix violations

To fix a violation of this rule, edit the summary text for the constructor as described above.
Expand Down

0 comments on commit ff5c432

Please sign in to comment.