Skip to content

Commit 2633726

Browse files
committed
Fix SA1133 hard-coding of CRLF
1 parent f9cfef7 commit 2633726

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace StyleCop.Analyzers.ReadabilityRules
1616
using Microsoft.CodeAnalysis.CodeFixes;
1717
using Microsoft.CodeAnalysis.CSharp;
1818
using Microsoft.CodeAnalysis.CSharp.Syntax;
19+
using Microsoft.CodeAnalysis.Options;
20+
using Microsoft.CodeAnalysis.Text;
1921
using StyleCop.Analyzers.Helpers;
2022

2123
/// <summary>
@@ -58,22 +60,24 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5860
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5961
{
6062
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
63+
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
64+
var options = document.Project.Solution.Workspace.Options;
6165
var nodeInSourceSpan = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
6266
AttributeListSyntax attributeList = nodeInSourceSpan.FirstAncestorOrSelf<AttributeListSyntax>();
6367

6468
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
6569
var indentationSteps = IndentationHelper.GetIndentationSteps(settings.Indentation, attributeList);
6670
var indentationTrivia = IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps);
6771

68-
List<AttributeListSyntax> newAttributeLists = GetNewAttributeList(attributeList, indentationTrivia);
72+
List<AttributeListSyntax> newAttributeLists = GetNewAttributeList(attributeList, indentationTrivia, sourceText, options);
6973

7074
var newSyntaxRoot = syntaxRoot.ReplaceNode(attributeList, newAttributeLists);
7175
var newDocument = document.WithSyntaxRoot(newSyntaxRoot.WithoutFormatting());
7276

7377
return newDocument;
7478
}
7579

76-
private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax attributeList, SyntaxTrivia indentationTrivia)
80+
private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax attributeList, SyntaxTrivia indentationTrivia, SourceText sourceText, OptionSet options)
7781
{
7882
var newAttributeLists = new List<AttributeListSyntax>();
7983

@@ -90,7 +94,7 @@ private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax
9094

9195
newAttributeList = (i == (attributeList.Attributes.Count - 1))
9296
? newAttributeList.WithTrailingTrivia(attributeList.GetTrailingTrivia())
93-
: newAttributeList.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
97+
: newAttributeList.WithTrailingTrivia(FormattingHelper.GetEndOfLineForCodeFix(attributeList.CloseBracketToken, sourceText, options));
9498

9599
newAttributeLists.Add(newAttributeList);
96100
}
@@ -114,6 +118,8 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
114118
}
115119

116120
var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
121+
var sourceText = await document.GetTextAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
122+
var options = document.Project.Solution.Workspace.Options;
117123
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
118124

119125
// 🐉 Need to eagerly evaluate this with ToList() to ensure nodes are not garbage collected between the
@@ -126,7 +132,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
126132
{
127133
var indentationSteps = IndentationHelper.GetIndentationSteps(settings.Indentation, attributeList);
128134
var indentationTrivia = IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps);
129-
newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(attributeList), GetNewAttributeList(attributeList, indentationTrivia));
135+
newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(attributeList), GetNewAttributeList(attributeList, indentationTrivia, sourceText, options));
130136
}
131137

132138
return newRoot;

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1133UnitTests.cs

Lines changed: 12 additions & 8 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.SA1133DoNotCombineAttributes,
@@ -56,24 +57,27 @@ public class TestClass
5657
/// <summary>
5758
/// Verifies that an attribute list will produce the required diagnostics.
5859
/// </summary>
60+
/// <param name="lineEnding">The line ending to use in the test code.</param>
5961
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
60-
[Fact]
61-
public async Task VerifyThatAttributeListProducesDiagnosticAsync()
62+
[Theory]
63+
[InlineData("\n")]
64+
[InlineData("\r\n")]
65+
public async Task VerifyThatAttributeListProducesDiagnosticAsync(string lineEnding)
6266
{
6367
var testCode = @"using System.ComponentModel;
6468
65-
[EditorBrowsable(EditorBrowsableState.Never), DesignOnly(true)]
69+
[EditorBrowsable(EditorBrowsableState.Never), {|#0:DesignOnly|}(true)]
6670
public class TestClass
6771
{
6872
/// <summary>
6973
/// Test method.
7074
/// </summary>
71-
[EditorBrowsable(EditorBrowsableState.Never), DesignOnly(true), DisplayName(""Test"")] // test comment
75+
[EditorBrowsable(EditorBrowsableState.Never), {|#1:DesignOnly|}(true), DisplayName(""Test"")] // test comment
7276
public void TestMethod()
7377
{
7478
}
7579
}
76-
";
80+
".ReplaceLineEndings(lineEnding);
7781

7882
var fixedTestCode = @"using System.ComponentModel;
7983
@@ -91,12 +95,12 @@ public void TestMethod()
9195
{
9296
}
9397
}
94-
";
98+
".ReplaceLineEndings(lineEnding);
9599

96100
DiagnosticResult[] expected =
97101
{
98-
Diagnostic().WithLocation(3, 47),
99-
Diagnostic().WithLocation(9, 51),
102+
Diagnostic().WithLocation(0),
103+
Diagnostic().WithLocation(1),
100104
};
101105

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

0 commit comments

Comments
 (0)