Skip to content

Commit

Permalink
Fix RCS1056 (dotnet#1096)
Browse files Browse the repository at this point in the history
Co-authored-by: Josef Pihrt <[email protected]>
  • Loading branch information
2 people authored and JochemHarmes committed Oct 30, 2023
1 parent 9ae7b2c commit e8e9b8a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix [RCS1169](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1169.md) ([#1092](https://github.com/JosefPihrt/Roslynator/pull/1092)).
- Recognize more shapes of IAsyncEnumerable as being Async ([RCS1047](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1047.md)) ([#1084](https://github.com/josefpihrt/roslynator/pull/1084)).
- Fix [RCS1197](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1197.md) ([#1093](https://github.com/JosefPihrt/Roslynator/pull/1093)).
- Fix [RCS1056](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1056.md) ([#1096](https://github.com/JosefPihrt/Roslynator/pull/1096)).

## [4.3.0] - 2023-04-24

Expand Down
13 changes: 13 additions & 0 deletions src/CSharp/CSharp/Syntax/UsingDirectiveListInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ internal static UsingDirectiveListInfo Create(NamespaceDeclarationSyntax namespa
return new UsingDirectiveListInfo(namespaceDeclaration, namespaceDeclaration.Usings);
}

internal static UsingDirectiveListInfo Create(FileScopedNamespaceDeclarationSyntax namespaceDeclaration)
{
if (namespaceDeclaration is null)
return default;

return new UsingDirectiveListInfo(namespaceDeclaration, namespaceDeclaration.Usings);
}

internal static UsingDirectiveListInfo Create(CompilationUnitSyntax compilationUnit)
{
if (compilationUnit is null)
Expand All @@ -113,6 +121,11 @@ internal static UsingDirectiveListInfo Create(SyntaxNode declaration)
var namespaceDeclaration = (NamespaceDeclarationSyntax)declaration;
return new UsingDirectiveListInfo(namespaceDeclaration, namespaceDeclaration.Usings);
}
case SyntaxKind.FileScopedNamespaceDeclaration:
{
var fileScopedNamespaceDeclaration = (FileScopedNamespaceDeclarationSyntax)declaration;
return new UsingDirectiveListInfo(fileScopedNamespaceDeclaration, fileScopedNamespaceDeclaration.Usings);
}
}

return default;
Expand Down
9 changes: 9 additions & 0 deletions src/CSharp/CSharp/SyntaxInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,15 @@ public static UsingDirectiveListInfo UsingDirectiveListInfo(NamespaceDeclaration
return Syntax.UsingDirectiveListInfo.Create(declaration);
}

/// <summary>
/// Creates a new <see cref="Syntax.UsingDirectiveListInfo"/> from the specified declaration.
/// </summary>
/// <param name="declaration"></param>
public static UsingDirectiveListInfo UsingDirectiveListInfo(FileScopedNamespaceDeclarationSyntax declaration)
{
return Syntax.UsingDirectiveListInfo.Create(declaration);
}

/// <summary>
/// Creates a new <see cref="Syntax.XmlElementInfo"/> from the specified xml node.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,64 @@ void M()
string u2 = System.String.Empty;
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidUsageOfUsingAliasDirective)]
public async Task Test_FileScopedNamespaces()
{
await VerifyDiagnosticAndFixAsync(@"
namespace FileScopedNamespace;
[|using s = System;|]
class C
{
void M()
{
string u1 = s.String.Empty;
}
}
", @"
namespace FileScopedNamespace;
class C
{
void M()
{
string u1 = System.String.Empty;
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AvoidUsageOfUsingAliasDirective)]
public async Task Test_BlockNamespaces()
{
await VerifyDiagnosticAndFixAsync(@"
namespace BlockNamespace
{
[|using s = System;|]
class C
{
void M()
{
string u1 = s.String.Empty;
}
}
}
", @"
namespace BlockNamespace
{
class C
{
void M()
{
string u1 = System.String.Empty;
}
}
}
");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static async Task<Document> RefactorAsync(

SyntaxNode parent = usingDirective.Parent;

Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration), "");
Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration, SyntaxKind.FileScopedNamespaceDeclaration), "");

int index = SyntaxInfo.UsingDirectiveListInfo(parent).IndexOf(usingDirective);

Expand All @@ -43,6 +43,8 @@ private static SyntaxNode RemoveUsingDirective(SyntaxNode node, int index)
return compilationUnit.RemoveNode(compilationUnit.Usings[index]);
case NamespaceDeclarationSyntax namespaceDeclaration:
return namespaceDeclaration.RemoveNode(namespaceDeclaration.Usings[index]);
case FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclaration:
return fileScopedNamespaceDeclaration.RemoveNode(fileScopedNamespaceDeclaration.Usings[index]);
}

return node;
Expand Down

0 comments on commit e8e9b8a

Please sign in to comment.