From 967a6cd5c226bd2e196080ecf290595cbcb8e877 Mon Sep 17 00:00:00 2001 From: vweijsters Date: Tue, 13 Oct 2015 22:13:26 +0200 Subject: [PATCH 1/2] SA1010 now properly supports index initializers --- .../SpacingRules/SA1010UnitTests.cs | 22 +++++++++++++++++++ ...ningSquareBracketsMustBeSpacedCorrectly.cs | 18 ++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs index 2baec871b..a202d7974 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs @@ -161,6 +161,28 @@ public int this [ [CLSCompliant(true)]int index] await this.VerifyCSharpFixAsync(testCode, ExpectedCode).ConfigureAwait(false); } + /// + /// Verify that index initializers are properly handled. + /// Regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1617 + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task VerifyIndexInitializerAsync() + { + var testCode = @"using System.Collections.Generic; + +public class TestClass +{ + public void TestMethod(IDictionary items) + { + var test = new Dictionary(items) { [100] = ""100"" }; + } +} +"; + + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + /// protected override IEnumerable GetCSharpDiagnosticAnalyzers() { diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs index 77b961064..e676a9eef 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs @@ -92,7 +92,7 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy bool followedBySpace = token.IsFollowedByWhitespace(); bool lastInLine = token.IsLastInLine(); - if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem) + if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && !IsPartOfIndexInitializer(token)) { // Opening square bracket must {not be preceded} by a space. context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingCodeFixProvider.RemovePreceding, "not be preceded")); @@ -104,5 +104,21 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingCodeFixProvider.RemoveFollowing, "not be followed")); } } + + private static bool IsPartOfIndexInitializer(SyntaxToken token) + { + var currentParent = token.Parent; + while (currentParent != null) + { + if (currentParent.IsKind(SyntaxKind.ImplicitElementAccess)) + { + return true; + } + + currentParent = currentParent.Parent; + } + + return false; + } } } From 77c2d7215fcf62cb011d7c36c3de6bff4725205c Mon Sep 17 00:00:00 2001 From: vweijsters Date: Wed, 14 Oct 2015 09:30:06 +0200 Subject: [PATCH 2/2] Improved index initializer detection --- .../SpacingRules/SA1010UnitTests.cs | 36 +++++++++++++++++++ ...ningSquareBracketsMustBeSpacedCorrectly.cs | 14 ++------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs index a202d7974..53aea2e34 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs @@ -183,6 +183,42 @@ public void TestMethod(IDictionary items) await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } + /// + /// Verify that index initializer scope determination is working as intended. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task VerifyThatIndexInitializerScopeIsDeterminedProperlyAsync() + { + var testCode = @"using System.Collections.Generic; + +public class TestClass +{ + public void TestMethod(IDictionary items) + { + int[] indexes = { 0 }; + var dictionary = new Dictionary { [indexes [0]] = 0 }; + } +} +"; + + var fixedTestCode = @"using System.Collections.Generic; + +public class TestClass +{ + public void TestMethod(IDictionary items) + { + int[] indexes = { 0 }; + var dictionary = new Dictionary { [indexes[0]] = 0 }; + } +} +"; + var expected = this.CSharpDiagnostic().WithLocation(8, 62).WithArguments("not be preceded"); + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); + await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); + } + /// protected override IEnumerable GetCSharpDiagnosticAnalyzers() { diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs index e676a9eef..721866905 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs @@ -107,18 +107,8 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy private static bool IsPartOfIndexInitializer(SyntaxToken token) { - var currentParent = token.Parent; - while (currentParent != null) - { - if (currentParent.IsKind(SyntaxKind.ImplicitElementAccess)) - { - return true; - } - - currentParent = currentParent.Parent; - } - - return false; + return token.Parent.IsKind(SyntaxKind.BracketedArgumentList) + && token.Parent.Parent.IsKind(SyntaxKind.ImplicitElementAccess); } } }