Skip to content
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
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