Skip to content

Commit 773eaaa

Browse files
committed
Fix SA1136 hard-coding of CRLF
1 parent 602b220 commit 773eaaa

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs

Lines changed: 3 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
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
5759

5860
var enumMemberDeclaration = (EnumMemberDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
@@ -71,7 +73,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
7173

7274
var newTrailingTrivia = SyntaxFactory.TriviaList(sharedTrivia)
7375
.WithoutTrailingWhitespace()
74-
.Add(SyntaxFactory.CarriageReturnLineFeed);
76+
.Add(FormattingHelper.GetEndOfLineForCodeFix(precedingSeparatorToken, sourceText, options));
7577

7678
// replace the trivia for the tokens
7779
var replacements = new Dictionary<SyntaxToken, SyntaxToken>

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1136UnitTests.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
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.ReadabilityRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.Testing;
119
using StyleCop.Analyzers.ReadabilityRules;
10+
using StyleCop.Analyzers.Test.Helpers;
1211
using Xunit;
1312
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1413
StyleCop.Analyzers.ReadabilityRules.SA1136EnumValuesShouldBeOnSeparateLines,
@@ -23,16 +22,19 @@ public class SA1136UnitTests
2322
/// Verifies that an enum declaration with all values on the same line will return the expected diagnostics.
2423
/// This also verifies that an enum declaration with all values on separate lines will not produce diagnostics.
2524
/// </summary>
25+
/// <param name="lineEnding">The line ending to use in the test code.</param>
2626
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
27-
[Fact]
28-
public async Task TestSingleLineEnumDeclarationAsync()
27+
[Theory]
28+
[InlineData("\n")]
29+
[InlineData("\r\n")]
30+
public async Task TestSingleLineEnumDeclarationAsync(string lineEnding)
2931
{
3032
var testCode = @"
3133
public enum TestEnum
3234
{
33-
FirstValue, SecondValue, ThirdValue
35+
FirstValue, {|#0:SecondValue|}, {|#1:ThirdValue|}
3436
}
35-
";
37+
".ReplaceLineEndings(lineEnding);
3638

3739
var fixedTestCode = @"
3840
public enum TestEnum
@@ -41,12 +43,12 @@ public enum TestEnum
4143
SecondValue,
4244
ThirdValue
4345
}
44-
";
46+
".ReplaceLineEndings(lineEnding);
4547

4648
DiagnosticResult[] expected =
4749
{
48-
Diagnostic().WithLocation(4, 17),
49-
Diagnostic().WithLocation(4, 30),
50+
Diagnostic().WithLocation(0),
51+
Diagnostic().WithLocation(1),
5052
};
5153

5254
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);

0 commit comments

Comments
 (0)