Skip to content

Commit

Permalink
Merge pull request #3607 from bjornhellander/feature/sa1513-linefeed
Browse files Browse the repository at this point in the history
Update SA1513 codefix to use the existing newline character sequence
  • Loading branch information
sharwell authored Jun 21, 2023
2 parents 0e192df + 861b432 commit ec1112e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ private static async Task<SyntaxNode> GetTransformedDocumentAsync(Document docum
diagnostics.Select(diagnostic => root.FindToken(diagnostic.Location.SourceSpan.End)),
(originalToken, rewrittenToken) =>
{
var newTrivia = rewrittenToken.LeadingTrivia.Insert(0, SyntaxFactory.CarriageReturnLineFeed);
var endOfLineTrivia = rewrittenToken.GetPrecedingEndOfLineTrivia();
var newTrivia = rewrittenToken.LeadingTrivia.Insert(0, endOfLineTrivia);
return rewrittenToken.WithLeadingTrivia(newTrivia);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.Helpers
{
public static class StringExtensions
{
public static string ReplaceLineEndings(this string input, string replacementText)
{
// First normalize to LF
var lineFeedInput = input
.Replace("\r\n", "\n")
.Replace("\r", "\n")
.Replace("\f", "\n")
.Replace("\x0085", "\n")
.Replace("\x2028", "\n")
.Replace("\x2029", "\n");

// Then normalize to the replacement text
return lineFeedInput.Replace("\n", replacementText);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.LayoutRules;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1513ClosingBraceMustBeFollowedByBlankLine,
Expand Down Expand Up @@ -1013,5 +1014,25 @@ public void TestMethod(string extraSupport)

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

[Fact]
[WorkItem(3360, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3360")]
public async Task TestLineFeedEndOfLinesAsync()
{
var testCode = @"
public class TestClass
{
}[|
|]// Hello".ReplaceLineEndings("\n");

var fixedCode = @"
public class TestClass
{
}
// Hello".ReplaceLineEndings("\n");

await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
30 changes: 30 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers/Helpers/TokenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,35 @@ internal static bool IsFollowedByWhitespace(this SyntaxToken token)
triviaList = token.GetNextToken().LeadingTrivia;
return triviaList.Count > 0 && triviaList.First().IsKind(SyntaxKind.WhitespaceTrivia);
}

/// <summary>
/// Returns the closest end of line trivia preceding the <paramref name="token"/>.
/// This currently only looks immediately before the specified token.
/// </summary>
/// <param name="token">The token to process.</param>
/// <returns>The closest preceding end of line trivia, or <see cref="SyntaxFactory.CarriageReturnLineFeed"/> if none is found.</returns>
internal static SyntaxTrivia GetPrecedingEndOfLineTrivia(this SyntaxToken token)
{
var leadingTrivia = token.LeadingTrivia;
for (var i = leadingTrivia.Count - 1; i >= 0; i--)
{
if (leadingTrivia[i].IsKind(SyntaxKind.EndOfLineTrivia))
{
return leadingTrivia[i];
}
}

var prevToken = token.GetPreviousToken();
var prevTrailingTrivia = prevToken.TrailingTrivia;
for (var i = prevTrailingTrivia.Count - 1; i >= 0; i--)
{
if (prevTrailingTrivia[i].IsKind(SyntaxKind.EndOfLineTrivia))
{
return prevTrailingTrivia[i];
}
}

return SyntaxFactory.CarriageReturnLineFeed;
}
}
}

0 comments on commit ec1112e

Please sign in to comment.