Skip to content

Commit

Permalink
Remove trailing white-space in documentation comment (RCS1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Feb 2, 2020
1 parent 4c4281e commit c3f7d19
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Roslynator.CodeFixes;

Expand All @@ -29,8 +31,14 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

if (!TryFindTrivia(root, context.Span.Start, out SyntaxTrivia trivia))
TextSpan span = context.Span;

if (!root.FindTrivia(span.Start).IsWhitespaceOrEndOfLineTrivia()
&& !root.FindToken(span.Start, findInsideTrivia: true).IsKind(SyntaxKind.XmlTextLiteralToken))
{
Debug.Fail("");
return;
}

foreach (Diagnostic diagnostic in context.Diagnostics)
{
Expand All @@ -40,7 +48,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
CodeAction codeAction = CodeAction.Create(
"Remove trailing white-space",
ct => context.Document.WithTextChangeAsync(new TextChange(context.Span, ""), ct),
ct => context.Document.WithTextChangeAsync(new TextChange(span, ""), ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
Expand All @@ -50,7 +58,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
CodeAction codeAction = CodeAction.Create(
"Remove empty line",
ct => context.Document.WithTextChangeAsync(new TextChange(context.Span, ""), ct),
ct => context.Document.WithTextChangeAsync(new TextChange(span, ""), ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
Expand Down
4 changes: 3 additions & 1 deletion src/Analyzers/Analysis/WhitespaceAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.CSharp;

namespace Roslynator.CSharp.Analysis
{
Expand Down Expand Up @@ -94,7 +95,8 @@ private static void AnalyzeTrailingTrivia(SyntaxTreeAnalysisContext context)

TextSpan whitespace = TextSpan.FromBounds(start, end + 1);

if (root.FindTrivia(start).IsWhitespaceTrivia())
if (root.FindTrivia(start).IsWhitespaceTrivia()
|| root.FindToken(start, findInsideTrivia: true).IsKind(SyntaxKind.XmlTextLiteralToken))
{
if (previousLineIsEmpty && start == textLine.Start)
{
Expand Down
63 changes: 63 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1037RemoveTrailingWhitespaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslynator.CSharp.CodeFixes;
using Xunit;

namespace Roslynator.CSharp.Analysis.Tests
{
public class RCS1037RemoveTrailingWhitespaceTests : AbstractCSharpFixVerifier
{
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.RemoveTrailingWhitespace;

public override DiagnosticAnalyzer Analyzer { get; } = new WhitespaceAnalyzer();

public override CodeFixProvider FixProvider { get; } = new WhitespaceTriviaCodeFixProvider();

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveTrailingWhitespace)]
public async Task Test()
{
await VerifyDiagnosticAndFixAsync(@"[| |]
class C[| |]
{
[|
|]
}
", @"
class C
{
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveTrailingWhitespace)]
public async Task Test_DocumentationComment()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
/// <summary>[| |]
/// x[| |]
/// </summary>[| |]
void M()
{
}
}
", @"
class C
{
/// <summary>
/// x
/// </summary>
void M()
{
}
}
");
}
}
}

0 comments on commit c3f7d19

Please sign in to comment.