Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.MaintainabilityRules
{
using System.Collections.Immutable;
Expand Down Expand Up @@ -50,11 +48,9 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)

private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var syntaxNode = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);

TextChange textChange = new TextChange(diagnostic.Location.SourceSpan, syntaxNode.ToString() + ",");
var textSpan = new TextSpan(diagnostic.Location.SourceSpan.End, 0);
var textChange = new TextChange(textSpan, ",");
return document.WithText(text.WithChanges(textChange));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,5 +516,69 @@ public async Task VerifyEnumWithValueWithoutTrailingCommaAsync()

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

/// <summary>
/// Verifies that the code fix handles nested diagnostics in a single pass.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task VerifyNestedDiagnosticsFixAsync()
{
var testCode = @"
class TestClass
{
void Foo()
{
var test = new
{
MyArray = new[]
{
new
{
MyProp = ""Test""
},
new
{
MyProp = ""asdf""
}
}
};
}
}
";

var fixedTestCode = @"
class TestClass
{
void Foo()
{
var test = new
{
MyArray = new[]
{
new
{
MyProp = ""Test"",
},
new
{
MyProp = ""asdf"",
},
},
};
}
}
";

DiagnosticResult[] expected =
{
Diagnostic().WithSpan(8, 13, 18, 14),
Diagnostic().WithSpan(12, 21, 12, 36),
Diagnostic().WithSpan(14, 17, 17, 18),
Diagnostic().WithSpan(16, 21, 16, 36),
};

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