Skip to content

Commit

Permalink
Fix RCS1060 (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Feb 17, 2024
1 parent 248e785 commit 3f1a366
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 31 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix analyzer [RCS1159](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1159) ([PR](https://github.com/dotnet/roslynator/pull/1390))
- Fix analyzer [RCS1019](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1019) ([PR](https://github.com/dotnet/roslynator/pull/1402))
- Fix analyzer [RCS1250](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1250) ([PR](https://github.com/dotnet/roslynator/pull/1403))
- Fix analyzer [RCS1060](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1060) ([PR](https://github.com/dotnet/roslynator/pull/1401))
- Fix code fix for [CS8600](https://josefpihrt.github.io/docs/roslynator/fixes/CS8600) changing the wrong type when casts or `var` are involved ([PR](https://github.com/dotnet/roslynator/pull/1393) by @jroessel)

## [4.10.0] - 2024-01-24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ void Analyze(SyntaxList<MemberDeclarationSyntax> members)
{
foreach (MemberDeclarationSyntax member in members)
{
SyntaxKind kind = member.Kind();

if (kind == SyntaxKind.NamespaceDeclaration)
#if ROSLYN_4_0
if (member is BaseNamespaceDeclarationSyntax namespaceDeclaration)
#else
if (member is NamespaceDeclarationSyntax namespaceDeclaration)
#endif
{
var namespaceDeclaration = (NamespaceDeclarationSyntax)member;

Analyze(namespaceDeclaration.Members);
}
else if (SyntaxFacts.IsTypeDeclaration(kind))
else if (SyntaxFacts.IsTypeDeclaration(member.Kind()))
{
if (firstTypeDeclaration is null)
{
Expand Down Expand Up @@ -93,16 +93,18 @@ private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, MemberDe

private static bool ContainsSingleNamespaceWithSingleNonNamespaceMember(SyntaxList<MemberDeclarationSyntax> members)
{
MemberDeclarationSyntax member = members.SingleOrDefault(shouldThrow: false);

if (member?.Kind() != SyntaxKind.NamespaceDeclaration)
return false;

var namespaceDeclaration = (NamespaceDeclarationSyntax)member;
#if ROSLYN_4_0
if (members.SingleOrDefault(shouldThrow: false) is BaseNamespaceDeclarationSyntax namespaceDeclaration)
#else
if (members.SingleOrDefault(shouldThrow: false) is NamespaceDeclarationSyntax namespaceDeclaration)
#endif
{
MemberDeclarationSyntax member = namespaceDeclaration.Members.SingleOrDefault(shouldThrow: false);

member = namespaceDeclaration.Members.SingleOrDefault(shouldThrow: false);
return member is not null
&& member.Kind() != SyntaxKind.NamespaceDeclaration;
}

return member is not null
&& member.Kind() != SyntaxKind.NamespaceDeclaration;
return false;
}
}
2 changes: 1 addition & 1 deletion src/Refactorings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ if (y)
<Syntax>while statement</Syntax>
</Syntaxes>
</Refactoring>
<Refactoring Id="RR0046" Identifier="ExtractTypeDeclarationToNewFile" Title="Extract type declaration to a new file">
<Refactoring Id="RR0046" Identifier="ExtractTypeDeclarationToNewFile" Title="Extract type declaration to a new file" IsEnabledByDefault="false">
<OptionKey>extract_type_declaration_to_new_file</OptionKey>
<Syntaxes>
<Syntax>class declaration</Syntax>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public static void ComputeRefactorings(RefactoringContext context, DelegateDecla
private static void ComputeRefactorings(RefactoringContext context, MemberDeclarationSyntax memberDeclaration, SyntaxToken identifier)
{
if (identifier.Span.Contains(context.Span)
&& memberDeclaration.IsParentKind(SyntaxKind.NamespaceDeclaration, SyntaxKind.CompilationUnit)
&& memberDeclaration.IsParentKind(
SyntaxKind.NamespaceDeclaration,
#if ROSLYN_4_0
SyntaxKind.FileScopedNamespaceDeclaration,
#endif
SyntaxKind.CompilationUnit)
&& context.IsRootCompilationUnit
&& context.Workspace.Kind != WorkspaceKind.MiscellaneousFiles
&& ExtractTypeDeclarationToNewDocumentRefactoring.GetNonNestedTypeDeclarations((CompilationUnitSyntax)context.Root).Skip(1).Any())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Roslynator.CSharp.CodeFixes;
using Roslynator.Testing.CSharp;
using Xunit;

namespace Roslynator.CSharp.Analysis.Tests;

public class RCS1060DeclareEachTypeInSeparateFileTests : AbstractCSharpDiagnosticVerifier<DeclareEachTypeInSeparateFileAnalyzer, ExtractMemberToNewDocumentCodeFixProvider>
{
public override DiagnosticDescriptor Descriptor => DiagnosticRules.DeclareEachTypeInSeparateFile;

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.DeclareEachTypeInSeparateFile)]
public async Task Test_Namespace()
{
await VerifyDiagnosticAndFixAsync("""
namespace N
{
public class [|C1|]
{
}

public class [|C2|]
{
}
}
""", """
namespace N
{
public class C2
{
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.DeclareEachTypeInSeparateFile)]
public async Task Test_FileScopedNamespace()
{
await VerifyDiagnosticAndFixAsync("""
namespace N;

public class [|C1|]
{
}

public class [|C2|]
{
}
""", """
namespace N;

public class C2
{
}
""");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Roslynator.Testing.CSharp;
using Xunit;

namespace Roslynator.CSharp.Refactorings.Tests;

public class RR0046ExtractTypeDeclarationToNewFileTests : AbstractCSharpRefactoringVerifier
{
public override string RefactoringId { get; } = RefactoringIdentifiers.ExtractTypeDeclarationToNewFile;

[Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.ExtractTypeDeclarationToNewFile)]
public async Task Test_Namespace()
{
await VerifyRefactoringAsync("""
namespace N
{
public class C1
{

}

public class [||]C2
{

}
}
""", """
namespace N
{
public class C1
{

}
}
""", equivalenceKey: EquivalenceKey.Create(RefactoringId));
}

[Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.ExtractTypeDeclarationToNewFile)]
public async Task Test_FileScopedNamespace()
{
await VerifyRefactoringAsync("""
namespace N;

public class C1
{
}

public class [||]C2
{
}
""", """
namespace N;

public class C1
{
}

""", equivalenceKey: EquivalenceKey.Create(RefactoringId));
}
}
17 changes: 7 additions & 10 deletions src/VisualStudio/.roslynatorconfig
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
is_global = true

roslnator.max_line_length = 140
roslynator.prefix_field_identifier_with_underscore = false

roslynator.refactoring.RR0010.enabled = false
roslynator.refactoring.RR0012.enabled = false
roslynator.refactoring.RR0066.enabled = false
roslynator.refactoring.RR0088.enabled = false
roslynator.refactoring.RR0138.enabled = false
roslynator.refactoring.RR0171.enabled = false
roslynator.refactoring.RR0188.enabled = false
roslynator_refactoring.convert_foreach_to_for_and_reverse_loop.enabled = false
roslynator_refactoring.expand_initializer.enabled = false
roslynator_refactoring.extract_type_declaration_to_new_file.enabled = false
roslynator_refactoring.introduce_constructor.enabled = false
roslynator_refactoring.remove_all_documentation_comments.enabled = false
roslynator_refactoring.replace_method_with_property.enabled = false
roslynator_refactoring.use_string_empty_instead_of_empty_string_literal.enabled = false
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private static SyntaxNode RemoveNode(MemberDeclarationSyntax member)

int index = members.IndexOf(member);

if (index == 0
if (member.IsParentKind(SyntaxKind.NamespaceDeclaration)
&& index == 0
&& index < members.Count - 1)
{
MemberDeclarationSyntax nextMember = newMemberList[index];
Expand Down Expand Up @@ -113,10 +114,12 @@ private static IEnumerable<MemberDeclarationSyntax> GetNonNestedTypeDeclarations
{
SyntaxKind kind = member.Kind();

if (kind == SyntaxKind.NamespaceDeclaration)
#if ROSLYN_4_0
if (member is BaseNamespaceDeclarationSyntax namespaceDeclaration)
#else
if (member is NamespaceDeclarationSyntax namespaceDeclaration)
#endif
{
var namespaceDeclaration = (NamespaceDeclarationSyntax)member;

foreach (MemberDeclarationSyntax member2 in GetNonNestedTypeDeclarations(namespaceDeclaration.Members))
yield return member2;
}
Expand Down

0 comments on commit 3f1a366

Please sign in to comment.