From f983b5f165190e0083a78a866d3cd908196f6b2f Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Thu, 8 Dec 2016 23:16:55 +0100 Subject: [PATCH] Changes due to review comments. --- .../SA1413CodeFixProvider.cs | 80 +------------------ .../MaintainabilityRules/SA1413UnitTests.cs | 4 +- ...seTrailingCommasInMultiLineInitializers.cs | 3 +- 3 files changed, 7 insertions(+), 80 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs index 3b03aa2ed..d1c177326 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs @@ -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; /// @@ -54,78 +49,11 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task 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(initializer.Expressions); - var last = existingItems.Last(); - existingItems.Remove(last); - existingItems.Add(last.WithoutTrailingTrivia()); - - var existingSeparators = initializer.Expressions.GetSeparators(); - var newSeparators = new List(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(initializer.Initializers); - var last = existingItems.Last(); - existingItems.Remove(last); - existingItems.Add(last.WithoutTrailingTrivia()); - - var existingSeparators = initializer.Initializers.GetSeparators(); - var newSeparators = new List(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)); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1413UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1413UnitTests.cs index 7281a3074..43dfbeec6 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1413UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1413UnitTests.cs @@ -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 */ } "; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs index f218c0cf3..1a2d131db 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs @@ -86,8 +86,7 @@ private static void HandleEnumMemberDeclarationAction(SyntaxNodeAnalysisContext return; } - var commas = initializer.ChildNodesAndTokens().OfType().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())); }