Skip to content

Commit

Permalink
Merge pull request #74216 from CyrusNajmabadi/convertNamespace
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Jul 1, 2024
2 parents 16eecff + 44cbba7 commit 105491f
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 467 deletions.
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
{
[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

0 comments on commit 105491f

Please sign in to comment.