Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of semicolon preceded by a comment #1462

Merged
merged 2 commits into from
Sep 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,71 @@ Comment Line 2 */
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#1426.
/// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1426
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestPrecededByBlockCommentAsync()
{
string testCode = @"
class ClassName
{
void MethodName()
{
bool special = true ? true /*comment*/ : false /*comment*/ ;
}
}
";
string fixedCode = @"
class ClassName
{
void MethodName()
{
bool special = true ? true /*comment*/ : false /*comment*/;
}
}
";

DiagnosticResult expected = this.CSharpDiagnostic().WithArguments(" not", "preceded").WithLocation(6, 68);

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#403.
/// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/403
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestSemicolonAtBeginningOfLineAsync()
{
string testCode = @"
class ClassName
{
void MethodName()
{
// The first one is indented (has leading trivia).
bool special = false
;

// The second one is not indented (no leading trivia).
special = true
;

// The third one is preceded by non-whitespace trivia.
special = false
/*comment*/;
}
}
";

await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
{
yield return new SA1002SemicolonsMustBeSpacedCorrectly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class OpenCloseSpacingCodeFixProvider : CodeFixProvider
private const string LocationFollowing = "following";
private const string ActionInsert = "insert";
private const string ActionRemove = "remove";
private const string ActionRemoveImmediate = "remove-immediate";
private const string LayoutPack = "pack";
private const string LayoutPreserve = "preserve";

Expand Down Expand Up @@ -69,6 +70,22 @@ public class OpenCloseSpacingCodeFixProvider : CodeFixProvider
.SetItem(LocationKey, LocationPreceding)
.SetItem(ActionKey, ActionInsert);

/// <summary>
/// Gets a property collection indicating that the code fix should remove any
/// <see cref="SyntaxKind.WhitespaceTrivia"/> trivia which <em>immediately</em> precedes the token identified by
/// the diagnostic span.
/// </summary>
/// <value>
/// A property collection indicating that the code fix should remove any
/// <see cref="SyntaxKind.WhitespaceTrivia"/> trivia which <em>immediately</em> precedes the token identified by
/// the diagnostic span.
/// </value>
internal static ImmutableDictionary<string, string> RemoveImmediatePreceding { get; } =
ImmutableDictionary<string, string>.Empty
.SetItem(LocationKey, LocationPreceding)
.SetItem(ActionKey, ActionRemoveImmediate)
.SetItem(LayoutKey, LayoutPack);

internal static ImmutableDictionary<string, string> RemovePreceding { get; } =
ImmutableDictionary<string, string>.Empty
.SetItem(LocationKey, LocationPreceding)
Expand Down Expand Up @@ -224,6 +241,28 @@ private static void UpdateReplaceMap(Dictionary<SyntaxToken, SyntaxToken> replac
replaceMap[token] = token.WithLeadingTrivia().WithTrailingTrivia(trailingTrivia);
}

break;

case ActionRemoveImmediate:
SyntaxTriviaList tokenLeadingTrivia = token.LeadingTrivia;
while (tokenLeadingTrivia.Any() && tokenLeadingTrivia.Last().IsKind(SyntaxKind.WhitespaceTrivia))
{
tokenLeadingTrivia = tokenLeadingTrivia.RemoveAt(tokenLeadingTrivia.Count - 1);
}

replaceMap[token] = token.WithLeadingTrivia(tokenLeadingTrivia);

if (!tokenLeadingTrivia.Any())
{
SyntaxTriviaList previousTrailingTrivia = prevToken.TrailingTrivia;
while (previousTrailingTrivia.Any() && previousTrailingTrivia.Last().IsKind(SyntaxKind.WhitespaceTrivia))
{
previousTrailingTrivia = previousTrailingTrivia.RemoveAt(previousTrailingTrivia.Count - 1);
}

replaceMap[prevToken] = prevToken.WithTrailingTrivia(previousTrailingTrivia);
}

break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace StyleCop.Analyzers.SpacingRules
{
using System.Collections.Immutable;
using Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -105,11 +106,12 @@ private static void HandleSemicolonToken(SyntaxTreeAnalysisContext context, Synt

bool hasPrecedingSpace = false;
bool ignorePrecedingSpace = false;
if (!token.HasLeadingTrivia)
if (!token.IsFirstInLine())
{
// only the first token on the line has leading trivia, and those are ignored
SyntaxToken precedingToken = token.GetPreviousToken();
if (precedingToken.HasTrailingTrivia)
SyntaxTriviaList trailingTrivia = precedingToken.TrailingTrivia;
if (trailingTrivia.Any() && trailingTrivia.Last().IsKind(SyntaxKind.WhitespaceTrivia))
{
hasPrecedingSpace = true;
}
Expand All @@ -131,7 +133,7 @@ private static void HandleSemicolonToken(SyntaxTreeAnalysisContext context, Synt
if (hasPrecedingSpace && !ignorePrecedingSpace)
{
// semicolon must{ not} be {preceded} by a space
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), OpenCloseSpacingCodeFixProvider.RemovePreceding, " not", "preceded"));
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), OpenCloseSpacingCodeFixProvider.RemoveImmediatePreceding, " not", "preceded"));
}
}
}
Expand Down