Skip to content

Commit 6a7a9cb

Browse files
committed
Fix SA1212 hard-coding of CRLF
1 parent 6b53df4 commit 6a7a9cb

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1212SA1213CodeFixProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5353
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5454
{
5555
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
56+
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
57+
var options = document.Project.Solution.Workspace.Options;
5658

5759
var accessorToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
5860
var accessorList = (AccessorListSyntax)accessorToken.Parent.Parent;
@@ -67,8 +69,9 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
6769

6870
if (HasLeadingBlankLines(secondAccessor))
6971
{
72+
var endOfLine = FormattingHelper.GetEndOfLineForCodeFix(firstAccesor.Keyword, sourceText, options);
7073
trackedFirstAccessor = syntaxRoot.GetCurrentNode(firstAccesor);
71-
var newFirstAccessor = trackedFirstAccessor.WithLeadingTrivia(new[] { SyntaxFactory.CarriageReturnLineFeed }.Concat(firstAccesor.GetFirstToken().WithoutLeadingBlankLines().LeadingTrivia));
74+
var newFirstAccessor = trackedFirstAccessor.WithLeadingTrivia(new[] { endOfLine }.Concat(firstAccesor.GetFirstToken().WithoutLeadingBlankLines().LeadingTrivia));
7275
syntaxRoot = syntaxRoot.ReplaceNode(trackedFirstAccessor, newFirstAccessor);
7376
}
7477

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1212UnitTests.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.OrderingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.Testing;
9+
using StyleCop.Analyzers.Test.Helpers;
1110
using Xunit;
1211
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1312
StyleCop.Analyzers.OrderingRules.SA1212PropertyAccessorsMustFollowOrder,
1413
StyleCop.Analyzers.OrderingRules.SA1212SA1213CodeFixProvider>;
1514

1615
public class SA1212UnitTests
1716
{
18-
[Fact]
19-
public async Task TestPropertyWithDocumentationAsync()
17+
[Theory]
18+
[InlineData("\n")]
19+
[InlineData("\r\n")]
20+
public async Task TestPropertyWithDocumentationAsync(string lineEnding)
2021
{
2122
var testCode = @"
2223
public class Foo
@@ -28,10 +29,10 @@ public int Prop
2829
/// <summary>
2930
/// The setter documentation
3031
/// </summary>
31-
set
32+
{|#0:set
3233
{
3334
i = value;
34-
}
35+
}|}
3536
3637
/// <summary>
3738
/// The getter documentation
@@ -41,9 +42,9 @@ public int Prop
4142
return i;
4243
}
4344
}
44-
}";
45+
}".ReplaceLineEndings(lineEnding);
4546

46-
DiagnosticResult expected = Diagnostic().WithLocation(11, 9);
47+
DiagnosticResult expected = Diagnostic().WithLocation(0);
4748

4849
var fixedCode = @"
4950
public class Foo
@@ -68,7 +69,7 @@ public int Prop
6869
i = value;
6970
}
7071
}
71-
}";
72+
}".ReplaceLineEndings(lineEnding);
7273

7374
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
7475
}

0 commit comments

Comments
 (0)