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

Have the naming style fixer understand the 'I' pattern for interfaces better #75758

Merged
merged 1 commit into from
Nov 6, 2024
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
40 changes: 31 additions & 9 deletions src/Analyzers/CSharp/Tests/NamingStyles/NamingStylesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -21,14 +19,10 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.NamingStyles;

[Trait(Traits.Feature, Traits.Features.NamingStyle)]
public class NamingStylesTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor
public sealed class NamingStylesTests(ITestOutputHelper logger)
: AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor(logger)
{
public NamingStylesTests(ITestOutputHelper logger)
: base(logger)
{
}

private static readonly NamingStylesTestOptionSets s_options = new NamingStylesTestOptionSets(LanguageNames.CSharp);
private static readonly NamingStylesTestOptionSets s_options = new(LanguageNames.CSharp);

internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new CSharpNamingStyleDiagnosticAnalyzer(), new NamingStyleCodeFixProvider());
Expand Down Expand Up @@ -1402,6 +1396,34 @@ internal interface
""", new TestParameters(options: s_options.InterfaceNamesStartWithI));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/17656")]
public async Task TestInterfacesStartWithIOnTypeThatAlreadyStartsWithI1()
{
await TestInRegularAndScript1Async("""
interface [|InputStream|] { }
""", """
interface IInputStream { }
""", new TestParameters(options: s_options.InterfaceNamesStartWithI));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/17656")]
public async Task TestInterfacesStartWithIOnTypeThatAlreadyStartsWithI2()
{
await TestInRegularAndScript1Async("""
interface [|Stream|] { }
""", """
interface IStream { }
""", new TestParameters(options: s_options.InterfaceNamesStartWithI));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/17656")]
public async Task TestInterfacesStartWithIOnTypeThatAlreadyStartsWithI3()
{
await TestMissingInRegularAndScriptAsync("""
interface [|IInputStream|] { }
""", new TestParameters(options: s_options.InterfaceNamesStartWithI));
}

#if CODE_STYLE
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/42218")]
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,25 +427,34 @@ private string EnsureSuffix(string name)
for (var i = Suffix.Length; i > 0; i--)
{
if (name.EndsWith(Suffix[..i]))
{
return name + Suffix[i..];
}
}

return name + Suffix;
}

private string EnsurePrefix(string name)
{
// Exceptional cases. If the name is some interface name (like `InputStream`) and the rule is to have a single
// character prefix like "Add `I` for interfaces" don't consider the existing 'I' to be a match of the prefix.

if (Prefix is [var prefixChar] &&
char.IsUpper(prefixChar) &&
name is [var nameChar1, var nameChar2, ..] &&
prefixChar == nameChar1 &&
char.IsLower(nameChar2))
{
// return IInputStream here, even though InputStream already starts with 'I'.
return Prefix + name;
}

// If the name already starts with any suffix of the Prefix, only prepend the prefix of
// the Prefix not contained in the longest such Prefix suffix. For example, if the
// required prefix is "catdog_" and the name is "dog_test", then only prepend "cat".
for (var i = 0; i < Prefix.Length; i++)
{
if (name.StartsWith(Prefix[i..]))
{
return Prefix[..i] + name;
}
}

return Prefix + name;
Expand Down Expand Up @@ -480,13 +489,11 @@ public void WriteTo(ObjectWriter writer)
}

public static NamingStyle ReadFrom(ObjectReader reader)
{
return new NamingStyle(
=> new(
reader.ReadGuid(),
reader.ReadString(),
reader.ReadString(),
reader.ReadString(),
reader.ReadString(),
(Capitalization)reader.ReadInt32());
}
}
Loading