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

Order APIs case-insensitive with case-sensitive fallback #4370

Merged
merged 1 commit into from
Oct 27, 2020
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
22 changes: 20 additions & 2 deletions src/PublicApiAnalyzers/Core/CodeFixes/DeclarePublicApiFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static void insertInList(List<string> list, string name)
{
for (int i = 0; i < list.Count; i++)
{
if (string.Compare(name, list[i], StringComparison.Ordinal) < 0)
if (IgnoreCaseWhenPossibleComparer.Instance.Compare(name, list[i]) < 0)
{
list.Insert(i, name);
return;
Expand Down Expand Up @@ -231,7 +231,7 @@ await publicSurfaceAreaAdditionalDocument.GetTextAsync(cancellationToken).Config
.Where(d => d.Location.IsInSource)
.GroupBy(d => d.Location.SourceTree);

var newSymbolNames = new SortedSet<string>();
var newSymbolNames = new SortedSet<string>(IgnoreCaseWhenPossibleComparer.Instance);
var symbolNamesToRemoveBuilder = PooledHashSet<string>.GetInstance();

foreach (IGrouping<SyntaxTree, Diagnostic> grouping in groupedDiagnostics)
Expand Down Expand Up @@ -358,5 +358,23 @@ private class PublicSurfaceAreaFixAllProvider : FixAllProvider
return new FixAllAdditionalDocumentChangeAction(title, fixAllContext.Solution, diagnosticsToFix);
}
}

private sealed class IgnoreCaseWhenPossibleComparer : IComparer<string>
{
public static readonly IgnoreCaseWhenPossibleComparer Instance = new();

private IgnoreCaseWhenPossibleComparer()
{
}

public int Compare(string x, string y)
{
var result = StringComparer.OrdinalIgnoreCase.Compare(x, y);
if (result == 0)
result = StringComparer.Ordinal.Compare(x, y);

return result;
}
}
}
}
29 changes: 29 additions & 0 deletions src/PublicApiAnalyzers/UnitTests/DeclarePublicApiAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,35 @@ public class {|RS0016:{|RS0016:C2|}|} { }
await VerifyCSharpAdditionalFileFixAsync(source, shippedText, unshippedText, fixedUnshippedText);
}

[Fact]
public async Task TestMultipleMissingTypeAndMember_CaseSensitiveFix()
{
var source = @"
public class {|RS0016:C|}
{
private C() { }
public int {|RS0016:Field_A|};
public int {|RS0016:Field_b|};
public int {|RS0016:Field_C|};
public int {|RS0016:Field_d|};
}

public class {|RS0016:{|RS0016:C2|}|} { }
";

var shippedText = @"";
var unshippedText = @"";
var fixedUnshippedText = @"C
C.Field_A -> int
C.Field_b -> int
C.Field_C -> int
C.Field_d -> int
C2
C2.C2() -> void";

await VerifyCSharpAdditionalFileFixAsync(source, shippedText, unshippedText, fixedUnshippedText);
}

[Fact]
public async Task TestChangingMethodSignatureForAnUnshippedMethod_Fix()
{
Expand Down