Skip to content

Commit

Permalink
Fall back to case sensitive comparisons if case-insensitive is a match
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jun 28, 2017
1 parent 6e957c7 commit 58c7c46
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,10 @@ private int CompareUsings(UsingDirectiveSyntax left, UsingDirectiveSyntax right)
{
if ((left.Alias != null) && (right.Alias != null))
{
return CultureInfo.InvariantCulture.CompareInfo.Compare(left.Alias.Name.Identifier.ValueText, right.Alias.Name.Identifier.ValueText, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreWidth);
return NameSyntaxHelpers.Compare(left.Alias.Name, right.Alias.Name);
}

return CultureInfo.InvariantCulture.CompareInfo.Compare(left.Name.ToNormalizedString(), right.Name.ToNormalizedString(), CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreWidth);
return NameSyntaxHelpers.Compare(left.Name, right.Name);
}

private bool IsSeparatedSystemUsing(UsingDirectiveSyntax syntax)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,35 @@ namespace Bar
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
}

[Fact]
[WorkItem(2336, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2336")]
public async Task TestUsingDirectivesCaseSensitivityAsync()
{
var testCode = @"namespace First
{
using Second;
using second;
}
namespace Second { }
namespace second { }";

var fixedTestCode = @"namespace First
{
using second;
using Second;
}
namespace Second { }
namespace second { }";

DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(3, 5);

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);
}

[Fact]
public async Task TestInvalidOrderedUsingDirectivesWithInlineCommentsAsync()
{
Expand Down
17 changes: 17 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers/Helpers/NameSyntaxHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace StyleCop.Analyzers.Helpers
{
using System.Globalization;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -44,6 +45,22 @@ internal static string ToUnaliasedString(this NameSyntax nameSyntax)
return StringBuilderPool.ReturnAndFree(sb);
}

internal static int Compare(NameSyntax first, NameSyntax second)
{
string left = first.ToNormalizedString();
string right = second.ToNormalizedString();

// First compare without considering case
int result = CultureInfo.InvariantCulture.CompareInfo.Compare(left, right, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreWidth);
if (result == 0)
{
// Compare case if they matched
result = CultureInfo.InvariantCulture.CompareInfo.Compare(left, right, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreWidth);
}

return result;
}

private static void BuildName(NameSyntax nameSyntax, StringBuilder builder, bool includeAlias)
{
if (nameSyntax.IsKind(SyntaxKind.IdentifierName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static void CheckIncorrectlyOrderedUsingsAndReportDiagnostic(SyntaxNodeA
{
if (previousUsingDirective != null)
{
if (CultureInfo.InvariantCulture.CompareInfo.Compare(previousUsingDirective.Name.ToNormalizedString(), directive.Name.ToNormalizedString(), CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreWidth) > 0)
if (NameSyntaxHelpers.Compare(previousUsingDirective.Name, directive.Name) > 0)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, previousUsingDirective.GetLocation()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ private static void CheckUsingDeclarations(SyntaxNodeAnalysisContext context, Sy
{
if (lastStaticUsingDirective != null)
{
var firstName = lastStaticUsingDirective.Name.ToNormalizedString();
var secondName = usingDirective.Name.ToNormalizedString();
var firstName = lastStaticUsingDirective.Name;
var secondName = usingDirective.Name;

if (CultureInfo.InvariantCulture.CompareInfo.Compare(firstName, secondName, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreWidth) > 0)
if (NameSyntaxHelpers.Compare(firstName, secondName) > 0)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, lastStaticUsingDirective.GetLocation(), new[] { firstName, secondName }));
context.ReportDiagnostic(Diagnostic.Create(
Descriptor,
lastStaticUsingDirective.GetLocation(),
new[] { firstName.ToNormalizedString(), secondName.ToNormalizedString() }));
return;
}
}
Expand Down

0 comments on commit 58c7c46

Please sign in to comment.