Skip to content

Commit f6e5dbe

Browse files
csharpfritzCopilot
andcommitted
fix: use document line endings in code fix providers for cross-platform CI
Code fix providers for BWFC002, BWFC004, BWFC010, BWFC012 hardcoded \r\n in EndOfLine trivia, causing test failures on Linux CI runners where source files have \n line endings. Added SyntaxExtensions.DetectEndOfLine() helper that reads the first EndOfLineTrivia from the syntax tree, ensuring code fixes match the document's existing line ending style. Fixes failing tests in PR FritzAndFriends#487. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 46cb20d commit f6e5dbe

5 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/BlazorWebFormsComponents.Analyzers/EventHandlerSignatureCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private async Task<Document> AddTodoCommentAsync(Document document, MethodDeclar
5858

5959
var newLeadingTrivia = method.GetLeadingTrivia()
6060
.Add(todoComment)
61-
.Add(SyntaxFactory.EndOfLine("\r\n"));
61+
.Add(root.DetectEndOfLine());
6262

6363
// Collect indentation from original method
6464
foreach (var trivia in method.GetLeadingTrivia())

src/BlazorWebFormsComponents.Analyzers/RequiredAttributeCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private async Task<Document> AddTodoCommentAsync(Document document, SyntaxNode n
5757

5858
var newLeadingTrivia = statement.GetLeadingTrivia()
5959
.Insert(0, todoComment)
60-
.Insert(1, SyntaxFactory.EndOfLine("\r\n"));
60+
.Insert(1, root.DetectEndOfLine());
6161

6262
var newStatement = statement.WithLeadingTrivia(newLeadingTrivia);
6363
var newRoot = root.ReplaceNode(statement, newStatement);

src/BlazorWebFormsComponents.Analyzers/ResponseRedirectCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private async Task<Document> ReplaceWithTodoCommentAsync(Document document, Synt
6464
// Build leading trivia: original leading + comment + newline + indentation for the ;
6565
var leading = statement.GetLeadingTrivia()
6666
.Add(todoComment)
67-
.Add(SyntaxFactory.EndOfLine("\r\n"))
67+
.Add(root.DetectEndOfLine())
6868
.AddRange(indentation);
6969

7070
var emptyStatement = SyntaxFactory.EmptyStatement()

src/BlazorWebFormsComponents.Analyzers/SessionUsageCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private async Task<Document> ReplaceWithTodoCommentAsync(Document document, Synt
6464
// Build leading trivia: original leading + comment + newline + indentation for the ;
6565
var leading = statement.GetLeadingTrivia()
6666
.Add(todoComment)
67-
.Add(SyntaxFactory.EndOfLine("\r\n"))
67+
.Add(root.DetectEndOfLine())
6868
.AddRange(indentation);
6969

7070
var emptyStatement = SyntaxFactory.EmptyStatement()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
4+
namespace BlazorWebFormsComponents.Analyzers
5+
{
6+
internal static class SyntaxExtensions
7+
{
8+
/// <summary>
9+
/// Detects the line ending style used in the syntax tree and returns a matching
10+
/// EndOfLine trivia. Falls back to <c>\n</c> if no existing line endings are found.
11+
/// </summary>
12+
internal static SyntaxTrivia DetectEndOfLine(this SyntaxNode root)
13+
{
14+
foreach (var trivia in root.DescendantTrivia())
15+
{
16+
if (trivia.IsKind(SyntaxKind.EndOfLineTrivia))
17+
{
18+
return trivia;
19+
}
20+
}
21+
22+
return SyntaxFactory.EndOfLine("\n");
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)