Skip to content

Commit bf6fec5

Browse files
committed
Treat empty XML elements as a "word not ending with a period"
1 parent ecee2c7 commit bf6fec5

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1629UnitTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,47 @@ public interface ITest
377377
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
378378
}
379379

380+
[Fact]
381+
[WorkItem(2680, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2680")]
382+
public async Task TestReportingAfterEmptyElementTwoSentencesAsync()
383+
{
384+
var testCode = @"
385+
/// <summary>
386+
/// Test interface. <see cref=""ITest""/>
387+
/// </summary>
388+
public interface ITest
389+
{
390+
/// <summary>
391+
/// Test method. <see cref=""Method""/><see cref=""Method""/>
392+
/// </summary>
393+
void Method();
394+
}
395+
";
396+
397+
var fixedTestCode = @"
398+
/// <summary>
399+
/// Test interface. <see cref=""ITest""/>.
400+
/// </summary>
401+
public interface ITest
402+
{
403+
/// <summary>
404+
/// Test method. <see cref=""Method""/><see cref=""Method""/>.
405+
/// </summary>
406+
void Method();
407+
}
408+
";
409+
410+
DiagnosticResult[] expected =
411+
{
412+
this.CSharpDiagnostic().WithLocation(3, 40),
413+
this.CSharpDiagnostic().WithLocation(8, 62),
414+
};
415+
416+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
417+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
418+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
419+
}
420+
380421
[Fact]
381422
[WorkItem(2679, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2679")]
382423
public async Task TestElementsThatDoNotRequirePeriodsAsync()

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1629DocumentationTextMustEndWithAPeriod.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, bool
8484
}
8585

8686
var elementDone = false;
87-
int? reportingLocation = null;
8887
for (var i = xmlElement.Content.Count - 1; !elementDone && (i >= 0); i--)
8988
{
9089
if (xmlElement.Content[i] is XmlTextSyntax contentNode)
@@ -98,7 +97,7 @@ protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, bool
9897
{
9998
if (!textWithoutTrailingWhitespace.EndsWith(".", StringComparison.Ordinal))
10099
{
101-
var location = Location.Create(xmlElement.SyntaxTree, new TextSpan(reportingLocation ?? textToken.SpanStart + textWithoutTrailingWhitespace.Length, 1));
100+
var location = Location.Create(xmlElement.SyntaxTree, new TextSpan(textToken.SpanStart + textWithoutTrailingWhitespace.Length, 1));
102101
context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
103102
}
104103

@@ -108,9 +107,10 @@ protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, bool
108107
}
109108
else if (xmlElement.Content[i].IsInlineElement())
110109
{
111-
// If a diagnostic gets reported for the element, place the diagnostic after the last inline
112-
// element.
113-
reportingLocation = reportingLocation ?? xmlElement.Content[i].Span.End;
110+
// Treat empty XML elements as a "word not ending with a period"
111+
var location = Location.Create(xmlElement.SyntaxTree, new TextSpan(xmlElement.Content[i].Span.End, 1));
112+
context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
113+
elementDone = true;
114114
}
115115
}
116116
}

0 commit comments

Comments
 (0)