Skip to content

Commit c19a6ac

Browse files
committed
Order APIs case-insensitive with case-sensitive fallback
1 parent 9f68083 commit c19a6ac

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/PublicApiAnalyzers/Core/CodeFixes/DeclarePublicApiFix.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static void insertInList(List<string> list, string name)
115115
{
116116
for (int i = 0; i < list.Count; i++)
117117
{
118-
if (string.Compare(name, list[i], StringComparison.Ordinal) < 0)
118+
if (IgnoreCaseWhenPossibleComparer.Instance.Compare(name, list[i]) < 0)
119119
{
120120
list.Insert(i, name);
121121
return;
@@ -231,7 +231,7 @@ await publicSurfaceAreaAdditionalDocument.GetTextAsync(cancellationToken).Config
231231
.Where(d => d.Location.IsInSource)
232232
.GroupBy(d => d.Location.SourceTree);
233233

234-
var newSymbolNames = new SortedSet<string>();
234+
var newSymbolNames = new SortedSet<string>(IgnoreCaseWhenPossibleComparer.Instance);
235235
var symbolNamesToRemoveBuilder = PooledHashSet<string>.GetInstance();
236236

237237
foreach (IGrouping<SyntaxTree, Diagnostic> grouping in groupedDiagnostics)
@@ -358,5 +358,23 @@ private class PublicSurfaceAreaFixAllProvider : FixAllProvider
358358
return new FixAllAdditionalDocumentChangeAction(title, fixAllContext.Solution, diagnosticsToFix);
359359
}
360360
}
361+
362+
private sealed class IgnoreCaseWhenPossibleComparer : IComparer<string>
363+
{
364+
public static readonly IgnoreCaseWhenPossibleComparer Instance = new();
365+
366+
private IgnoreCaseWhenPossibleComparer()
367+
{
368+
}
369+
370+
public int Compare(string x, string y)
371+
{
372+
var result = StringComparer.OrdinalIgnoreCase.Compare(x, y);
373+
if (result == 0)
374+
result = StringComparer.Ordinal.Compare(x, y);
375+
376+
return result;
377+
}
378+
}
361379
}
362380
}

src/PublicApiAnalyzers/UnitTests/DeclarePublicApiAnalyzerTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,35 @@ public class {|RS0016:{|RS0016:C2|}|} { }
17571757
await VerifyCSharpAdditionalFileFixAsync(source, shippedText, unshippedText, fixedUnshippedText);
17581758
}
17591759

1760+
[Fact]
1761+
public async Task TestMultipleMissingTypeAndMember_CaseSensitiveFix()
1762+
{
1763+
var source = @"
1764+
public class {|RS0016:C|}
1765+
{
1766+
private C() { }
1767+
public int {|RS0016:Field_A|};
1768+
public int {|RS0016:Field_b|};
1769+
public int {|RS0016:Field_C|};
1770+
public int {|RS0016:Field_d|};
1771+
}
1772+
1773+
public class {|RS0016:{|RS0016:C2|}|} { }
1774+
";
1775+
1776+
var shippedText = @"";
1777+
var unshippedText = @"";
1778+
var fixedUnshippedText = @"C
1779+
C.Field_A -> int
1780+
C.Field_b -> int
1781+
C.Field_C -> int
1782+
C.Field_d -> int
1783+
C2
1784+
C2.C2() -> void";
1785+
1786+
await VerifyCSharpAdditionalFileFixAsync(source, shippedText, unshippedText, fixedUnshippedText);
1787+
}
1788+
17601789
[Fact]
17611790
public async Task TestChangingMethodSignatureForAnUnshippedMethod_Fix()
17621791
{

0 commit comments

Comments
 (0)