Skip to content

Commit b2b7a4f

Browse files
committed
Fix SA1127 hard-coding of CRLF
1 parent ab4f40c commit b2b7a4f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5656
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5757
{
5858
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
59+
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
60+
var options = document.Project.Solution.Workspace.Options;
5961
var whereToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
6062
var precedingToken = whereToken.GetPreviousToken();
6163
var endToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.End);
@@ -65,11 +67,14 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
6567
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
6668
var indentationTrivia = SyntaxFactory.Whitespace(parentIndentation + IndentationHelper.GenerateIndentationString(settings.Indentation, 1));
6769

70+
var precedingTokenEndOfLine = FormattingHelper.GetEndOfLineForCodeFix(precedingToken, sourceText, options);
71+
var endTokenEndOfLine = FormattingHelper.GetEndOfLineForCodeFix(endToken, sourceText, options);
72+
6873
var replaceMap = new Dictionary<SyntaxToken, SyntaxToken>()
6974
{
70-
[precedingToken] = precedingToken.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed),
75+
[precedingToken] = precedingToken.WithTrailingTrivia(precedingTokenEndOfLine),
7176
[whereToken] = whereToken.WithLeadingTrivia(indentationTrivia),
72-
[endToken] = endToken.WithTrailingTrivia(RemoveUnnecessaryWhitespaceTrivia(endToken).Add(SyntaxFactory.CarriageReturnLineFeed)),
77+
[endToken] = endToken.WithTrailingTrivia(RemoveUnnecessaryWhitespaceTrivia(endToken).Add(endTokenEndOfLine)),
7378
};
7479

7580
if (afterEndToken.IsKind(SyntaxKind.EqualsGreaterThanToken))

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1127UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Microsoft.CodeAnalysis.Testing;
12+
using StyleCop.Analyzers.Test.Helpers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1415
StyleCop.Analyzers.ReadabilityRules.SA1127GenericTypeConstraintsMustBeOnOwnLine,
@@ -49,22 +50,24 @@ public async Task TestViolationWithTypeDeclarationAsync(string declaration, stri
4950
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
5051
}
5152

52-
[Fact]
53-
public async Task TestViolationWithMethodDeclarationAsync()
53+
[Theory]
54+
[InlineData("\n")]
55+
[InlineData("\r\n")]
56+
public async Task TestViolationWithMethodDeclarationAsync(string lineEnding)
5457
{
5558
var testCode = $@"
5659
class Foo
5760
{{
58-
private void Method<T>() where T : class {{ }}
59-
}}";
61+
private void Method<T>() {{|#0:where T : class|}} {{ }}
62+
}}".ReplaceLineEndings(lineEnding);
6063
var fixedCode = $@"
6164
class Foo
6265
{{
6366
private void Method<T>()
6467
where T : class
6568
{{ }}
66-
}}";
67-
var expected = Diagnostic().WithLocation(4, 30);
69+
}}".ReplaceLineEndings(lineEnding);
70+
var expected = Diagnostic().WithLocation(0);
6871
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
6972
}
7073

0 commit comments

Comments
 (0)