Skip to content

Commit 141f967

Browse files
Update SA1413CodeFixProvider to handle nested diagnostics in a single pass
1 parent 904ac40 commit 141f967

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Contributors to the New StyleCop Analyzers project.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.MaintainabilityRules
75
{
86
using System.Collections.Immutable;
@@ -50,11 +48,9 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5048

5149
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5250
{
53-
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5451
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
55-
var syntaxNode = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
56-
57-
TextChange textChange = new TextChange(diagnostic.Location.SourceSpan, syntaxNode.ToString() + ",");
52+
var textSpan = new TextSpan(diagnostic.Location.SourceSpan.End, 0);
53+
var textChange = new TextChange(textSpan, ",");
5854
return document.WithText(text.WithChanges(textChange));
5955
}
6056
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1413UnitTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,5 +516,69 @@ public async Task VerifyEnumWithValueWithoutTrailingCommaAsync()
516516

517517
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
518518
}
519+
520+
/// <summary>
521+
/// Verifies that the code fix handles nested diagnostics in a single pass.
522+
/// </summary>
523+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
524+
[Fact]
525+
public async Task VerifyNestedDiagnosticsFixAsync()
526+
{
527+
var testCode = @"
528+
class TestClass
529+
{
530+
void Foo()
531+
{
532+
var test = new
533+
{
534+
MyArray = new[]
535+
{
536+
new
537+
{
538+
MyProp = ""Test""
539+
},
540+
new
541+
{
542+
MyProp = ""asdf""
543+
}
544+
}
545+
};
546+
}
547+
}
548+
";
549+
550+
var fixedTestCode = @"
551+
class TestClass
552+
{
553+
void Foo()
554+
{
555+
var test = new
556+
{
557+
MyArray = new[]
558+
{
559+
new
560+
{
561+
MyProp = ""Test"",
562+
},
563+
new
564+
{
565+
MyProp = ""asdf"",
566+
},
567+
},
568+
};
569+
}
570+
}
571+
";
572+
573+
DiagnosticResult[] expected =
574+
{
575+
Diagnostic().WithSpan(8, 13, 18, 14),
576+
Diagnostic().WithSpan(12, 21, 12, 36),
577+
Diagnostic().WithSpan(14, 17, 17, 18),
578+
Diagnostic().WithSpan(16, 21, 16, 36),
579+
};
580+
581+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
582+
}
519583
}
520584
}

0 commit comments

Comments
 (0)