Skip to content

Commit de3f675

Browse files
committed
Fix SA1104/SA1105 hard-coding of CRLF
1 parent f80d806 commit de3f675

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace StyleCop.Analyzers.ReadabilityRules
1313
using Microsoft.CodeAnalysis;
1414
using Microsoft.CodeAnalysis.CodeActions;
1515
using Microsoft.CodeAnalysis.CodeFixes;
16-
using Microsoft.CodeAnalysis.CSharp;
1716
using StyleCop.Analyzers.Helpers;
1817

1918
/// <summary>
@@ -54,14 +53,17 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5453
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5554
{
5655
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
56+
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
5757
var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
5858

5959
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
6060
var indentationTrivia = QueryIndentationHelpers.GetQueryIndentationTrivia(settings.Indentation, token);
6161

6262
var precedingToken = token.GetPreviousToken();
63+
var options = document.Project.Solution.Workspace.Options;
64+
var endOfLineTrivia = FormattingHelper.GetEndOfLineForCodeFix(token, text, options);
6365
var triviaList = precedingToken.TrailingTrivia.AddRange(token.LeadingTrivia);
64-
var processedTriviaList = triviaList.WithoutTrailingWhitespace().Add(SyntaxFactory.CarriageReturnLineFeed);
66+
var processedTriviaList = triviaList.WithoutTrailingWhitespace().Add(endOfLineTrivia);
6567

6668
var replaceMap = new Dictionary<SyntaxToken, SyntaxToken>()
6769
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1104UnitTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
1111
using StyleCop.Analyzers.ReadabilityRules;
12+
using StyleCop.Analyzers.Test.Helpers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1415
StyleCop.Analyzers.ReadabilityRules.SA110xQueryClauses,
@@ -22,9 +23,12 @@ public class SA1104UnitTests
2223
/// <summary>
2324
/// Verifies that a select query expression produces the expected results.
2425
/// </summary>
26+
/// <param name="lineEnding">The line ending to use in the test code.</param>
2527
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
26-
[Fact]
27-
public async Task TestSelectQueryExpressionAsync()
28+
[Theory]
29+
[InlineData("\n")]
30+
[InlineData("\r\n")]
31+
public async Task TestSelectQueryExpressionAsync(string lineEnding)
2832
{
2933
var testCode = @"namespace TestNamespace
3034
{
@@ -43,11 +47,11 @@ public void TestMethod2()
4347
from element in TestMethod1
4448
(
4549
12
46-
) select element;
50+
) {|#0:select|} element;
4751
}
4852
}
4953
}
50-
";
54+
".ReplaceLineEndings(lineEnding);
5155

5256
var fixedTestCode = @"namespace TestNamespace
5357
{
@@ -71,11 +75,11 @@ from element in TestMethod1
7175
}
7276
}
7377
}
74-
";
78+
".ReplaceLineEndings(lineEnding);
7579

7680
DiagnosticResult[] expectedDiagnostics =
7781
{
78-
Diagnostic(SA110xQueryClauses.SA1104Descriptor).WithLocation(18, 19),
82+
Diagnostic(SA110xQueryClauses.SA1104Descriptor).WithLocation(0),
7983
};
8084

8185
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1105UnitTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
1111
using StyleCop.Analyzers.ReadabilityRules;
12+
using StyleCop.Analyzers.Test.Helpers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1415
StyleCop.Analyzers.ReadabilityRules.SA110xQueryClauses,
@@ -22,9 +23,12 @@ public class SA1105UnitTests
2223
/// <summary>
2324
/// Verifies that a select query expression produces the expected results.
2425
/// </summary>
26+
/// <param name="lineEnding">The line ending to use in the test code.</param>
2527
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
26-
[Fact]
27-
public async Task TestSelectQueryExpressionAsync()
28+
[Theory]
29+
[InlineData("\n")]
30+
[InlineData("\r\n")]
31+
public async Task TestSelectQueryExpressionAsync(string lineEnding)
2832
{
2933
var testCode = @"namespace TestNamespace
3034
{
@@ -40,14 +44,14 @@ public int[] TestMethod1(int x)
4044
public void TestMethod2()
4145
{
4246
var x =
43-
from element in new[] { 1, 2, 3 } select TestMethod1
47+
from element in new[] { 1, 2, 3 } {|#0:select|} TestMethod1
4448
(
4549
element
4650
);
4751
}
4852
}
4953
}
50-
";
54+
".ReplaceLineEndings(lineEnding);
5155

5256
var fixedTestCode = @"namespace TestNamespace
5357
{
@@ -71,11 +75,11 @@ select TestMethod1
7175
}
7276
}
7377
}
74-
";
78+
".ReplaceLineEndings(lineEnding);
7579

7680
DiagnosticResult[] expectedDiagnostics =
7781
{
78-
Diagnostic(SA110xQueryClauses.SA1105Descriptor).WithLocation(15, 51),
82+
Diagnostic(SA110xQueryClauses.SA1105Descriptor).WithLocation(0),
7983
};
8084

8185
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);

0 commit comments

Comments
 (0)