Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed #220: Codefix for RECS0113 C# Redundant comma in array initiali…
…zer does not format correctly
  • Loading branch information
Rpinski committed May 3, 2016
1 parent c93cd67 commit 20a38ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Expand Up @@ -46,11 +46,15 @@ public async override Task RegisterCodeFixesAsync(CodeFixContext context)
return;

var redundantComma = node.Expressions.GetSeparator(elementCount - 1);
var newSeparatedExpList = new List<SyntaxNodeOrToken>(node.Expressions.GetWithSeparators().Where(t => t != redundantComma));
var lastExp = newSeparatedExpList.Last();
newSeparatedExpList.RemoveAt(newSeparatedExpList.Count - 1);
newSeparatedExpList.Add(lastExp.WithTrailingTrivia(redundantComma.TrailingTrivia));

var newRoot = root.ReplaceNode(
node,
node
.WithExpressions(SyntaxFactory.SeparatedList(node.Expressions.ToArray()))
.WithAdditionalAnnotations(Formatter.Annotation));
.WithExpressions(SyntaxFactory.SeparatedList<ExpressionSyntax>(SyntaxFactory.NodeOrTokenList(newSeparatedExpList))));

context.RegisterCodeFix(CodeActionFactory.Create(node.Span, diagnostic.Severity, "Remove ','", document.WithSyntaxRoot(newRoot)), diagnostic);
}
Expand Down
26 changes: 26 additions & 0 deletions Tests/CSharp/Diagnostics/RedundantCommaInArrayInitializerTests.cs
Expand Up @@ -95,5 +95,31 @@ void TestMethod ()
}";
Analyze<RedundantCommaInArrayInitializerAnalyzer>(input);
}

[Test]
public void TestPreserveTrivia()
{
Analyze<RedundantCommaInArrayInitializerAnalyzer>(@"
class TestClass
{
void TestMethod()
{
var a = new int[] {
1,
2$,$
};
}
}", @"
class TestClass
{
void TestMethod()
{
var a = new int[] {
1,
2
};
}
}");
}
}
}

0 comments on commit 20a38ce

Please sign in to comment.