Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SA1010 now properly supports index initializers #1622

Merged
merged 2 commits into from
Oct 14, 2015
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
Expand Up @@ -161,6 +161,64 @@ public int this [ [CLSCompliant(true)]int index]
await this.VerifyCSharpFixAsync(testCode, ExpectedCode).ConfigureAwait(false);
}

/// <summary>
/// Verify that index initializers are properly handled.
/// Regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1617
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task VerifyIndexInitializerAsync()
{
var testCode = @"using System.Collections.Generic;

public class TestClass
{
public void TestMethod(IDictionary<ulong, string> items)
{
var test = new Dictionary<ulong, string>(items) { [100] = ""100"" };
}
}
";

await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verify that index initializer scope determination is working as intended.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task VerifyThatIndexInitializerScopeIsDeterminedProperlyAsync()
{
var testCode = @"using System.Collections.Generic;

public class TestClass
{
public void TestMethod(IDictionary<ulong, string> items)
{
int[] indexes = { 0 };
var dictionary = new Dictionary<int, int> { [indexes [0]] = 0 };
}
}
";

var fixedTestCode = @"using System.Collections.Generic;

public class TestClass
{
public void TestMethod(IDictionary<ulong, string> items)
{
int[] indexes = { 0 };
var dictionary = new Dictionary<int, int> { [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);
}

/// <inheritdoc/>
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -104,5 +104,11 @@ 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)
{
return token.Parent.IsKind(SyntaxKind.BracketedArgumentList)
&& token.Parent.Parent.IsKind(SyntaxKind.ImplicitElementAccess);
}
}
}