Skip to content

Commit 7486aea

Browse files
authored
Merge pull request #2904 from vweijsters/fix-2901
Fixed spacing issue for SA1139 code fix
2 parents 921316e + 00e44b0 commit 7486aea

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1139CodeFixProvider.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4747
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
4848
{
4949
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
50-
var oldSemanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
5150

5251
if (!(syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is CastExpressionSyntax node))
5352
{
@@ -68,9 +67,18 @@ private static SyntaxNode GenerateReplacementNode(CastExpressionSyntax node)
6867
(LiteralExpressionSyntax)plusMinusSyntax.Operand.WalkDownParentheses();
6968
var typeToken = node.Type.GetFirstToken();
7069
var replacementLiteral = literalExpressionSyntax.WithLiteralSuffix(typeToken.Kind());
70+
71+
var newLeadingTrivia = SyntaxFactory.TriviaList(node.GetLeadingTrivia().Concat(node.CloseParenToken.TrailingTrivia).Concat(node.Expression.GetLeadingTrivia()))
72+
.WithoutLeadingWhitespace()
73+
.WithoutTrailingWhitespace();
74+
75+
if (newLeadingTrivia.Count != 0)
76+
{
77+
newLeadingTrivia = newLeadingTrivia.Add(SyntaxFactory.Space);
78+
}
79+
7180
var replacementNode = node.Expression.ReplaceNode(literalExpressionSyntax, replacementLiteral)
72-
.WithLeadingTrivia(node.GetLeadingTrivia().Concat(node.CloseParenToken.TrailingTrivia).Concat(node.Expression.GetLeadingTrivia()))
73-
.WithTrailingTrivia(node.Expression.GetTrailingTrivia().Concat(node.GetTrailingTrivia()));
81+
.WithLeadingTrivia(newLeadingTrivia);
7482

7583
return replacementNode;
7684
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1139UnitTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,5 +266,53 @@ public void Method()
266266

267267
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
268268
}
269+
270+
/// <summary>
271+
/// Verifies that the codefix will not insert extraneous spaces.
272+
/// </summary>
273+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
274+
[Fact]
275+
[WorkItem(2901, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2901")]
276+
public async Task TestCodeFixDoesNotAddExtraneousSpacesAsync()
277+
{
278+
var testCode = @"
279+
public class TestClass
280+
{
281+
public void TestMethod()
282+
{
283+
var x = (long)64 * 1024;
284+
var y = (double)64 /* test */ * 1024;
285+
var z = (long) /* test */ 64 * 1024;
286+
var a = (double)64 // dividend
287+
/ (double)4; // divisor
288+
}
289+
}
290+
";
291+
292+
var fixedCode = @"
293+
public class TestClass
294+
{
295+
public void TestMethod()
296+
{
297+
var x = 64L * 1024;
298+
var y = 64D /* test */ * 1024;
299+
var z = /* test */ 64L * 1024;
300+
var a = 64D // dividend
301+
/ 4D; // divisor
302+
}
303+
}
304+
";
305+
306+
DiagnosticResult[] expectedDiagnosticResult =
307+
{
308+
Diagnostic().WithLocation(6, 17),
309+
Diagnostic().WithLocation(7, 17),
310+
Diagnostic().WithLocation(8, 17),
311+
Diagnostic().WithLocation(9, 17),
312+
Diagnostic().WithLocation(10, 15),
313+
};
314+
315+
await VerifyCSharpFixAsync(testCode, expectedDiagnosticResult, fixedCode, CancellationToken.None).ConfigureAwait(false);
316+
}
269317
}
270318
}

0 commit comments

Comments
 (0)