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

Fix method GetRootNamespace (RCS0015) #1000

Merged
merged 2 commits into from Nov 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix refactoring ([RR0180](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RR0180.md)) ([#988](https://github.com/josefpihrt/roslynator/pull/988)).
- Recognize `ArgumentNullException.ThrowIfNull` ([RCS1227](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1227.md)) ([#992](https://github.com/josefpihrt/roslynator/pull/992)).
- Detect pattern matching in [RCS1146](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1146.md) ([#999](https://github.com/josefpihrt/roslynator/pull/999)).
- Handle `using` directive that starts with `global::` [RCS0015](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS0015.md) ([#1000](https://github.com/josefpihrt/roslynator/pull/1000)).

## [4.1.2] - 2022-10-31

Expand Down
11 changes: 8 additions & 3 deletions src/CSharp/CSharp/Extensions/SyntaxExtensions.cs
Expand Up @@ -4211,10 +4211,15 @@ public static bool IsVoid(this TypeSyntax type)
#region UsingDirectiveSyntax
internal static IdentifierNameSyntax GetRootNamespace(this UsingDirectiveSyntax usingDirective)
{
if (usingDirective.Name is IdentifierNameSyntax identifierName)
NameSyntax name = usingDirective.Name;

if (name is AliasQualifiedNameSyntax aliasQualifiedName)
name = aliasQualifiedName.Name;

if (name is IdentifierNameSyntax identifierName)
return identifierName;

if (usingDirective.Name is QualifiedNameSyntax qualifiedName)
if (name is QualifiedNameSyntax qualifiedName)
{
NameSyntax left;

Expand All @@ -4233,7 +4238,7 @@ internal static IdentifierNameSyntax GetRootNamespace(this UsingDirectiveSyntax
}
else
{
SyntaxDebug.Fail(usingDirective.Name);
SyntaxDebug.Fail(name);
}

return null;
Expand Down
Expand Up @@ -30,6 +30,30 @@ class C

using System.Threading;

class C
{
}
", options: Options.AddConfigOption(ConfigOptionKeys.BlankLineBetweenUsingDirectives, ConfigOptionValues.BlankLineBetweenUsingDirectives_SeparateGroups));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.BlankLineBetweenUsingDirectives)]
public async Task Test_AddEmptyLine_GlobalAlias()
{
await VerifyDiagnosticAndFixAsync(@"
using global::System;[||]
using Microsoft.CodeAnalysis;[||]
using System.Threading;

class C
{
}
", @"
using global::System;

using Microsoft.CodeAnalysis;

using System.Threading;

class C
{
}
Expand Down