Skip to content

Commit fb992b7

Browse files
committed
Fix SA1128 assumption that files match default end-of-line settings
1 parent b2b7a4f commit fb992b7

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5757
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5858
{
5959
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
60+
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
61+
var options = document.Project.Solution.Workspace.Options;
6062
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
61-
var newLine = FormattingHelper.GetNewLineTrivia(document);
6263

6364
var constructorInitializer = (ConstructorInitializerSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
6465
var constructorDeclaration = (ConstructorDeclarationSyntax)constructorInitializer.Parent;
6566

67+
var newLine = FormattingHelper.GetEndOfLineForCodeFix(constructorDeclaration.GetFirstToken(), text, options);
6668
var newConstructorDeclaration = ReformatConstructorDeclaration(constructorDeclaration, settings.Indentation, newLine);
6769

6870
var newSyntaxRoot = syntaxRoot.ReplaceNode(constructorDeclaration, newConstructorDeclaration);
@@ -110,12 +112,19 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
110112
}
111113

112114
var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
115+
var sourceText = await document.GetTextAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
116+
var options = document.Project.Solution.Workspace.Options;
113117
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
114-
var newLine = FormattingHelper.GetNewLineTrivia(document);
115118

116119
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan).Parent);
117120

118-
return syntaxRoot.ReplaceNodes(nodes, (originalNode, rewrittenNode) => ReformatConstructorDeclaration((ConstructorDeclarationSyntax)rewrittenNode, settings.Indentation, newLine));
121+
return syntaxRoot.ReplaceNodes(
122+
nodes,
123+
(originalNode, rewrittenNode) =>
124+
{
125+
var endOfLineTrivia = FormattingHelper.GetEndOfLineForCodeFix(originalNode.GetFirstToken(), sourceText, options);
126+
return ReformatConstructorDeclaration((ConstructorDeclarationSyntax)rewrittenNode, settings.Indentation, endOfLineTrivia);
127+
});
119128
}
120129
}
121130
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1128UnitTests.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.SA1128ConstructorInitializerMustBeOnOwnLine,
@@ -30,25 +31,27 @@ public async Task TestNullScenariosAsync(string declaration)
3031
await VerifyCSharpDiagnosticAsync(declaration, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3132
}
3233

33-
[Fact]
34-
public async Task TestViolationWithBaseInitializerOnSameLineAsync()
34+
[Theory]
35+
[InlineData("\n")]
36+
[InlineData("\r\n")]
37+
public async Task TestViolationWithBaseInitializerOnSameLineAsync(string lineEnding)
3538
{
3639
var testCode = @"
3740
public class TypeName
3841
{
39-
public TypeName() : base()
42+
public TypeName() {|#0:: base()|}
4043
{
4144
}
42-
}";
45+
}".ReplaceLineEndings(lineEnding);
4346
var fixedCode = @"
4447
public class TypeName
4548
{
4649
public TypeName()
4750
: base()
4851
{
4952
}
50-
}";
51-
var expected = Diagnostic().WithLocation(4, 23);
53+
}".ReplaceLineEndings(lineEnding);
54+
var expected = Diagnostic().WithLocation(0);
5255
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
5356
}
5457

0 commit comments

Comments
 (0)