Skip to content

Commit ab4f40c

Browse files
committed
Fix SA1116 hard-coding of CRLF
1 parent d2db3e8 commit ab4f40c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
7272
}
7373
}
7474

75+
var options = document.Project.Solution.Workspace.Options;
76+
var endOfLineTrivia = FormattingHelper.GetEndOfLineForCodeFix(originalToken, sourceText, options);
7577
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, tree, cancellationToken);
7678
SyntaxTriviaList newTrivia =
7779
SyntaxFactory.TriviaList(
78-
SyntaxFactory.CarriageReturnLineFeed,
80+
endOfLineTrivia,
7981
SyntaxFactory.Whitespace(lineText.Substring(0, indentLength) + IndentationHelper.GenerateIndentationString(settings.Indentation, 1)));
8082

8183
SyntaxToken updatedToken = originalToken.WithLeadingTrivia(originalToken.LeadingTrivia.AddRange(newTrivia));

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1116UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
1313
using System.Threading;
1414
using System.Threading.Tasks;
1515
using Microsoft.CodeAnalysis.Testing;
16+
using StyleCop.Analyzers.Test.Helpers;
1617
using Xunit;
1718
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1819
StyleCop.Analyzers.ReadabilityRules.SA1116SplitParametersMustStartOnLineAfterDeclaration,
@@ -273,8 +274,10 @@ class ObsoleteType
273274
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
274275
}
275276

276-
[Fact]
277-
public async Task TestInvalidAttributeAsync()
277+
[Theory]
278+
[InlineData("\n")]
279+
[InlineData("\r\n")]
280+
public async Task TestInvalidAttributeAsync(string lineEnding)
278281
{
279282
var testCode = @"
280283
[System.AttributeUsage(System.AttributeTargets.Class)]
@@ -285,11 +288,11 @@ public MyAttribute(int a, int b)
285288
}
286289
}
287290
288-
[MyAttribute(1,
291+
[MyAttribute({|#0:1|},
289292
2)]
290293
class Foo
291294
{
292-
}";
295+
}".ReplaceLineEndings(lineEnding);
293296
var fixedCode = @"
294297
[System.AttributeUsage(System.AttributeTargets.Class)]
295298
public class MyAttribute : System.Attribute
@@ -304,9 +307,9 @@ public MyAttribute(int a, int b)
304307
2)]
305308
class Foo
306309
{
307-
}";
310+
}".ReplaceLineEndings(lineEnding);
308311

309-
DiagnosticResult expected = Diagnostic().WithLocation(10, 14);
312+
DiagnosticResult expected = Diagnostic().WithLocation(0);
310313
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
311314
}
312315
}

0 commit comments

Comments
 (0)