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

Do not offer 'convert' namespace when the ns has sibling types #74216

Merged
merged 5 commits into from
Jul 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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.

using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
Expand Down Expand Up @@ -68,16 +67,14 @@ internal static bool CanOfferUseFileScoped(
if (!canOffer)
return false;

// even if we could offer this here, we have to make sure it would be legal. A file scoped namespace is
// only legal if it's the only namespace in the file and there are no top level statements.
var tooManyNamespaces = root.DescendantNodesAndSelf(n => n is CompilationUnitSyntax or BaseNamespaceDeclarationSyntax)
.OfType<BaseNamespaceDeclarationSyntax>()
.Take(2)
.Count() != 1;
if (tooManyNamespaces)
// even if we could offer this here, we have to make sure it would be legal.

// A file scoped namespace is only legal if it's the only top level member in the file.
if (root.Members is not [var singleMember] || singleMember != namespaceDeclaration)
return false;

if (root.Members.Any(m => m is GlobalStatementSyntax))
// A file scoped namespace is only legal if it's the only namespace in the file.
if (namespaceDeclaration.Members.Any(static m => m is BaseNamespaceDeclarationSyntax))
return false;

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1544,4 +1544,46 @@ limit 1
}
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/74214")]
public async Task TestNotWithClassAfter()
{
await new VerifyCS.Test
{
TestCode = """
namespace N
{
class Inner { }
}

class Outer { }
""",
LanguageVersion = LanguageVersion.CSharp10,
Options =
{
{ CSharpCodeStyleOptions.NamespaceDeclarations, NamespaceDeclarationPreference.FileScoped }
}
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/74214")]
public async Task TestNotWithClassBefore()
{
await new VerifyCS.Test
{
TestCode = """
class Outer { }

namespace N
{
class Inner { }
}
""",
LanguageVersion = LanguageVersion.CSharp10,
Options =
{
{ CSharpCodeStyleOptions.NamespaceDeclarations, NamespaceDeclarationPreference.FileScoped }
}
}.RunAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ namespace Microsoft.CodeAnalysis.CSharp.ConvertNamespace;
using static ConvertNamespaceTransform;

[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.ConvertNamespace), Shared]
internal class ConvertNamespaceCodeRefactoringProvider : SyntaxEditorBasedCodeRefactoringProvider
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal class ConvertNamespaceCodeRefactoringProvider() : SyntaxEditorBasedCodeRefactoringProvider
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: seal?

{
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public ConvertNamespaceCodeRefactoringProvider()
{
}

protected override ImmutableArray<FixAllScope> SupportedFixAllScopes
=> [FixAllScope.Project, FixAllScope.Solution];

Expand Down
Loading
Loading