Skip to content

Commit

Permalink
Changes due to review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Lemstra committed Dec 8, 2016
1 parent b0e5f48 commit f983b5f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@

namespace StyleCop.Analyzers.MaintainabilityRules
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

/// <summary>
Expand Down Expand Up @@ -54,78 +49,11 @@ 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 parent = syntaxRoot.FindNode(diagnostic.Location.SourceSpan).Parent;
var syntaxNode = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

SyntaxNode newParent = null;
switch (parent.Kind())
{
case SyntaxKind.ObjectInitializerExpression:
case SyntaxKind.ArrayInitializerExpression:
case SyntaxKind.CollectionInitializerExpression:
newParent = RewriteInitializer((InitializerExpressionSyntax)parent);
break;

case SyntaxKind.AnonymousObjectCreationExpression:
newParent = RewriteAnonymousObjectInitializer((AnonymousObjectCreationExpressionSyntax)parent);
break;

case SyntaxKind.EnumDeclaration:
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
TextChange textChange = GetEnumMemberTextChange(diagnostic, syntaxRoot);
return document.WithText(text.WithChanges(textChange));

default:
throw new InvalidOperationException("Unknown initializer type: " + parent.Kind());
}

var newSyntaxRoot = syntaxRoot.ReplaceNode(parent, newParent);

var newDocument = document.WithSyntaxRoot(newSyntaxRoot.WithoutFormatting());
return newDocument;
}

private static SyntaxNode RewriteInitializer(InitializerExpressionSyntax initializer)
{
var existingItems = new List<ExpressionSyntax>(initializer.Expressions);
var last = existingItems.Last();
existingItems.Remove(last);
existingItems.Add(last.WithoutTrailingTrivia());

var existingSeparators = initializer.Expressions.GetSeparators();
var newSeparators = new List<SyntaxToken>(existingSeparators);
newSeparators.Add(SyntaxFactory.Token(SyntaxKind.CommaToken).WithTrailingTrivia(last.GetTrailingTrivia()));

var newInitializerExpressions = SyntaxFactory.SeparatedList(
existingItems,
newSeparators);

var fixedInitializer = initializer.WithExpressions(newInitializerExpressions);
return fixedInitializer;
}

private static SyntaxNode RewriteAnonymousObjectInitializer(AnonymousObjectCreationExpressionSyntax initializer)
{
var existingItems = new List<AnonymousObjectMemberDeclaratorSyntax>(initializer.Initializers);
var last = existingItems.Last();
existingItems.Remove(last);
existingItems.Add(last.WithoutTrailingTrivia());

var existingSeparators = initializer.Initializers.GetSeparators();
var newSeparators = new List<SyntaxToken>(existingSeparators);
newSeparators.Add(SyntaxFactory.Token(SyntaxKind.CommaToken).WithTrailingTrivia(last.GetTrailingTrivia()));

var newInitializerExpressions = SyntaxFactory.SeparatedList(
existingItems,
newSeparators);

var fixedInitializer = initializer.WithInitializers(newInitializerExpressions);
return fixedInitializer;
}

private static TextChange GetEnumMemberTextChange(Diagnostic diagnostic, SyntaxNode syntaxRoot)
{
var member = (EnumMemberDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
return new TextChange(diagnostic.Location.SourceSpan, member.ToString() + ",");
TextChange textChange = new TextChange(diagnostic.Location.SourceSpan, syntaxNode.ToString() + ",");
return document.WithText(text.WithChanges(textChange));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ public async Task VerifyEnumWithValueWithoutTrailingCommaAsync()
{
var testCode = @"enum TestEnum
{
One = 2
One = 2 /* test comment */
}
";

var fixedTestCode = @"enum TestEnum
{
One = 2,
One = 2, /* test comment */
}
";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ private static void HandleEnumMemberDeclarationAction(SyntaxNodeAnalysisContext
return;
}

var commas = initializer.ChildNodesAndTokens().OfType<SyntaxNodeOrToken>().Where(token => token.IsKind(SyntaxKind.CommaToken));
if (initializer.Members.Count() != commas.Count())
if (initializer.Members.Count() != initializer.Members.SeparatorCount)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, lastMember.GetLocation()));
}
Expand Down

0 comments on commit f983b5f

Please sign in to comment.